此篇跟上篇画矩形框类似。画框改为画点。用floodfill和带有掩码的copyTo得到抠图。右击确定floodFill中的种子点。
参考自http://blog.csdn.net/cv_yuippe/article/details/13035063
// substr.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include <string> #include <iostream> #include <vector> #include <OpenCV245.h> using namespace std; using namespace cv; Point p0 = Point(-1,-1); bool select_flag = false; Mat img,showImg; Mat imgmask; void onMouse(int event,int x,int y,int flags,void*param) { if(event==CV_EVENT_LBUTTONDOWN) { p0.x = x; p0.y = y; select_flag = true; } else if(select_flag &&event == CV_EVENT_MOUSEMOVE) { Point pt = Point(x,y); line(showImg,p0,pt,Scalar(0,255,0),2,8,0); line(imgmask,p0,pt,Scalar::all(0),2,8,0); p0 = pt; imshow("showImg",showImg); } else if(select_flag && event == CV_EVENT_LBUTTONUP) { select_flag = false; p0 = Point(-1,-1); //img.copyTo(showImg); } else if(event == CV_EVENT_RBUTTONUP)//右击显示抠出的图 { Mat dst;//若希望将圈选的结果相加,定义在外头 floodFill(imgmask,Point(x,y),Scalar(0));//point种子点所在的连通域被填充 //imshow("floodFill",imgmask); img.copyTo(dst,imgmask);//mask中所有不为零的点被dst对应的值填充 imshow("dst",dst); img.copyTo(showImg); imgmask.setTo(Scalar(255));//重新取值 } } //框图超过画面时会报错 int _tmain(int argc, _TCHAR* argv[]) { img = imread("C:\\Users\\Ma Ruihuan\\Desktop\\Lena.jpg",1); img.copyTo(showImg); imgmask.create(img.size(),CV_8UC1); imgmask.setTo(Scalar(255)); namedWindow("showImg"); imshow("showImg",showImg); setMouseCallback("showImg",onMouse,0); waitKey(0); //system("pause"); return 0; }