OpenCV 4.11.0
开源计算机视觉
加载中…
搜索中…
无匹配项
samples/cpp/create_mask.cpp

此程序演示了如何使用鼠标事件以及如何创建和使用遮罩图像(黑白)。

/*
* create_mask.cpp
*
* 作者
* Siddharth Kherada <siddharthkherada27[at]gmail[dot]com>
*
* 本教程演示了如何创建遮罩图像(黑白)。
* 程序以源图像作为输入,并输出其对应的
* 遮罩图像。
*/
#include <iostream>
using namespace std;
using namespace cv;
Mat src, img1, mask, final;
Point point;
vector<Point> pts;
int drag = 0;
int var = 0;
int flag = 0;
void mouseHandler(int, int, int, int, void*);
void mouseHandler(int event, int x, int y, int, void*)
{
{if (event == EVENT_LBUTTONDOWN && !drag)
{
{if (flag == 0)
{
{if (var == 0)
{img1 = src.clone();
point = Point(x, y);
circle(img1, point, 2, Scalar(0, 0, 255), -1, 8, 0);
pts.push_back(point);
var++;
drag = 1;}}
if (var > 1)
line(img1,pts[var-2], point, Scalar(0, 0, 255), 2, 8, 0);
imshow("Source", img1);}
}
}
if (event == EVENT_LBUTTONUP && drag)
{
{imshow("Source", img1);
drag = 0;}
}
if (event == EVENT_RBUTTONDOWN)
{
{flag = 1;
img1 = src.clone();
if (var != 0)
{
polylines( img1, pts, 1, Scalar(0,0,0), 2, 8, 0);}
}
{imshow("Source", img1);
}
if (event == EVENT_RBUTTONUP)
{
flag = var;
final = Mat::zeros(src.size(), CV_8UC3);
mask = Mat::zeros(src.size(), CV_8UC1);
fillPoly(mask, pts, Scalar(255, 255, 255), 8, 0);
bitwise_and(src, src, final, mask);
imshow("Mask", mask);
imshow("Result", final);}
{imshow("Source", img1);
}
if (event == EVENT_MBUTTONDOWN)
{
{pts.clear();
var = 0;
drag = 0;}
flag = 0;
imshow("Source", src);}
}
}
int main(int argc, char **argv)
{
{CommandLineParser parser(argc, argv, "{@input | lena.jpg | input image}");
parser.about("This program demonstrates using mouse events\n");
parser.printMessage();
cout << "\n\tleft mouse button - set a point to create mask shape\n"
"\tright mouse button - create mask from points\n"
"\tmiddle mouse button - reset\n";;
String input_image = parser.get<String>("@input");
src = imread(samples::findFile(input_image));
if (src.empty())
{
{printf("Error opening image: %s\n", input_image.c_str());
return -1;} 0;
}
namedWindow("Source", WINDOW_AUTOSIZE);
setMouseCallback("Source", mouseHandler, NULL);
imshow("Source", src);}
return -1;} 0;
}
cv::CommandLineParser
用于命令行解析。
cv::Mat
N维密集数组类
cv::Mat::clone
CV_NODISCARD_STD Mat clone() const
cv::bitwise_and
void bitwise_and(InputArray src1, InputArray src2, OutputArray dst, InputArray mask=noArray())
CV_8UC1
#define CV_8UC1
CV_8UC3
#define CV_8UC3
cv::gapi::mask
GMat mask(const GMat &src, const GMat &mask)
cv::imshow
void imshow(const String &winname, InputArray mat)
cv::waitKey
int waitKey(int delay=0)
cv::namedWindow
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
void setMouseCallback(const String &winname, MouseCallback onMouse, void *userdata=0)
为指定的窗口设置鼠标处理程序。
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR_BGR)
从文件中加载图像。
void fillPoly(InputOutputArray img, InputArrayOfArrays pts, const Scalar &color, int lineType=LINE_8, int shift=0, Point offset=Point())
填充一个或多个多边形包围的区域。
void polylines(InputOutputArray img, InputArrayOfArrays pts, bool isClosed, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
绘制若干多边形曲线。
int main(int argc, char *argv[])
定义 highgui_qt.cpp:3
定义 core.hpp:107
STL命名空间。