作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/
#include "cv.h" #include "highgui.h" #include <stdio.h> CvPoint prev_pt={-1,-1} ; CvPoint pt_beg={-1,-1},pt_end = {-1,-1}; CvPoint pt1={-1,-1},pt2={-1,-1}; CvRect rect; IplImage *src; IplImage *tmp; void onMouse(int event,int x,int y,int flags,void *param); int main(int argc,char **argv) { IplImage *dst; int c; if ((argc !=2)||!(src=/blog.armyourlife.info/cvLoadImage(argv[1],1))) { printf("Error when Loading the Picture....Exit/n"); return -1; } cvNamedWindow("SRC",1); cvShowImage("SRC",src); tmp = cvCloneImage(src); cvSetMouseCallback("SRC",onMouse,0); for (;;) { c = cvWaitKey(10); if ((char)c=='c') { if (pt_beg.x!=-1) { rect.x = min(pt_beg.x,pt_end.x); rect.y = min(pt_beg.y,pt_end.y); rect.height = abs(pt_end.y-pt_beg.y); rect.width = abs(pt_end.x-pt_beg.x); printf("RECT:height=%d,width=%d/n",rect.height,rect.width); cvSetImageROI(src,rect); printf("ROI:Height=%d,Width=%d./n",src->roi->height,src->roi->width); dst = cvCreateImage(cvSize(rect.width,rect.height),src->depth,src->nChannels); dst = cvCloneImage(src); printf("DST Height=%d,Width=%d/n",dst->height,dst->width); cvNamedWindow("Selected",1); cvShowImage("Selected",dst); cvSaveImage("selected.jpg",dst); printf("Finished!/n"); cvWaitKey(0); cvDestroyWindow("Selected"); cvReleaseImage(&dst); break; } } else if ((char)c==27) //ESC to exit { cvReleaseImage(&tmp); cvReleaseImage(&src); cvDestroyWindow("SRC"); break; } } return 0; } void onMouse(int event,int x,int y,int flags,void *) { printf("x=%d,y=%d./n",x,y); printf("Begin:x=%d,y=%d./n",pt_beg.x,pt_beg,y); printf("END:x=%d,y=%d./n",pt_end.x,pt_end.y); if (!tmp) { return ; } /*判断左键是否放开,并且没有进行拖拽*/ if (event == CV_EVENT_LBUTTONUP||!(flags & CV_EVENT_FLAG_LBUTTON)) {//CV_EVENT_LBUTTONUP左键放开,CV_EVENT_FLAG_LBUTTON左键拖拽, prev_pt=cvPoint(-1,-1); } /*若左键按下则记录为起始点*/ else if (event == CV_EVENT_LBUTTONDOWN) { prev_pt = cvPoint(x,y); pt_beg = cvPoint(x,y); } /*若鼠标在滑动,并且在拖拽状态*/ else if (event == CV_EVENT_MOUSEMOVE && (flags && CV_EVENT_FLAG_LBUTTON) ) { cvCopy(src,tmp); CvPoint pt =cvPoint(x,y); /*得到另外两个点,以便进行矩形拖动绘制*/ pt1.x = pt.x; pt1.y = prev_pt.y; pt2.x = prev_pt.x; pt2.y = pt.y; cvLine( tmp, prev_pt, pt1, cvScalarAll(255), 2, 8, 0 ); cvLine( tmp, prev_pt, pt2, cvScalarAll(255), 2, 8, 0 ); cvLine( tmp, pt1, pt, cvScalarAll(255), 2, 8, 0 ); cvLine( tmp, pt2, pt, cvScalarAll(255), 2, 8, 0 ); pt_end = pt; cvShowImage("SRC",tmp); } }
作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/
作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/
除非另有声明,本网站采用知识共享“署名 2.5 *”许可协议授权。