-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
191 lines (147 loc) · 4.36 KB
/
main.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
#include "Enums.hpp"
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <opencv2/opencv.hpp>
#define DEBUG(x) do{ printf("DEBUG - %s\n", x); }while(false)
#define SOURCE 1
#define HSV 1
#define MASK 1
#define CONTOUR 1
#define RESULT 1
void process(std::string ipath, std::string opath)
{
cv::Mat src = cv::imread(ipath);
if(src.empty())
{
throw std::runtime_error("Cannot open image!");
}
///*
//DISPLAY NORMAL SOURCE
DEBUG("START NORMAL DISPLAY");
std::string normal_name = opath + "_src.jpg";
#if SOURCE
cv::imwrite(normal_name, src);
#endif
DEBUG("END NORMAL DISPLAY");
//*/
///*
//HSV CONVERT
DEBUG("START HSV CONVERT");
std::string hsv_name = opath + "_hsv.jpg";
cv::Mat hsv, hsv_display;
cv::cvtColor(src, hsv, cv::COLOR_BGR2HSV);
cv::cvtColor(hsv, hsv_display, cv::COLOR_HSV2BGR);
#if HSV
cv::imwrite(hsv_name, hsv_display);
#endif
DEBUG("END HSV CONVERT");
//*/
///*
//CUT OUT COLOURS
DEBUG("START COLOUR MASKING");
std::string masked_name = opath + "_mask.jpg";
cv::Mat r_mask_l, r_mask_h, b_mask_l, b_mask_h, y_mask_l, y_mask_h, mask, masked, masked_display;
cv::inRange(hsv, cv::Scalar(0, 120, 70), cv::Scalar(10, 255, 255), r_mask_l);
cv::inRange(hsv, cv::Scalar(170, 120, 70), cv::Scalar(180, 255, 255), r_mask_h);
//cv::inRange(hsv, cv::Scalar(20, 150, 0), cv::Scalar(30, 255, 255), b_mask_l);
cv::inRange(hsv, cv::Scalar(20, 150, 0), cv::Scalar(30, 255, 255), b_mask_h);
//cv::inRange(hsv, cv::Scalar(100, 100, 100), cv::Scalar(140, 255, 255), y_mask_l);
cv::inRange(hsv, cv::Scalar(100, 100, 100), cv::Scalar(140, 255, 255), y_mask_h);
mask = r_mask_l + r_mask_h /*+ b_mask_l*/ + b_mask_h /*+ y_mask_l*/ + y_mask_h;
cv::bitwise_and(hsv, hsv, masked, mask);
cv::cvtColor(masked, masked_display, cv::COLOR_HSV2BGR);
#if MASK
cv::imwrite(masked_name, masked_display);
#endif
DEBUG("END COLOUR MASKING");
//*/
///*
//FIND CONTOURS
DEBUG("START CONTOUR SEARCH");
std::string contour_name = opath + "_contours.jpg";
cv::Mat grayscale, binary, contour;
std::vector<std::vector<cv::Point>> contours;
cv::cvtColor(masked_display, grayscale, cv::COLOR_BGR2GRAY);
cv::threshold(grayscale, binary, 1, 255, cv::THRESH_BINARY);
cv::findContours(binary, contours, cv::RETR_LIST, cv::CHAIN_APPROX_NONE);
std::vector<int> ids;
double area = 0;
double img_area = src.rows*src.cols;
#if 1
std::cout << "Image Area: " << img_area << std::endl;
std::cout << "Lower bounds: " << 0.05*img_area << std::endl;
std::cout << "Upper bounds: " << 0.95*img_area << std::endl;
#endif
for(size_t i = 0; i < contours.size(); i++)
{
area = cv::contourArea(contours[i]);
#if 0
std::cout << "Area " << i << ": " << area << std::endl;
#endif
if(area > 0.007*img_area && area < 0.95*img_area)
{
ids.push_back(i);
}
}
std::vector<std::vector<cv::Point>> contours_poly;
for(size_t i = 0; i < ids.size(); i++)
{
std::vector<cv::Point> base;
contours_poly.push_back(base);
}
std::vector<cv::Rect> boundRect;
for(size_t i = 0; i < ids.size(); i++)
{
cv::approxPolyDP(contours[ids[i]], contours_poly[i], 3, true);
boundRect.push_back(boundingRect(contours_poly[i]));
}
contour = cv::Mat::zeros(binary.size(), CV_8UC3);
for(size_t i = 0; i < ids.size(); i++)
{
cv::rectangle(contour, boundRect[i].tl(), boundRect[i].br(), cv::Scalar(255, 255, 255), 2);
}
#if CONTOUR
cv::imwrite(contour_name, contour);
#endif
DEBUG("END CONTOUR SEARCH");
//*/
///*
//CROP TO CONTOUR
DEBUG("START CROP");
std::string crop_name = opath + "_crop";
std::vector<cv::Mat> cropped;
for(size_t i = 0; i < ids.size(); i++)
{
cropped.push_back(src(boundRect[i]));
}
#if RESULT
for(size_t i = 0; i < ids.size(); i++)
{
std::string i_name = crop_name + "_" + std::to_string(i) + ".jpg";
cv::imwrite(i_name, cropped[i]);
}
#endif
DEBUG("END CROP");
//*/
}
int main()
{
//PROCESS
std::string ipath = "./inputs/img_";
std::string opath = "./outputs/img_";
for(int i = 1; i < 516; i++)
{
std::cout << "Input: " << ipath + std::to_string(i) + ".jpg" << std::endl;
std::cout << "Output: " << opath + std::to_string(i) + "/out" << std::endl;
process(ipath + std::to_string(i) + ".jpg", opath + std::to_string(i) + "/out");
}
/*
ipath += std::to_string(0) + ".jpg";
opath += std::to_string(0);
std::cout << "Input: " << ipath << std::endl;
std::cout << "Output: " << opath << std::endl;
process(ipath, opath);
*/
return 0;
}