21 #include <hazard_detection/door_check.hpp>
22 #include <hazard_detection/Line.hpp>
26 int DoorCheck::process(
const std::string & fname, DoorCheckParams params ) {
27 cv::Mat img = cv::imread(fname, CV_LOAD_IMAGE_GRAYSCALE);
30 if (img.empty())
return -1;
34 cv::adaptiveThreshold(img, img_thr, 255, params.thr_method, cv::THRESH_BINARY_INV, params.thr_block, params.thr_c);
36 int prop_width = img.size().width;
37 int prop_height = img.size().height;
40 std::vector<cv::Vec4i> tmp_lines;
41 cv::HoughLinesP( img_thr, tmp_lines, 1, CV_PI/180, params.hough_thr, params.hough_len, params.hough_gap);
43 std::vector<Line> lines;
44 for(
size_t i = 0; i < tmp_lines.size(); i++ )
46 lines.push_back(Line(cv::Point(tmp_lines[i][0], tmp_lines[i][1]), cv::Point(tmp_lines[i][2], tmp_lines[i][3])));
52 std::vector<Line> lines_v, lines_h;
54 for (
int i = 0; i < lines.size(); ++i) {
57 line.draw(img, cv::Scalar(255,255,255));
58 if (abs(line.getAngle()) < M_PI/6) {
59 lines_h.push_back(line);
61 line.draw(img, cv::Scalar(128));
63 if (abs(line.getAngle()) > 2*M_PI/6) {
64 lines_v.push_back(line);
70 std::vector<cv::Point2f> points_v, points_h;
82 for (
int i = 0; i < lines_v.size(); ++i) {
83 Line * tmp = &lines_v[i];
86 float angle_score = fabs(tmp->getAngle()) / (M_PI/2);
89 float mean_x = (tmp->getP1().x + tmp->getP2().x) / 2;
90 float cx = 0.5 * prop_width;
91 float position_score = 1.0 - fabs(cx - mean_x) / cx;
94 float length_score = 2 * tmp->length() / prop_height;
95 if (length_score > 1) length_score = 1;
97 float tmp_score = angle_score * position_score * length_score;
98 if (tmp_score > cl_score) {
100 cl_score = tmp_score;
105 cl->draw(img, cv::Scalar(0, 0, 0));
113 for (
int i = 0; i < lines_h.size(); ++i) {
114 Line * tmp = &lines_h[i];
117 float angle_score = (M_PI/2 - fabs(tmp->getAngle())) / (M_PI/2);
120 float mean_x = (tmp->getP1().x + tmp->getP2().x) / 2;
121 float cx = 0.25 * prop_width;
122 float position_score = 1.0 - fabs(cx - mean_x) / cx;
124 if (mean_x > 0.5 * prop_width) position_score = 0;
127 float length_score = 2 * tmp->length() / prop_width;
128 if (length_score > 1) length_score = 1;
130 float tmp_score = angle_score * position_score * length_score;
131 if (tmp_score > ll_score) {
133 ll_score = tmp_score;
138 ll->draw(img, cv::Scalar(0));
146 for (
int i = 0; i < lines_h.size(); ++i) {
147 Line * tmp = &lines_h[i];
150 float angle_score = (M_PI/2 - fabs(tmp->getAngle())) / (M_PI/2);
153 float mean_x = (tmp->getP1().x + tmp->getP2().x) / 2;
154 float cx = 0.75 * prop_width;
155 float position_score = 1.0 - fabs(cx - mean_x) / cx;
157 if (mean_x < 0.5 * prop_width) position_score = 0;
160 float length_score = 2 * tmp->length() / prop_width;
161 if (length_score > 1) length_score = 1;
163 float tmp_score = angle_score * position_score * length_score;
164 if (tmp_score > rl_score) {
166 rl_score = tmp_score;
171 rl->draw(img, cv::Scalar(0));
175 angle = fabs(ll->getAngle() - rl->getAngle()) * 180 / 3.1415;
181 cv::imwrite(
"/tmp/door_out.png", img);