-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSourceMaster.cpp
280 lines (238 loc) · 7.88 KB
/
SourceMaster.cpp
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include<iostream>
#include<opencv2/opencv.hpp>
#include<opencv2/imgproc.hpp>
using namespace std;
using namespace cv;
int findMaxContourArea(vector<vector<Point> > contours);
Mat createPoints(Mat img);
Mat drawRectangle(Rect r, Mat img);
bool checkDensity(Mat img, Point p);
void contourCreation(Mat img);
int threshold_value = 220;
int threshold_type = 0; //binary threshold
int const max_BINARY_value = 255;
Mat img, imgSegmented, frame;
int main()
{
//for working with images
Mat img = imread("inputImg1.png");
cvtColor(img, imgSegmented, CV_BGR2GRAY);
//cvtColor(frame, img, CV_BGR2HSV);
//inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgSegmented);
//filter
//Canny(frame, cannyOp, 30, 90, 3); //0,100 //30,60 //100,200 //0,176
//GaussianBlur(imgSegmented, imgSegmented, Size(7, 7), 1.5, 1.5); //used to blur the image/frame
dilate(imgSegmented, imgSegmented, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
erode(imgSegmented, imgSegmented, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
threshold(imgSegmented, imgSegmented, threshold_value, max_BINARY_value, threshold_type);
imshow("check", imgSegmented);
Scalar intensity;
while (1)
{
contourCreation(imgSegmented);
waitKey(25);
}
//for working with webcam
/*VideoCapture cap = VideoCapture(0);
int slider = 0;
int slider_max = 255;
namedWindow("Control", 1);
//namedWindow("CannyControl", 2);
int iLowH = 0;
int iHighH = 179;
int iLowS = 0;
int iHighS = 255;
int iLowV = 0;
int iHighV = 255;
//Create trackbars for controlliing Hue Saturation Value
cvCreateTrackbar("LowH", "Control", &iLowH, 255);
cvCreateTrackbar("HighH", "Control", &iHighH, 255);
cvCreateTrackbar("LowS", "Control", &iLowS, 255);
cvCreateTrackbar("HighS", "Control", &iHighS, 255);
cvCreateTrackbar("LowV", "Control", &iLowV, 255);
cvCreateTrackbar("HighV", "Control", &iHighV, 255);
for (;;)
{
cap >> frame;
cap.read(frame);
//frame = imread("hand.png");
Mat imgHSV;
//Mat imgOp;
cvtColor(frame, imgHSV, CV_BGR2HSV);
inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgSegmented);
//filter
//Canny(frame, cannyOp, 30, 90, 3); //0,100 //30,60 //100,200 //0,176
//GaussianBlur(imgSegmented, imgSegmented, Size(7, 7), 1.5, 1.5); //used to blur the image/frame
dilate(imgSegmented, imgSegmented, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
erode(imgSegmented, imgSegmented, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)));
threshold(imgSegmented, img, threshold_value, max_BINARY_value, threshold_type);
//imshow("canny op", cannyOp);
imshow("Original", frame);
//imshow("Output", img);
imshow("Segmentation", img);
//img = createPoints(img);
contourCreation(img);
//imshow("Line", img);
waitKey(25);
}*/
}
Mat createPoints(Mat img)
{
//4 points to be found.
/*
1) top -> i, j should be lowest
2) side_L -> j lowest
3) side_R -> j highest
4) bottom -> img.rows
*/
Point top, bottom;
Scalar intensity;
Point side_R[2];
Point side_L[2];
int count = 0;
side_L[0] = Point(img.cols, img.rows);
for (long i = 0; i < img.rows; i++)
{
for (long j = 0; j < img.cols; j++)
{
intensity = img.at<uchar>(Point(j, i));
if (intensity.val[0] > 200)
{
side_R[1] = Point(j, i);
side_L[1] = Point(j, i);
if (count == 0)
{
if (checkDensity(img, Point(j, i)))
{
top = Point(j, i);
count++;
//cout << top <<"top\n";
bottom = Point(j, img.rows);
//cout << bottom;
}
}
if (side_R[1].x > side_R[0].x)
{
side_R[0] = side_R[1];
//cout << " R" << side_R[0];
}
if (side_L[1].x < side_L[0].x)
{
//cout << "inside";
side_L[0] = side_L[1];
//cout << " L" << side_L[0] << "\n";
}
}
}
}
//rectangle width
int width = side_R[0].x - side_L[0].x;
//rectangle height
int height = img.rows - top.y;
//to find co-ordinate of vertex
Point vertex = Point(side_L[0].x, top.y);
//cout << vertex << "Vertex";
Rect r = Rect(vertex.x, vertex.y, width, height);
return drawRectangle(r, img);
}
Mat drawRectangle(Rect r, Mat img)
{
rectangle(img, r, Scalar(255, 0, 0), 1, 8, 0);
//imshow("Rectangle", img);
//crop image based on roi
Mat crop = img(r);
//imshow("Cropped image", crop);
return crop;
}
bool checkDensity(Mat img, Point p)
{
Scalar checkIntensityV, checkIntensityHL, checkIntensityHR;
for (int i = 0; i < 5; i++)
{
checkIntensityV = img.at<uchar>(Point(p.x, (p.y + i)));
checkIntensityHL = img.at<uchar>(Point(p.x - 5 + i, p.y + i + 5));
checkIntensityHR = img.at<uchar>(Point(p.x + 5 - i, p.y + i + 5));
if (checkIntensityV.val[0] > 200 && checkIntensityHL.val[0] > 200 && checkIntensityHR.val[0] > 200)
{
continue;
}
else
return false;
return true;
}
//cout << p.x << " " << p.y;
}
void contourCreation(Mat img)
{
//cout << "inside contour image" << endl;
std::vector<std::vector<cv::Point> > contours;
//Mat contourImage;
findContours(img, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, Point(0, 0));
vector<vector<int> > hull(contours.size());
vector<vector<Point> >hullPoint(contours.size());
Mat drawing = Mat::zeros(img.rows, img.cols, CV_8UC1);
Mat drawingContours = Mat::zeros(img.rows, img.cols, CV_8UC1);
int maxContourAreaLoc = findMaxContourArea(contours);
//cout << maxContourAreaLoc << " maxContourAreaLox" << endl;
drawContours(drawing, contours, maxContourAreaLoc, Scalar(255, 255, 255), CV_FILLED);
//drawing = createPoints(drawing);
std::vector<std::vector<cv::Point> > contoursDrawing;
vector<Rect> boundRect(contoursDrawing.size());
drawingContours = drawing.clone();
findContours(drawingContours, contoursDrawing, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, Point(0, 0));
//cout << contoursDrawing.size()<< " contours size"<<endl;
//drawContours(drawingContours, contoursDrawing, 0, Scalar(255, 255, 255));
//not working properly
/*for (long i = drawingContours.rows; i > 10; i--)
{
//cout << contoursDrawing.size() << " contoursSize" << endl;
line(drawingContours, Point(0, i), Point(drawingContours.cols, i), Scalar(0,0,0), 1, 8, 0);
if (contoursDrawing.size() == 4)
{
cout << "contour size " << contoursDrawing.size();
//cout << "Greater than 4" <<endl;
for (size_t j = 0; j < contoursDrawing.size(); j++)
{
//drawContours(drawing, contours, j, Scalar(255, 255, 255));
boundRect[i] = boundingRect(contours[i]);
rectangle(drawingContours, boundRect[i].tl(), boundRect[i].br(), Scalar(0, 0, 255), 2);
}
break;
}
}*/
int i = drawingContours.rows;
while (contoursDrawing.size() < 4 && i >0)
{
cout << "Drawing line\t" << contoursDrawing.size()<<endl;
line(drawingContours, Point(0, i), Point(drawingContours.cols, i), Scalar(0, 0, 0), 1, 8, 0);
findContours(drawingContours, contoursDrawing, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, Point(0, 0));
i--;
}
if (contoursDrawing.size() == 4)
{
cout << contoursDrawing.size() << " contoursDrawing.size()" << endl;
for (size_t i = 0; i < contoursDrawing.size(); i++)
{
drawContours(drawingContours, contoursDrawing, i, Scalar(255, 255, 255));
boundRect[i] = boundingRect(contours[i]);
rectangle(drawingContours, boundRect[i].tl(), boundRect[i].br(), Scalar(0, 0, 255), 2);
}
}
imshow("DrawingContours", drawingContours);
imshow("contours", drawing);
//cout << contours.size() << "contour Size";
}
int findMaxContourArea(vector<vector<Point> > contours)
{
int maxContourAreaLoc = 0;
double maxContourArea = 0;
for (size_t i = 0; i < contours.size(); i++)
{
if (contourArea(contours[i],false) > maxContourArea)
{
maxContourArea = contourArea(contours[i], false);
maxContourAreaLoc = i;
}
}
return maxContourAreaLoc;
}