29 #ifndef _XF_OCV_REF_HPP_
30 #define _XF_OCV_REF_HPP_
41 if ((maxval > t1[2]) && (maxval > t2[1]) && (maxval > t2[2]) && (maxval > t2[3]) && (maxval > m1[0]) &&
42 (maxval > m1[1]) && (maxval > m1[3]) && (maxval > m1[4]) && (maxval > b1[1]) && (maxval > b1[2]) &&
43 (maxval > b1[3]) && (maxval > b2[2]))
58 for (j = 0; j < src.cols; j++) {
60 for (k = 0; k < 5; k++) {
63 for (k = 0; k < 5; k++) {
66 for (k = 0, ii = i, jj = j - 2; k < 5; k++, jj++) {
68 m1[k] = src.at<
NMSTYPE>(ii, jj);
72 for (k = 0, ii = i + 1, jj = j - 2; k < 5; k++, jj++) {
74 b1[k] = src.at<
NMSTYPE>(ii, jj);
78 for (k = 0, ii = i + 2, jj = j - 2; k < 5; k++, jj++) {
80 b2[k] = src.at<
NMSTYPE>(ii, jj);
84 }
else if ((j >= 2) && (j < src.cols - 2)) {
85 for (k = 0, ii = i - 2, jj = j - 2; k < 5; k++, jj++) {
88 for (k = 0, ii = i - 1, jj = j - 2; k < 5; k++, jj++) {
91 for (k = 0, ii = i, jj = j - 2; k < 5; k++, jj++) {
92 m1[k] = src.at<
NMSTYPE>(ii, jj);
94 for (k = 0, ii = i + 1, jj = j - 2; k < 5; k++, jj++) {
95 b1[k] = src.at<
NMSTYPE>(ii, jj);
97 for (k = 0, ii = i + 2, jj = j - 2; k < 5; k++, jj++) {
98 b2[k] = src.at<
NMSTYPE>(ii, jj);
100 }
else if (j >= src.cols - 2) {
101 for (k = 0, ii = i - 2, jj = j - 2; k < 5; k++, jj++) {
104 for (k = 0, ii = i - 1, jj = j - 2; k < 5; k++, jj++) {
107 for (k = 0, ii = i, jj = j - 2; k < 5; k++, jj++) {
109 m1[k] = src.at<
NMSTYPE>(ii, jj);
113 for (k = 0, ii = i + 1, jj = j - 2; k < 5; k++, jj++) {
115 b1[k] = src.at<
NMSTYPE>(ii, jj);
119 for (k = 0, ii = i + 2, jj = j - 2; k < 5; k++, jj++) {
121 b2[k] = src.at<
NMSTYPE>(ii, jj);
127 dst.at<uchar>(i, j) =
result ? 255 : 0;
131 for (j = 0; j < src.cols; j++) {
133 for (k = 0; k < 5; k++) {
136 for (k = 0, ii = i - 1, jj = j - 2; k < 5; k++, jj++) {
138 t2[k] = src.at<
NMSTYPE>(ii, jj);
142 for (k = 0, ii = i, jj = j - 2; k < 5; k++, jj++) {
144 m1[k] = src.at<
NMSTYPE>(ii, jj);
148 for (k = 0, ii = i + 1, jj = j - 2; k < 5; k++, jj++) {
150 b1[k] = src.at<
NMSTYPE>(ii, jj);
154 for (k = 0, ii = i + 2, jj = j - 2; k < 5; k++, jj++) {
156 b2[k] = src.at<
NMSTYPE>(ii, jj);
160 }
else if ((j >= 2) && (j < src.cols - 2)) {
161 for (k = 0, ii = i - 2, jj = j - 2; k < 5; k++, jj++) {
164 for (k = 0, ii = i - 1, jj = j - 2; k < 5; k++, jj++) {
165 t2[k] = src.at<
NMSTYPE>(ii, jj);
167 for (k = 0, ii = i, jj = j - 2; k < 5; k++, jj++) {
168 m1[k] = src.at<
NMSTYPE>(ii, jj);
170 for (k = 0, ii = i + 1, jj = j - 2; k < 5; k++, jj++) {
171 b1[k] = src.at<
NMSTYPE>(ii, jj);
173 for (k = 0, ii = i + 2, jj = j - 2; k < 5; k++, jj++) {
174 b2[k] = src.at<
NMSTYPE>(ii, jj);
176 }
else if (j >= src.cols - 2) {
177 for (k = 0, ii = i - 2, jj = j - 2; k < 5; k++, jj++) {
180 for (k = 0, ii = i - 1, jj = j - 2; k < 5; k++, jj++) {
183 for (k = 0, ii = i, jj = j - 2; k < 5; k++, jj++) {
185 m1[k] = src.at<
NMSTYPE>(ii, jj);
189 for (k = 0, ii = i + 1, jj = j - 2; k < 5; k++, jj++) {
191 b1[k] = src.at<
NMSTYPE>(ii, jj);
195 for (k = 0, ii = i + 2, jj = j - 2; k < 5; k++, jj++) {
197 b2[k] = src.at<
NMSTYPE>(ii, jj);
203 dst.at<uchar>(i, j) =
result ? 255 : 0;
206 for (i = 2; i < src.rows - 2; i++) {
207 for (j = 0; j < src.cols; j++) {
209 for (k = 0, ii = i - 2, jj = j - 2; k < 5; k++, jj++) {
211 t1[k] = src.at<
NMSTYPE>(ii, jj);
215 for (k = 0, ii = i - 1, jj = j - 2; k < 5; k++, jj++) {
217 t2[k] = src.at<
NMSTYPE>(ii, jj);
221 for (k = 0, ii = i, jj = j - 2; k < 5; k++, jj++) {
223 m1[k] = src.at<
NMSTYPE>(ii, jj);
227 for (k = 0, ii = i + 1, jj = j - 2; k < 5; k++, jj++) {
229 b1[k] = src.at<
NMSTYPE>(ii, jj);
233 for (k = 0, ii = i + 2, jj = j - 2; k < 5; k++, jj++) {
235 b2[k] = src.at<
NMSTYPE>(ii, jj);
239 }
else if ((j >= 2) && (j < src.cols - 2)) {
240 for (k = 0, ii = i - 2, jj = j - 2; k < 5; k++, jj++) {
241 t1[k] = src.at<
NMSTYPE>(ii, jj);
243 for (k = 0, ii = i - 1, jj = j - 2; k < 5; k++, jj++) {
244 t2[k] = src.at<
NMSTYPE>(ii, jj);
246 for (k = 0, ii = i, jj = j - 2; k < 5; k++, jj++) {
247 m1[k] = src.at<
NMSTYPE>(ii, jj);
249 for (k = 0, ii = i + 1, jj = j - 2; k < 5; k++, jj++) {
250 b1[k] = src.at<
NMSTYPE>(ii, jj);
252 for (k = 0, ii = i + 2, jj = j - 2; k < 5; k++, jj++) {
253 b2[k] = src.at<
NMSTYPE>(ii, jj);
255 }
else if (j >= src.cols - 2) {
256 for (k = 0, ii = i - 2, jj = j - 2; k < 5; k++, jj++) {
258 t1[k] = src.at<
NMSTYPE>(ii, jj);
262 for (k = 0, ii = i - 1, jj = j - 2; k < 5; k++, jj++) {
264 t2[k] = src.at<
NMSTYPE>(ii, jj);
268 for (k = 0, ii = i, jj = j - 2; k < 5; k++, jj++) {
270 m1[k] = src.at<
NMSTYPE>(ii, jj);
274 for (k = 0, ii = i + 1, jj = j - 2; k < 5; k++, jj++) {
276 b1[k] = src.at<
NMSTYPE>(ii, jj);
280 for (k = 0, ii = i + 2, jj = j - 2; k < 5; k++, jj++) {
282 b2[k] = src.at<
NMSTYPE>(ii, jj);
289 dst.at<uchar>(i, j) =
result ? 255 : 0;
294 for (j = 0; j < src.cols; j++) {
296 for (k = 0, ii = i - 2, jj = j - 2; k < 5; k++, jj++) {
298 t1[k] = src.at<
NMSTYPE>(ii, jj);
302 for (k = 0, ii = i - 1, jj = j - 2; k < 5; k++, jj++) {
304 t2[k] = src.at<
NMSTYPE>(ii, jj);
308 for (k = 0, ii = i, jj = j - 2; k < 5; k++, jj++) {
310 m1[k] = src.at<
NMSTYPE>(ii, jj);
314 for (k = 0, ii = i + 1, jj = j - 2; k < 5; k++, jj++) {
316 b1[k] = src.at<
NMSTYPE>(ii, jj);
320 for (k = 0, ii = i + 2, jj = j - 2; k < 5; k++, jj++) {
323 }
else if ((j >= 2) && (j < src.cols - 2)) {
324 for (k = 0, ii = i - 2, jj = j - 2; k < 5; k++, jj++) {
325 t1[k] = src.at<
NMSTYPE>(ii, jj);
327 for (k = 0, ii = i - 1, jj = j - 2; k < 5; k++, jj++) {
328 t2[k] = src.at<
NMSTYPE>(ii, jj);
330 for (k = 0, ii = i, jj = j - 2; k < 5; k++, jj++) {
331 m1[k] = src.at<
NMSTYPE>(ii, jj);
333 for (k = 0, ii = i + 1, jj = j - 2; k < 5; k++, jj++) {
334 b1[k] = src.at<
NMSTYPE>(ii, jj);
336 for (k = 0, ii = i + 2, jj = j - 2; k < 5; k++, jj++) {
339 }
else if (j >= src.cols - 2) {
340 for (k = 0, ii = i - 2, jj = j - 2; k < 5; k++, jj++) {
342 t1[k] = src.at<
NMSTYPE>(ii, jj);
346 for (k = 0, ii = i - 1, jj = j - 2; k < 5; k++, jj++) {
348 t2[k] = src.at<
NMSTYPE>(ii, jj);
352 for (k = 0, ii = i, jj = j - 2; k < 5; k++, jj++) {
354 m1[k] = src.at<
NMSTYPE>(ii, jj);
358 for (k = 0, ii = i + 1, jj = j - 2; k < 5; k++, jj++) {
360 b1[k] = src.at<
NMSTYPE>(ii, jj);
364 for (k = 0, ii = i + 2, jj = j - 2; k < 5; k++, jj++) {
369 dst.at<uchar>(i, j) =
result ? 255 : 0;
373 for (j = 0; j < src.cols; j++) {
375 for (k = 0, ii = i - 1, jj = j - 2; k < 5; k++, jj++) {
377 t1[k] = src.at<
NMSTYPE>(ii, jj);
381 for (k = 0, ii = i - 1, jj = j - 2; k < 5; k++, jj++) {
383 t2[k] = src.at<
NMSTYPE>(ii, jj);
387 for (k = 0, ii = i, jj = j - 2; k < 5; k++, jj++) {
389 m1[k] = src.at<
NMSTYPE>(ii, jj);
393 for (k = 0, ii = i + 1, jj = j - 2; k < 5; k++, jj++) {
396 for (k = 0, ii = i + 2, jj = j - 2; k < 5; k++, jj++) {
399 }
else if ((j >= 2) && (j < src.cols - 2)) {
400 for (k = 0, ii = i - 2, jj = j - 2; k < 5; k++, jj++) {
401 t1[k] = src.at<
NMSTYPE>(ii, jj);
403 for (k = 0, ii = i - 1, jj = j - 2; k < 5; k++, jj++) {
404 t2[k] = src.at<
NMSTYPE>(ii, jj);
406 for (k = 0, ii = i, jj = j - 2; k < 5; k++, jj++) {
407 m1[k] = src.at<
NMSTYPE>(ii, jj);
409 for (k = 0, ii = i + 1, jj = j - 2; k < 5; k++, jj++) {
412 for (k = 0, ii = i + 2, jj = j - 2; k < 5; k++, jj++) {
415 }
else if (j >= src.cols - 2) {
416 for (k = 0, ii = i - 2, jj = j - 2; k < 5; k++, jj++) {
418 t1[k] = src.at<
NMSTYPE>(ii, jj);
422 for (k = 0, ii = i - 1, jj = j - 2; k < 5; k++, jj++) {
424 t2[k] = src.at<
NMSTYPE>(ii, jj);
428 for (k = 0, ii = i, jj = j - 2; k < 5; k++, jj++) {
430 m1[k] = src.at<
NMSTYPE>(ii, jj);
434 for (k = 0, ii = i + 1, jj = j - 2; k < 5; k++, jj++) {
437 for (k = 0, ii = i + 2, jj = j - 2; k < 5; k++, jj++) {
442 dst.at<uchar>(i, j) =
result ? 255 : 0;
449 if (m1 > t1 && m1 > m0 && m1 > m2 && m1 > b1) Max =
true;
463 for (j = 0; j < src.cols; j++) {
470 m2 = src.at<
NMSTYPE>(i, j + 1);
472 b1 = src.at<
NMSTYPE>(i + 1, j);
473 b2 = src.at<
NMSTYPE>(i + 1, j + 1);
474 }
else if ((j > 0) && (j < src.cols - 1)) {
478 m0 = src.at<
NMSTYPE>(i, j - 1);
480 m2 = src.at<
NMSTYPE>(i, j + 1);
481 b0 = src.at<
NMSTYPE>(i + 1, j - 1);
482 b1 = src.at<
NMSTYPE>(i + 1, j);
483 b2 = src.at<
NMSTYPE>(i + 1, j + 1);
484 }
else if (j == src.cols - 1) {
488 m0 = src.at<
NMSTYPE>(i, j - 1);
491 b0 = src.at<
NMSTYPE>(i + 1, j - 1);
492 b1 = src.at<
NMSTYPE>(i + 1, j);
496 dst.at<uchar>(i, j) =
result ? 255 : 0;
498 for (i = 1; i < src.rows - 1; i++) {
499 for (j = 0; j < src.cols; j++) {
502 t1 = src.at<
NMSTYPE>(i - 1, j);
503 t2 = src.at<
NMSTYPE>(i - 1, j + 1);
506 m2 = src.at<
NMSTYPE>(i, j + 1);
508 b1 = src.at<
NMSTYPE>(i + 1, j);
509 b2 = src.at<
NMSTYPE>(i + 1, j + 1);
510 }
else if ((j > 0) && (j < src.cols - 1)) {
511 t0 = src.at<
NMSTYPE>(i - 1, j - 1);
512 t1 = src.at<
NMSTYPE>(i - 1, j);
513 t2 = src.at<
NMSTYPE>(i - 1, j + 1);
514 m0 = src.at<
NMSTYPE>(i, j - 1);
516 m2 = src.at<
NMSTYPE>(i, j + 1);
517 b0 = src.at<
NMSTYPE>(i + 1, j - 1);
518 b1 = src.at<
NMSTYPE>(i + 1, j);
519 b2 = src.at<
NMSTYPE>(i + 1, j + 1);
520 }
else if (j == src.cols - 1) {
521 t0 = src.at<
NMSTYPE>(i - 1, j - 1);
522 t1 = src.at<
NMSTYPE>(i - 1, j);
524 m0 = src.at<
NMSTYPE>(i, j - 1);
527 b0 = src.at<
NMSTYPE>(i + 1, j - 1);
528 b1 = src.at<
NMSTYPE>(i + 1, j);
532 dst.at<uchar>(i, j) =
result ? 255 : 0;
537 for (j = 1; j < src.cols - 1; j++) {
538 t0 = src.at<
NMSTYPE>(i - 1, j - 1);
539 t1 = src.at<
NMSTYPE>(i - 1, j);
540 t2 = src.at<
NMSTYPE>(i - 1, j + 1);
541 m0 = src.at<
NMSTYPE>(i, j - 1);
543 m2 = src.at<
NMSTYPE>(i, j + 1);
548 dst.at<uchar>(i, j) =
result ? 255 : 0;
552 void ocv_ref(cv::Mat img_gray, cv::Mat& ocv_out_img,
float Th) {
553 cv::Mat gradx, grady;
554 cv::Mat gradx2, grady2, gradxy;
555 cv::Mat gradx2g, grady2g, gradxyg;
556 cv::Mat x2y2, xy, mtrace;
557 cv::Mat dst, ocvthresh_img;
561 cv::Sobel(img_gray, gradx, CV_32FC1, 1, 0,
FILTER_WIDTH, 1, 0, cv::BORDER_CONSTANT);
562 cv::Sobel(img_gray, grady, CV_32FC1, 0, 1,
FILTER_WIDTH, 1, 0, cv::BORDER_CONSTANT);
564 pow(gradx, 2.0, gradx2);
565 pow(grady, 2.0, grady2);
566 multiply(gradx, grady, gradxy);
572 multiply(gradx2g, grady2g, x2y2);
573 multiply(gradxyg, gradxyg, xy);
574 pow((gradx2g + grady2g), 2.0, mtrace);
577 dst.create(img_gray.rows, img_gray.cols, CV_32FC1);
578 ocvthresh_img.create(img_gray.rows, img_gray.cols, dst.depth());
581 for (
int j = 0; j < img_gray.rows; j++) {
582 for (
int i = 0; i < img_gray.cols; i++) {
583 float v1 = x2y2.at<
float>(j, i);
584 float v2 = xy.at<
float>(j, i);
585 float v3 = mtrace.at<
float>(j, i);
586 float temp1 = (v1 - v2) - (0.04 * v3);
587 dst.at<
float>(j, i) = temp1;
589 if (temp1 > 0.0) sum += temp1;
595 for (
int i = 0; i < img_gray.rows; i++) {
596 for (
int j = 0; j < img_gray.cols; j++) {
597 float pix = dst.at<
NMSTYPE>(i, j);
599 ocvthresh_img.at<
NMSTYPE>(i, j) = pix;
601 ocvthresh_img.at<
NMSTYPE>(i, j) = 0;
625 ocvthresh_img.~Mat();
bool OCVFindMaximum2(NMSTYPE *t1, NMSTYPE *t2, NMSTYPE *m1, NMSTYPE *b1, NMSTYPE *b2)
void OCVMaxSuppression2(cv::Mat &src, cv::Mat &dst)
void ocv_ref(cv::Mat img_gray, cv::Mat &ocv_out_img, float Th)
bool OCVFindMaximum1(NMSTYPE t0, NMSTYPE t1, NMSTYPE t2, NMSTYPE m0, NMSTYPE m1, NMSTYPE m2, NMSTYPE b0, NMSTYPE b1, NMSTYPE b2)
void OCVMaxSuppression1(cv::Mat &src, cv::Mat &dst)