-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource1.cpp
More file actions
220 lines (207 loc) · 6.26 KB
/
Source1.cpp
File metadata and controls
220 lines (207 loc) · 6.26 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
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
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
#include <cmath>
#include <fstream>
using namespace cv;
using namespace std;
Mat src;
Mat different_Channels[3];
Mat r;
Mat src_hsv, src_gray, src_thresh;
Mat img_res;
Mat mask, mask1, mask2;
Mat glassier;
Mat canny_output;
Mat box_hsv;
Mat img_dill;
Mat masked_gray;
Scalar meanBrightness;
bool logic = false;
bool find_object = false;
vector< int > arr;
int kernel_size = 1;
int thresh = 10;
int max;
const int regionWidth = 40;
const int regionHeight = 40;
const float min_temp = 0.0f;
const float max_temp = 100.0f;
Point poly_points[1][4];
const Point* ppt[1] = { poly_points[0] };
int npt[] = { 4 };
bool My_callback();
float normalizeToTemperature(int gray, float min_temp, float max_temp) {
return (max_temp - min_temp) * (gray / 255.0) + min_temp;
}
bool Parall(Mat box) {
double res1 = double(box.at<int>(0, 1) - box.at<int>(1, 1)) / (box.at<int>(0, 0) - box.at<int>(1, 0));
double res2 = double(box.at<int>(2, 1) - box.at<int>(3, 1)) / (box.at<int>(2, 0) - box.at<int>(3, 0));
double res3 = double(box.at<int>(0, 1) - box.at<int>(3, 1)) / (box.at<int>(0, 0) - box.at<int>(3, 0));
double res4 = double(box.at<int>(1, 1) - box.at<int>(2, 1)) / (box.at<int>(1, 0) - box.at<int>(2, 0));
res1 = ceil(res1 * 10.0) / 10.0;
res2 = ceil(res2 * 10.0) / 10.0;
res3 = ceil(res3 * 10.0) / 10.0;
res4 = ceil(res4 * 10.0) / 10.0;
double varior_1 = ceil((res1 - res2) * 100.0) / 100.0;
double varior_2 = ceil((res3 - res4) * 100.0) / 100.0;
if (res1 == res2 && res3 == res4) {
return true;
}
else if (abs(varior_1) < 0.2 && abs(varior_2) < 0.2) {
return true;
}
else {
return false;
}
}
void Draw_on_sqr(Mat box) {
poly_points[0][0] = cv::Point(box.at<int>(0, 0), box.at<int>(0, 1));
poly_points[0][1] = cv::Point(box.at<int>(1, 0), box.at<int>(1, 1));
poly_points[0][2] = cv::Point(box.at<int>(2, 0), box.at<int>(2, 1));
poly_points[0][3] = cv::Point(box.at<int>(3, 0), box.at<int>(3, 1));
cv::polylines(img_res, ppt, npt, 1, true, cv::Scalar(0, 165, 255), 3);
}
int main()
{
//auto start = chrono::steady_clock::now();
VideoCapture cap(0);
if (!cap.isOpened())
{
cout << "Could not open video capture!" << endl;
return -1;
}
while (true)
{
// Ñ÷èòûâàåì êàäð ñ âèäåîêàìåðû
cap >> src;
// Ïðîâåðÿåì, óäàëîñü ëè ñ÷èòàòü êàäð
if (src.empty())
{
cout << "Could not read frame!" << endl;
break;
}
// Âûçûâàåì âàøó ôóíêöèþ My_callback() äëÿ îáðàáîòêè êàäðà
My_callback();
// Äëÿ âûõîäà èç öèêëà íàæìèòå êëàâèøó 'q'
if (waitKey(1) == 'q')
break;
}
//auto end = chrono::steady_clock::now();
//auto diff = end - start;
//cout << chrono::duration <double, milli>(diff).count() << " ms" << endl;
return 0;
}
bool My_callback()
{
img_res = src.clone();
cv::split(src, different_Channels);
r = different_Channels[2];
double red_mean = cv::mean(r)[0];
for (int i = 0; i < r.rows; i++) {
for (int j = 0; j < r.cols; j++) {
if (r.at<uchar>(i, j) < red_mean) {
r.at<uchar>(i, j) = 0;
}
}
}
cvtColor(src, src_gray, COLOR_BGR2GRAY);
cvtColor(src, src_hsv, COLOR_BGR2HLS);
meanBrightness = cv::mean(src_hsv)[1];
std::cout << "Average brightness: " << meanBrightness.val[0] << std::endl;
cv:inRange(src_hsv, Scalar(0, 50, 20), Scalar(5, 255, 255), mask1);
cv::inRange(src_hsv, Scalar(170, 50, 20), Scalar(180, 255, 255), mask2);
cv::bitwise_or(mask1, mask2, mask);
vector<vector<Point> > contours_hsv;
findContours(mask, contours_hsv, RETR_TREE, CHAIN_APPROX_SIMPLE);
for (size_t i = 0; i < contours_hsv.size(); i++)
{
cv::approxPolyDP(contours_hsv[i], box_hsv, 10, true);
int area = int(cv::contourArea(box_hsv));
if (area > 300) {
arr.push_back(area);
}
}
const int max = *std::max_element(arr.begin(), arr.end());
cout << "MAAAAAAAAAX" << max << endl;
int contours_size = 0;
while (logic == false) {
Canny(r, canny_output, thresh, thresh * 2);
vector<vector<Point> > contours;
Mat kernel = cv::getStructuringElement(MORPH_RECT, Size(kernel_size, kernel_size));
Mat box;
cv::dilate(canny_output, img_dill, kernel);
findContours(img_dill, contours, RETR_TREE, CHAIN_APPROX_SIMPLE);
contours_size += contours.size();
if (contours_size > 3000) {
if (meanBrightness.val[0] < 30) {
GaussianBlur(r, glassier, Size(5, 5), 0);
}
else {
GaussianBlur(r, glassier, Size(1, 1), 0);
}
Canny(glassier, canny_output, thresh, thresh * 2);
cv::dilate(canny_output, img_dill, kernel);
findContours(img_dill, contours, RETR_TREE, CHAIN_APPROX_SIMPLE);
}
vector<vector<Point>>hull(contours.size());
for (size_t i = 0; i < contours.size(); i++)
{
cv::convexHull(contours[i], hull[i]);
}
for (size_t i = 0; i < contours.size(); i++)
{
Scalar color = Scalar(0, 255, 0);
cv::approxPolyDP(hull[i], box, 10, true);
if (box.rows == 4)
{
bool res = Parall(box);
if (res) {
if (cv::contourArea(box) > max) {
logic = true;
find_object = true;
Draw_on_sqr(box);
Mat mask = Mat::zeros(src.rows, src.cols, CV_8UC1);
cv::fillPoly(mask, ppt, npt, 1, cv::Scalar(255));
src_gray.copyTo(masked_gray, mask);
std::ofstream csvfile;
csvfile.open("pixel_temperatures.csv");
csvfile << "X,Y,Temperature\n";
for (int y = 0; y < masked_gray.rows; y++) {
for (int x = 0; x < masked_gray.cols; x++) {
uchar grayValue = masked_gray.at<uchar>(y, x);
if (grayValue > 0) {
float temperature = normalizeToTemperature(grayValue, min_temp, max_temp);
csvfile << x << "," << y << "," << temperature << "\n";
}
}
}
csvfile.close();
}
}
}
cv::drawContours(src, hull, (int)i, color, 2);
}
thresh += 10;
if (kernel_size > 7) {
logic = true;
}
if (thresh > 100) {
kernel_size++;
thresh = 10;
cout << "KERNEL=========" << kernel_size << endl;
}
}
cv::imshow("Hull demo", src);
cv::imshow("Canny", canny_output);
cv::imshow("Result", img_res);
cv::imshow("Result-mask", mask);
cv::imshow("Red Channel", r);
if (find_object) {
return true;
}
else {
return false;
}
}