RAPP Platform  v0.6.0
RAPP Platform is a collection of ROS nodes and back-end processes that aim to deliver ready-to-use generic services to robots
 All Classes Namespaces Files Functions Variables Macros
light_check.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 Copyright 2015 RAPP
3 
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 
16  Authors: Maciej StefaƄczyk
17  contact: m.stefanczyk@elka.pw.edu.pl
18 
19 ******************************************************************************/
20 
21 #include <hazard_detection/light_check.hpp>
22 
23 int LightCheck::process( const std::string & fname, bool debug ) {
24  cv::Mat img = cv::imread(fname);
25 
26  if (img.empty()) return -1;
27 
28  std::vector<cv::Rect> rois;
29  std::vector<float> values;
30 
31  cv::Mat out = img.clone();
32 
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;
38 
39 
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));
48 
49  rois.push_back(centered_rect(2*sx, 2*sy, sl, sl));
50 
51  char buf[256];
52 
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);
57 
58  if (val < vmin) vmin = val;
59  if (val > vmax) vmax = val;
60 
61  vsum += val;
62 
63  if (debug) {
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);
67  } else {
68  cv::rectangle(out, rois[i], cv::Scalar(256, 0, 0), 3);
69  }
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;
72  } // if (debug)
73  }
74 
75  float center_val = values.back();
76  vsum = vsum - vmin - vmax - center_val;
77  float vavg = vsum / 6;
78  float res = center_val - vavg;
79  res = res * 4;
80  if (res < 0) res = 0;
81  if (res > 100) res = 100;
82 
83  if (debug) {
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";
88  }
89 
90  if (debug) {
91  out.copyTo(img);
92  cv::imshow("out", out);
93  cv::waitKey(10000);
94  }
95 
96  return res;
97 }
98 
99 
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);
102 }
103 
104 float LightCheck::average_light(cv::Mat img) {
105  return cv::mean(img)[0];
106 }