Commit db858570 authored by Suleyman TURKMEN's avatar Suleyman TURKMEN

Update create_mask.cpp

parent 28d0e97c
......@@ -12,26 +12,18 @@
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/core.hpp"
#include <iostream>
#include <stdlib.h>
using namespace std;
using namespace cv;
Mat img0, img1, res1, final;
Mat src, img1, mask, final;
Point point;
vector<Point> pts;
int drag = 0;
int numpts = 100;
Point* pts = new Point[100];
int var = 0;
int flag = 0;
int flag1 = 0;
int minx,miny,maxx,maxy,lenx,leny;
void mouseHandler(int, int, int, int, void*);
......@@ -40,16 +32,17 @@ void mouseHandler(int event, int x, int y, int, void*)
if (event == EVENT_LBUTTONDOWN && !drag)
{
if(flag1 == 0)
if (flag == 0)
{
if(var==0)
img1 = img0.clone();
if (var == 0)
img1 = src.clone();
point = Point(x, y);
circle(img1,point,2,Scalar(0, 0, 255),-1, 8, 0);
pts[var] = point;
circle(img1, point, 2, Scalar(0, 0, 255), -1, 8, 0);
pts.push_back(point);
var++;
drag = 1;
if(var>1)
if (var > 1)
line(img1,pts[var-2], point, Scalar(0, 0, 255), 2, 8, 0);
imshow("Source", img1);
......@@ -59,103 +52,68 @@ void mouseHandler(int event, int x, int y, int, void*)
if (event == EVENT_LBUTTONUP && drag)
{
imshow("Source", img1);
drag = 0;
}
if (event == EVENT_RBUTTONDOWN)
{
flag1 = 1;
img1 = img0.clone();
for(int i = var; i < numpts ; i++)
pts[i] = point;
flag = 1;
img1 = src.clone();
if(var!=0)
if (var != 0)
{
const Point* pts3[1] = {&pts[0]};
polylines( img1, pts3, &numpts,1, 1, Scalar(0,0,0), 2, 8, 0);
polylines( img1, pts, 1, Scalar(0,0,0), 2, 8, 0);
}
for(int i=0;i<var;i++)
{
minx = min(minx,pts[i].x);
maxx = max(maxx,pts[i].x);
miny = min(miny,pts[i].y);
maxy = max(maxy,pts[i].y);
}
lenx = maxx - minx;
leny = maxy - miny;
imshow("Source", img1);
}
if (event == EVENT_RBUTTONUP)
{
flag = var;
final = Mat::zeros(img0.size(),CV_8UC3);
res1 = Mat::zeros(img0.size(),CV_8UC1);
const Point* pts4[1] = {&pts[0]};
fillPoly(res1, pts4,&numpts, 1, Scalar(255, 255, 255), 8, 0);
bitwise_and(img0, img0, final,res1);
imshow("mask",res1);
imwrite("mask.png",res1);
final = Mat::zeros(src.size(), CV_8UC3);
mask = Mat::zeros(src.size(), CV_8UC1);
vector<vector<Point> > vpts;
vpts.push_back(pts);
fillPoly(mask, vpts, 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)
{
for(int i = 0; i < numpts ; i++)
{
pts[i].x=0;
pts[i].y=0;
}
pts.clear();
var = 0;
flag1 = 0;
minx = INT_MAX; miny = INT_MAX; maxx = INT_MIN; maxy = INT_MIN;
imshow("Source", img0);
drag = 0;
flag = 0;
imshow("Source", src);
}
}
static void help()
{
cout << "\nThis program demonstrates using mouse events"
"\nCall:\n"
"./create_mask <image_name>\n"
"\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" << endl;
}
int main(int argc, char **argv)
{
cv::CommandLineParser parser(argc, argv, "{@input | ../data/lena.jpg | input image}");
help();
string input_image = parser.get<string>("@input");
if (input_image.empty())
{
parser.printMessage();
parser.printErrors();
return 0;
}
Mat src = imread(input_image);
minx = INT_MAX; miny = INT_MAX; maxx = INT_MIN; maxy = INT_MIN;
CommandLineParser parser(argc, argv, "{@input | ../data/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");
img0 = src;
src = imread(input_image);
res1 = Mat::zeros(img0.size(),CV_8UC1);
final = Mat::zeros(img0.size(),CV_8UC3);
//////////// source image ///////////////////
if (src.empty())
{
printf("Error opening image: %s\n", input_image.c_str());
return 0;
}
namedWindow("Source", 1);
namedWindow("Source", WINDOW_AUTOSIZE);
setMouseCallback("Source", mouseHandler, NULL);
imshow("Source", img0);
imshow("Source", src);
waitKey(0);
return 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment