-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrop_using_mc.cpp
More file actions
189 lines (136 loc) · 4.49 KB
/
Copy pathcrop_using_mc.cpp
File metadata and controls
189 lines (136 loc) · 4.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
bool leftDown = false, leftup = false;
Mat img;
Point cor1,cor2; //point is a class in opencv(or c++, idk), with the fields int x and int y
Rect box; //Rect is a predefined class in opencv(orr c++, idk), with the fields, int x, int y, int width, int height
void mouse_call(int event,int x,int y,int,void*);
int main()
{
img = imread("disha.jpeg");
namedWindow("Original");
imshow("Original",img);
//set the call back function for any mouse event
setMouseCallback("Original",mouse_call); //setting the mouse callback for selecting the region with mouse
while(char(waitKey(1)!='q')) //waiting for the 'q' key to finish the execution
{}
return 0;
}
void mouse_call(int event,int x,int y,int,void*)
{
if(event == EVENT_LBUTTONDOWN)
{
leftDown = true;
cor1.x=x;
cor1.y=y;
cout <<"Corner 1: "<<cor1<<endl;
}
if(event==EVENT_LBUTTONUP)
{
if(abs(x-cor1.x)>20 && abs(y-cor1.y)>20) //checking whether the region is too small
{
leftup = true;
cor2.x=x;
cor2.y=y;
cout<<"Corner 2: "<<cor2<<endl;
}
else
{
cout<<"Select a region more than 20 pixels"<<endl;
}
}
if(leftDown==true && leftup==false) //when the left button is down
{
Point pt;
pt.x=x;
pt.y=y;
Mat temp_img = img.clone();
rectangle(temp_img,cor1,pt,Scalar(0,0,255)); //drawing a rectangle continuously
imshow("Original",temp_img);
}
if(leftDown==true && leftup==true) //when the selection is done
{
box.width = abs(cor1.x-cor2.x);
box.height = abs(cor1.y-cor2.y);
box.x = min(cor1.x,cor2.x);
box.y = min(cor1.y,cor2.y);
Mat crop(img,box); //Selecting a ROI(region of interest) from the original pic
namedWindow("Cropped Image");
imshow("Cropped Image",crop); //showing the cropped image
leftDown=false;
leftup=false;
}
}
*/
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
bool leftDown = false, leftup = false;
Mat img;
Point cor1,cor2; //point is a class in opencv(or c++, idk), with the fields int x and int y
Rect box; //Rect is a predefined class in opencv(orr c++, idk), with the fields, int x, int y, int width, int height
void mouse_call(int event,int x,int y,int,void*);
int main()
{
img = imread("disha.jpeg");
namedWindow("Original");
imshow("Original",img);
//set the call back function for any mouse event
setMouseCallback("Original",mouse_call); //setting the mouse callback for selecting the region with mouse
while(char(waitKey(1)!='q')) //waiting for the 'q' key to finish the execution
{}
return 0;
}
void mouse_call(int event,int x,int y,int,void*)
{
if(event == EVENT_LBUTTONDOWN)
{
leftDown = true;
cor1.x=x;
cor1.y=y;
cout <<"Corner 1: "<<cor1<<endl;
}
if(event==EVENT_LBUTTONUP)
{
if(abs(x-cor1.x)>20 && abs(y-cor1.y)>20) //checking whether the region is too small
{
leftup = true;
cor2.x=x;
cor2.y=y;
cout<<"Corner 2: "<<cor2<<endl;
}
else
{
cout<<"Select a region more than 20 pixels"<<endl;
}
}
if(leftDown==true && leftup==false) //when the left button is down
{
Point pt;
pt.x=x;
pt.y=y;
Mat temp_img = img.clone();
rectangle(temp_img,cor1,pt,Scalar(0,0,255)); //drawing a rectangle continuously
imshow("Original",temp_img);
}
if(leftDown==true && leftup==true) //when the selection is done
{
box.width = abs(cor1.x-cor2.x);
box.height = abs(cor1.y-cor2.y);
box.x = min(cor1.x,cor2.x);
box.y = min(cor1.y,cor2.y);
Mat crop(img,box); //Selecting a ROI(region of interest) from the original pic
namedWindow("Cropped Image");
imshow("Cropped Image",crop); //showing the cropped image
leftDown=false;
leftup=false;
}
}