本文实例为大家分享了Opencv使用鼠标任意形状抠图的具体代码,供大家参考,具体内容如下
主要的方法思路是:首先利用鼠标在图上画任意形状,利用掩码将任意形状抠出来
主要难点是怎么填充,因为鼠标在画线的时候,滑动越快,点是不连续的,利用floodFill和drawContours都是没有办法进行填充的,从另一个方面想,一个面是由很多个点组成的,虽然鼠标滑动保存下来的就是一系列点,可以利用这一系列点构成一个面,利用面的性质进行填充就比较简单了。
一、首先使用鼠标点击事件,鼠标点击事件的函数为:
void on_mouse(int event, int x, int y, int flags, void* ustc)
常见的鼠标事件有:
event事件有:
CV_EVENT_MOUSEMOVE =0, //鼠标移动CV_EVENT_LBUTTONDOWN =1, //按下左键CV_EVENT_RBUTTONDOWN =2, //按下右键CV_EVENT_MBUTTONDOWN =3, //按下中键CV_EVENT_LBUTTONUP =4, //放开左键CV_EVENT_RBUTTONUP =5, //放开右键CV_EVENT_MBUTTONUP =6, //放开中键CV_EVENT_LBUTTONDBLCLK =7, //左键双击CV_EVENT_RBUTTONDBLCLK =8, //右键双击CV_EVENT_MBUTTONDBLCLK =9, //中键双击CV_EVENT_MOUSEWHEEL =10, //滚轮滚动CV_EVENT_MOUSEHWHEEL =11 //横向滚轮滚动
flag事件有:
CV_EVENT_FLAG_LBUTTON =1, //左键拖拽CV_EVENT_FLAG_RBUTTON =2, //右键拖拽CV_EVENT_FLAG_MBUTTON =4, //中键拖拽CV_EVENT_FLAG_CTRLKEY =8, //按住CTRL拖拽CV_EVENT_FLAG_SHIFTKEY =16, //按住Shift拖拽CV_EVENT_FLAG_ALTKEY =32 //按住ALT拖拽
首先鼠标在图像上的操作有三个步骤:
1、按下左键,建立一个起点
if (event == CV_EVENT_LBUTTONDOWN) { //画起点 }
2、按下左键的同时,进行左键拖拽画线
else if (event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON)) { //在拖动过程中画线 }
3、放下左键,起点与当前点进行连接
else if (event == CV_EVENT_LBUTTONUP) { //在结束之后进行起点和终点的连接 }
二、对一系列点组成的封闭图形进行填充
采用多边形填充函数fillpoly 取一系列队的首地址,进行多边形的填充,vctPoint保存鼠标滑过的一系列的点
const cv::Point * ppt[1] = { &vctPoint[0] };//取数组的首地址 int len = vctPoint.size(); int npt[] = { len }; org.copyTo(maskImage); maskImage.setTo(cv::Scalar(0,0, 0, 0)); cv::fillPoly(maskImage, ppt, npt, 1, cv::Scalar(0,255, 255, 255));
完整的代码如下:
#include <iostream> #include <vector>#include <opencv.hpp>#include <opencv2/opencv.hpp>#include <opencv2/highgui/highgui.hpp> #include <opencv2/core/core.hpp> static std::vector<std::vector<cv::Point>> vctvctPoint;cv::Mat org = cv::imread("images/kaola.jpg");cv::Mat dst, maskImage;static std::vector<cv::Point> vctPoint;static cv::Point ptStart = (-1, -1); //初始化起点static cv::Point cur_pt = (-1, -1); //初始化临时节点char temp[16]; void on_mouse(int event, int x, int y, int flags, void *ustc)//event鼠标事件代号,x,y鼠标坐标,flags拖拽和键盘操作的代号 { if (event == CV_EVENT_LBUTTONDOWN) { std::cout << "x:" << x << " y:" << y << std::endl; ptStart = cv::Point(x, y); vctPoint.push_back(ptStart); cv::circle(org, ptStart, 1, cv::Scalar(255, 0, 255), CV_FILLED, CV_AA, 0); cv::imshow("图片", org); //cv::putText(tmp, temp, ptStart, cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0, 0), 1, 8); } else if (event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON)) { std::cout << "x:" << x << " y:" << y << std::endl; cur_pt = cv::Point(x, y); cv::line(org, vctPoint.back(), cur_pt, cv::Scalar(0, 255, 0, 0), 1, 8, 0); cv::circle(org, cur_pt, 1, cv::Scalar(255, 0, 255), CV_FILLED, CV_AA, 0); cv::imshow("图片", org); vctPoint.push_back(cur_pt); //cv::putText(tmp, temp, cur_pt, cv::FONT_HERSHEY_SIMPLEX, 0.5, cv::Scalar(0, 0, 0, 0)); } else if (event == CV_EVENT_LBUTTONUP) { std::cout << "x:" << x << " y:" << y << std::endl; cur_pt = cv::Point(x, y); cv::line(org, ptStart, cur_pt, cv::Scalar(0, 255, 0, 0), 1, 8, 0); cv::circle(org, cur_pt, 1, cv::Scalar(255, 0, 255), CV_FILLED, CV_AA, 0); cv::imshow("图片", org); vctPoint.push_back(cur_pt); vctvctPoint.push_back(vctPoint); //把点构成任意多边形进行填充 const cv::Point * ppt[1] = { &vctPoint[0] };//取数组的首地址 int len = vctPoint.size(); int npt[] = { len }; // cv::polylines(org, ppt, npt, 1, 1, cv::Scalar(0,0, 0, 0), 1, 8, 0); org.copyTo(maskImage); maskImage.setTo(cv::Scalar(0,0, 0, 0)); cv::fillPoly(maskImage, ppt, npt, 1, cv::Scalar(0,255, 255, 255)); org.copyTo(dst ,maskImage); cv::imshow("抠图",dst); cv::waitKey(0); }}int main(){ //鼠标点击 cv::namedWindow("图片");//定义一个img窗口 cv::setMouseCallback("图片", on_mouse, 0);//调用回调函数 cv::imshow("图片", org); cv::waitKey(0); return 0;}
新闻热点
疑难解答