#include "cv.h" #include "highgui.h" #include <stdio.h> #include <stdlib.h> IplImage* inpaint_mask = 0; IplImage* img0 = 0, *img = 0, *inpainted = 0; CvPoint prev_pt = {-1,-1};//定义原点的位置 void on_mouse( int event, int x, int y, int flags, void* zhang)//event 是系统的回传代号,zhang是自己命名的代号名称 { if( !img )//当不存在图片时 return; if( event == CV_EVENT_LBUTTONUP || !(flags & CV_EVENT_FLAG_LBUTTON) )//如果鼠标的左键松开||左键不拖拽 prev_pt = cvPoint(-1,-1);//设置为起始点 else if( event == CV_EVENT_LBUTTONDOWN )//如果鼠标左键点击 prev_pt = cvPoint(x,y); else if( event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON) )//如果鼠标滑动了,并且左键滑动 { CvPoint pt = cvPoint(x,y);//创造点 if( prev_pt.x < 0 ) prev_pt = pt;//x小于0,那么默认基本不动 cvLine( inpaint_mask, prev_pt, pt, cvScalarAll(255), 5, 8, 0 );//数组,第一个点,第二个点,通道颜色,粗,反走样,0 cvLine( img, prev_pt, pt, cvScalarAll(255), 5, 8, 0 ); prev_pt = pt; cvShowImage( "image", img ); } } int main( int argc, char** argv ) { char* filename = argc >= 2 ? argv[1] : (char*)"Fruits.jpg";//提取图片,Fruits.jpg为源程序根目录下的图片 if( (img0 = cvLoadImage(filename,-1)) == 0 ) return 0; printf( "Hot keys: \n" "\tESC - quit the program\n" "\tr - restore the original image\n" "\ti or ENTER - run inpainting algorithm\n" "\t\t(before running it, paint something on the image)\n" );//各种操作的dos提示 cvNamedWindow( "image", 1 );//命名显示图片的窗口 img = cvCloneImage( img0 ); inpainted = cvCloneImage( img0 );//克隆原图片 inpaint_mask = cvCreateImage( cvGetSize(img), 8, 1 );//创造一个新的图片 cvZero( inpaint_mask );//各参数初始化为0 cvZero( inpainted ); cvShowImage( "image", img );//显示 cvShowImage( "watershed transform", inpainted ); cvSetMouseCallback( "image", on_mouse, 0 );//鼠标的呼叫函数,on_mouse是事件驱动的子程序 for(;;) { int c = cvWaitKey(0); if( (char)c == 27 ) break; if( (char)c == 'r' ) { cvZero( inpaint_mask ); cvCopy( img0, img,0 ); cvShowImage( "image", img ); } if( (char)c == 'i' || (char)c == '\n' ) { cvNamedWindow( "inpainted image", 1 ); cvInpaint( img, inpaint_mask, inpainted, 3, CV_INPAINT_TELEA ); cvShowImage( "inpainted image", inpainted ); } } return 1; } |