直接上码
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <ctype.h>
using namespace cv;
using namespace std;
CvPoint pt1 = Point(0,0);
CvPoint pt2 = Point(0,0);
bool is_selecting = false;
void cvMouseCallback(int mouseEvent,int x,int y,int flags,void* param)
{
switch(mouseEvent)
{
case CV_EVENT_LBUTTONDOWN:
pt1 = Point(x,y);
pt2 = Point(x,y);
is_selecting = true;
break;
case CV_EVENT_MOUSEMOVE:
if(is_selecting)
pt2 = Point(x,y);
break;
case CV_EVENT_LBUTTONUP:
pt2 = Point(x,y);
is_selecting = false;
break;
}
return;
}
int main(int argc,char* argv[])
{
char* window = "img";
Mat img = imread("D:\1.jpg",1);
Mat img_show,roi;
img.copyTo(img_show);
namedWindow(window,CV_WINDOW_AUTOSIZE);
setMouseCallback(window,cvMouseCallback);
bool shift_on = false;
while(true)
{
img.copyTo(img_show);
rectangle(img_show,pt1,pt2,Scalar(255,255,255));
imshow(window,img_show);
char key = cvWaitKey(10);
switch(key)
{