21 #include <hazard_detection/light_check.hpp>
23 int LightCheck::process(
const std::string & fname,
bool debug ) {
24 cv::Mat img = cv::imread(fname);
26 if (img.empty())
return -1;
28 std::vector<cv::Rect> rois;
29 std::vector<float> values;
31 cv::Mat out = img.clone();
33 int sx = img.size().width / 4;
34 int sy = img.size().height / 4;
35 int ss = img.size().height / 10;
36 int sm = img.size().height / 6;
37 int sl = img.size().height / 4;
40 rois.push_back(centered_rect(sx, sy, ss, ss));
41 rois.push_back(centered_rect(sx, 2*sy, sm, sm));
42 rois.push_back(centered_rect(sx, 3*sy, ss, ss));
43 rois.push_back(centered_rect(2*sx, sy, sm, sm));
44 rois.push_back(centered_rect(2*sx, 3*sy, sm, sm));
45 rois.push_back(centered_rect(3*sx, sy, ss, ss));
46 rois.push_back(centered_rect(3*sx, 2*sy, sm, sm));
47 rois.push_back(centered_rect(3*sx, 3*sy, ss, ss));
49 rois.push_back(centered_rect(2*sx, 2*sy, sl, sl));
53 float vmax = 0, vmin = 255, vsum = 0;
54 for (
int i = 0; i < rois.size(); ++i) {
55 float val = average_light(img(rois[i]));
56 values.push_back(val);
58 if (val < vmin) vmin = val;
59 if (val > vmax) vmax = val;
64 sprintf(buf,
"%6.0f", values[i]);
65 if (i < rois.size() - 1) {
66 cv::rectangle(out, rois[i], cv::Scalar(0, 0, 256), 3);
68 cv::rectangle(out, rois[i], cv::Scalar(256, 0, 0), 3);
70 cv::putText(out, buf, cv::Point(rois[i].x + 5, rois[i].y + rois[i].height - 5), cv::FONT_HERSHEY_PLAIN, 2, cv::Scalar(255, 255, 255));
71 std::cout << values[i] << std::endl;
75 float center_val = values.back();
76 vsum = vsum - vmin - vmax - center_val;
77 float vavg = vsum / 6;
78 float res = center_val - vavg;
81 if (res > 100) res = 100;
84 std::cout <<
"C: " << center_val <<
"\n";
85 std::cout <<
"m: " << vmin <<
"\n";
86 std::cout <<
"M: " << vmax <<
"\n";
87 std::cout <<
"s: " << vsum <<
"\n";
92 cv::imshow(
"out", out);
100 cv::Rect LightCheck::centered_rect(
int x,
int y,
int w,
int h) {
101 return cv::Rect(x-w/2, y-h/2, w, h);
104 float LightCheck::average_light(cv::Mat img) {
105 return cv::mean(img)[0];