Loading...
Searching...
No Matches
samples/cpp/create_mask.cpp
This program demonstrates using mouse events and how to make and use a mask image (black and white) .
/*
* 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"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
using namespace std;
using namespace cv;
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)
{
}
imshow("Source", img1);
}
if (event == EVENT_RBUTTONUP)
{
flag = var;
bitwise_and(src, src, final, mask);
imshow("Mask", mask);
imshow("Source", img1);
}
if (event == EVENT_MBUTTONDOWN)
{
pts.clear();
var = 0;
drag = 0;
flag = 0;
imshow("Source", src);
}
}
int main(int argc, char **argv)
{
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";
src = imread(samples::findFile(input_image));
if (src.empty())
{
printf("Error opening image: %s\n", input_image.c_str());
return 0;
}
namedWindow("Source", WINDOW_AUTOSIZE);
setMouseCallback("Source", mouseHandler, NULL);
imshow("Source", src);
waitKey(0);
return 0;
}
CV_NODISCARD_STD Mat clone() const
Creates a full copy of the array and the underlying data.
void bitwise_and(InputArray src1, InputArray src2, OutputArray dst, InputArray mask=noArray())
computes bitwise conjunction of the two arrays (dst = src1 & src2) Calculates the per-element bit-wis...
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
void setMouseCallback(const String &winname, MouseCallback onMouse, void *userdata=0)
Sets mouse handler for the specified window.
CV_EXPORTS_W Mat imread(const String &filename, int flags=IMREAD_COLOR)
Loads an image from a file.
void fillPoly(InputOutputArray img, InputArrayOfArrays pts, const Scalar &color, int lineType=LINE_8, int shift=0, Point offset=Point())
Fills the area bounded by one or more polygons.
void polylines(InputOutputArray img, InputArrayOfArrays pts, bool isClosed, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws several polygonal curves.
"black box" representation of the file storage associated with a file on disk.
Definition: core.hpp:106
STL namespace.