diff --git a/EyeRecToo/src/pupil-detection/PuRe.cpp b/EyeRecToo/src/pupil-detection/PuRe.cpp index d1c230108355e0aa0607a6504a8171f2effffd29..bf46d360fcb5c3075b78d93fe0f55f0f808c7a46 100644 --- a/EyeRecToo/src/pupil-detection/PuRe.cpp +++ b/EyeRecToo/src/pupil-detection/PuRe.cpp @@ -38,10 +38,10 @@ #include "PuRe.h" -#include -#include #include #include +#include + #include //#define SAVE_ILLUSTRATION @@ -146,8 +146,6 @@ Mat PuRe::canny(const Mat& in, const bool blurImage, const bool useL2, const int */ double minMag = 0; double maxMag = 0; - float* p_res; - float *p_x, *p_y; // result, x, y cv::magnitude(dx, dy, magnitude); cv::minMaxLoc(magnitude, &minMag, &maxMag); @@ -162,10 +160,10 @@ Mat PuRe::canny(const Mat& in, const bool blurImage, const bool useL2, const int magnitude = magnitude / maxMag; // Histogram - int* histogram = new int[bins](); + auto histogram = std::make_unique(bins); Mat res_idx = (bins - 1) * magnitude; res_idx.convertTo(res_idx, CV_16U); - short* p_res_idx = 0; + short* p_res_idx = nullptr; for (int i = 0; i < res_idx.rows; i++) { p_res_idx = res_idx.ptr(i); for (int j = 0; j < res_idx.cols; j++) @@ -174,65 +172,65 @@ Mat PuRe::canny(const Mat& in, const bool blurImage, const bool useL2, const int // Ratio int sum = 0; - int nonEdgePixels = static_cast(nonEdgePixelsRatio * in.rows * in.cols); + const int nonEdgePixels = static_cast(nonEdgePixelsRatio * in.rows * in.cols); for (int i = 0; i < bins; i++) { sum += histogram[i]; if (sum > nonEdgePixels) { - high_th = float(i + 1) / bins; + high_th = static_cast(i + 1) / bins; break; } } low_th = lowHighThresholdRatio * high_th; - delete[] histogram; - /* * Non maximum supression - */ + */ + enum { + NON_EDGE = 0, + POSSIBLE_EDGE = 128, + EDGE = 255 + }; const float tg22_5 = 0.4142135623730950488016887242097f; const float tg67_5 = 2.4142135623730950488016887242097f; - uchar* _edgeType; - float *p_res_b, *p_res_t; - edgeType.setTo(0); + edgeType.setTo(NON_EDGE); for (int i = 1; i < magnitude.rows - 1; i++) { - _edgeType = edgeType.ptr(i); - - p_res = magnitude.ptr(i); - p_res_t = magnitude.ptr(i - 1); - p_res_b = magnitude.ptr(i + 1); + auto edgeTypePtr = edgeType.ptr(i); - p_x = dx.ptr(i); - p_y = dy.ptr(i); + const auto& p_res = magnitude.ptr(i); + const auto& p_res_t = magnitude.ptr(i - 1); + const auto& p_res_b = magnitude.ptr(i + 1); + const auto& p_x = dx.ptr(i); + const auto& p_y = dy.ptr(i); for (int j = 1; j < magnitude.cols - 1; j++) { - float m = p_res[j]; + const auto& m = p_res[j]; if (m < low_th) continue; - float iy = p_y[j]; - float ix = p_x[j]; - float y = abs(iy); - float x = abs(ix); + const auto& iy = p_y[j]; + const auto& ix = p_x[j]; - uchar val = p_res[j] > high_th ? 255 : 128; + const auto y = abs(iy); + const auto x = abs(ix); + const auto val = p_res[j] > high_th ? EDGE : POSSIBLE_EDGE; + const auto tg22_5x = tg22_5 * x; - float tg22_5x = tg22_5 * x; if (y < tg22_5x) { if (m > p_res[j - 1] && m >= p_res[j + 1]) - _edgeType[j] = val; + edgeTypePtr[j] = val; } else { float tg67_5x = tg67_5 * x; if (y > tg67_5x) { if (m > p_res_b[j] && m >= p_res_t[j]) - _edgeType[j] = val; + edgeTypePtr[j] = val; } else { if ((iy <= 0) == (ix <= 0)) { if (m > p_res_t[j - 1] && m >= p_res_b[j + 1]) - _edgeType[j] = val; + edgeTypePtr[j] = val; } else { if (m > p_res_b[j - 1] && m >= p_res_t[j + 1]) - _edgeType[j] = val; + edgeTypePtr[j] = val; } } } @@ -242,21 +240,19 @@ Mat PuRe::canny(const Mat& in, const bool blurImage, const bool useL2, const int /* * Hystheresis */ - int pic_x = edgeType.cols; - int pic_y = edgeType.rows; - int area = pic_x * pic_y; + const int area = edgeType.cols * edgeType.rows; unsigned int lines_idx = 0; int idx = 0; vector lines; - edge.setTo(0); - for (int i = 1; i < pic_y - 1; i++) { - for (int j = 1; j < pic_x - 1; j++) { + edge.setTo(NON_EDGE); + for (int i = 1; i < edgeType.rows - 1; i++) { + for (int j = 1; j < edgeType.cols - 1; j++) { - if (edgeType.data[idx + j] != 255 || edge.data[idx + j] != 0) + if (edgeType.data[idx + j] != EDGE || edge.data[idx + j] != NON_EDGE) continue; - edge.data[idx + j] = 255; + edge.data[idx + j] = EDGE; lines_idx = 1; lines.clear(); lines.push_back(idx + j); @@ -266,20 +262,20 @@ Mat PuRe::canny(const Mat& in, const bool blurImage, const bool useL2, const int int akt_pos = lines[akt_idx]; akt_idx++; - if (akt_pos - pic_x - 1 < 0 || akt_pos + pic_x + 1 >= area) + if (akt_pos - edgeType.cols - 1 < 0 || akt_pos + edgeType.cols + 1 >= area) continue; for (int k1 = -1; k1 < 2; k1++) for (int k2 = -1; k2 < 2; k2++) { - if (edge.data[(akt_pos + (k1 * pic_x)) + k2] != 0 || edgeType.data[(akt_pos + (k1 * pic_x)) + k2] == 0) + if (edge.data[(akt_pos + (k1 * edgeType.cols)) + k2] != NON_EDGE || edgeType.data[(akt_pos + (k1 * edgeType.cols)) + k2] == NON_EDGE) continue; - edge.data[(akt_pos + (k1 * pic_x)) + k2] = 255; - lines.push_back((akt_pos + (k1 * pic_x)) + k2); + edge.data[(akt_pos + (k1 * edgeType.cols)) + k2] = EDGE; + lines.push_back((akt_pos + (k1 * edgeType.cols)) + k2); lines_idx++; } } } - idx += pic_x; + idx += edgeType.cols; } return edge;