// 矩形框选取显示图片
#include
#include
#include
void mycallback(int event,int x,int y,int flags,void* param);
int main()//改进版:矩形框选择图片并保存
void mycallback(int event,int x,int y,int flags,void* param);
int main()
{
IplImage *img=cvLoadImage("/home/eve/Download/ExamplePics/Lena.jpg",1);
// cvNamedWindow("Lena",1);
cvShowImage("Lena",img);
cvSetMouseCallback("Lena",mycallback,img);
cvWaitKey();
cvReleaseImage(&img);
cvDestroyAllWindows();
}
void mycallback(int event,int x,int y,int flags,void* param)
{
IplImage *img=(IplImage*)param;
IplImage *src=cvCloneImage(img);
// IplImage *dst=cvCloneImage(img);
static CvPoint lastpoint;
static CvPoint endpoint;
if (event ==CV_EVENT_LBUTTONDOWN)
{
lastpoint=cvPoint(x,y);
// cvShowImage("Lena",src);
}
if((event==CV_EVENT_MOUSEMOVE) && (flags & CV_EVENT_FLAG_LBUTTON))
{
endpoint=cvPoint(x,y);
cvDrawRect(src,lastpoint,endpoint,CV_RGB(0,0,255),1,8,0);
cvShowImage("Lena",src);
}
if((event==CV_EVENT_LBUTTONUP))
{
endpoint=cvPoint(x,y);
cvDrawRect(src,lastpoint,endpoint,CV_RGB(0,0,255),1,8,0);
cvShowImage("Lena",src);
CvRect rect;
if(endpoint.x
rect.x=endpoint.x+1;
}
else
{
rect.x=lastpoint.x+1;
}
if(endpoint.y
rect.y=endpoint.y+1;
}
else
{
rect.y=lastpoint.y+1;
}
rect.width=abs(lastpoint.x-endpoint.x)-1;
rect.height=abs(lastpoint.y-endpoint.y)-1;
IplImage *dst=cvCreateImage(cvSize(rect.width,rect.height),8,3);
cvSetImageROI(src,rect);
dst=cvCloneImage(src);
cvShowImage("dst",dst);
cvSaveImage("Part.jpg",dst);
}