demo/2023816/test3.cpp

74 lines
1.8 KiB
C++
Raw Permalink Normal View History

2023-10-08 22:10:52 +08:00
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;
// ȫ<>ֱ<EFBFBD><D6B1><EFBFBD>ͼ<EFBFBD><CDBC>Դ
cv::Mat srcImage;
// <20><>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
cv::Rect roiRect;
// <20><>ʼ<EFBFBD><CABC>
cv::Point startPoint;
// <20><>ֹ<EFBFBD><D6B9>
cv::Point endPoint;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־λ
bool downFlag = false;
bool upFlag = false;
void MouseEvent(int event, int x, int y, int flags, void* data)
{
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><>ȡ<EFBFBD><C8A1>ʼ<EFBFBD><CABC>
if (event == EVENT_LBUTTONDOWN)
{
downFlag = true;
startPoint.x = x;
startPoint.y = y;
}
// <20><><EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><CFB5>ɿ<EFBFBD> <20><>ȡ<EFBFBD><C8A1>ֹ<EFBFBD><D6B9>
if (event == EVENT_LBUTTONUP)
{
upFlag = true;
endPoint.x = x;
endPoint.y = y;
}
// <20><>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>ѡ<EFBFBD><D1A1><EFBFBD><EFBFBD>ͼ<EFBFBD><CDBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (downFlag == true && upFlag == false)
{
Point tempPoint;
tempPoint.x = x;
tempPoint.y = y;
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾͼ<CABE><CDBC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
cv::Mat tempImage = srcImage.clone();
cv::rectangle(tempImage, startPoint, tempPoint,
Scalar(255, 0, 0), 2, 3, 0);
cv::imshow("ROIing", tempImage);
} // ѡ<><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
if (downFlag == true && upFlag == true)
{
// <20><>ȡѡ<C8A1><D1A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ROI
roiRect.width = abs(startPoint.x - endPoint.x);
roiRect.height = abs(startPoint.y - endPoint.y);
roiRect.x = min(startPoint.x, endPoint.x);
roiRect.y = min(startPoint.y, endPoint.y);
cv::Mat roiMat(srcImage, roiRect);
imshow("ROI", roiMat);
downFlag = false;
upFlag = false;
}
cv::waitKey(0);
}
int main(int argc, char** argv)
{
// <20><>ȡͼ<C8A1><CDBC><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6>Ƿ<EFBFBD><C7B7><EFBFBD>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD>
srcImage = cv::imread("./vedio/test.jpg");
if (srcImage.empty())
return -1;
// <20>ص<EFBFBD><D8B5>¼<EFBFBD><C2BC><EFBFBD>Ӧ
cv::namedWindow("ROIing");
cv::imshow("ROIing", srcImage);
cv::setMouseCallback("ROIing", MouseEvent, 0);
cv::waitKey(0);
return 0;
}