opencv/samples/cpp/create_mask.cpp

119 lines
2.7 KiB
C++
Raw Normal View History

/*
* create_mask.cpp
*
* Author:
* Siddharth Kherada <siddharthkherada27[at]gmail[dot]com>
*
* This tutorial demonstrates how to make mask image (black and white).
* The program takes as input a source image and outputs its corresponding
* mask image.
*/
#include "opencv2/imgproc.hpp"
2014-07-04 22:48:15 +08:00
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
using namespace std;
using namespace cv;
2018-07-22 18:32:43 +08:00
Mat src, img1, mask, final;
Point point;
2018-07-22 18:32:43 +08:00
vector<Point> pts;
int drag = 0;
int var = 0;
int flag = 0;
2013-09-14 19:36:26 +08:00
void mouseHandler(int, int, int, int, void*);
void mouseHandler(int event, int x, int y, int, void*)
{
if (event == EVENT_LBUTTONDOWN && !drag)
{
2018-07-22 18:32:43 +08:00
if (flag == 0)
{
2018-07-22 18:32:43 +08:00
if (var == 0)
img1 = src.clone();
point = Point(x, y);
2018-07-22 18:32:43 +08:00
circle(img1, point, 2, Scalar(0, 0, 255), -1, 8, 0);
pts.push_back(point);
var++;
drag = 1;
2018-07-22 18:32:43 +08:00
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;
}
2018-07-22 18:32:43 +08:00
if (event == EVENT_RBUTTONDOWN)
{
2018-07-22 18:32:43 +08:00
flag = 1;
img1 = src.clone();
2018-07-22 18:32:43 +08:00
if (var != 0)
{
2018-07-22 18:32:43 +08:00
polylines( img1, pts, 1, Scalar(0,0,0), 2, 8, 0);
}
imshow("Source", img1);
}
if (event == EVENT_RBUTTONUP)
{
flag = var;
2018-07-22 18:32:43 +08:00
final = Mat::zeros(src.size(), CV_8UC3);
mask = Mat::zeros(src.size(), CV_8UC1);
2020-07-22 12:29:56 +08:00
fillPoly(mask, pts, Scalar(255, 255, 255), 8, 0);
2018-07-22 18:32:43 +08:00
bitwise_and(src, src, final, mask);
imshow("Mask", mask);
imshow("Result", final);
imshow("Source", img1);
}
2018-07-22 18:32:43 +08:00
if (event == EVENT_MBUTTONDOWN)
{
2018-07-22 18:32:43 +08:00
pts.clear();
var = 0;
drag = 0;
2018-07-22 18:32:43 +08:00
flag = 0;
imshow("Source", src);
}
}
int main(int argc, char **argv)
{
2018-10-31 20:48:56 +08:00
CommandLineParser parser(argc, argv, "{@input | lena.jpg | input image}");
2018-07-22 18:32:43 +08:00
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");
2018-10-31 20:48:56 +08:00
src = imread(samples::findFile(input_image));
2018-07-22 18:32:43 +08:00
if (src.empty())
2018-10-31 20:48:56 +08:00
{
2018-07-22 18:32:43 +08:00
printf("Error opening image: %s\n", input_image.c_str());
return 0;
2018-10-31 20:48:56 +08:00
}
2018-07-22 18:32:43 +08:00
namedWindow("Source", WINDOW_AUTOSIZE);
setMouseCallback("Source", mouseHandler, NULL);
2018-07-22 18:32:43 +08:00
imshow("Source", src);
waitKey(0);
2017-07-26 13:39:53 +08:00
return 0;
}