diff --git a/modules/ximgproc/src/structured_edge_detection.cpp b/modules/ximgproc/src/structured_edge_detection.cpp index ef8acff4f..0315005d1 100644 --- a/modules/ximgproc/src/structured_edge_detection.cpp +++ b/modules/ximgproc/src/structured_edge_detection.cpp @@ -235,7 +235,6 @@ static void gradientHist(const cv::Mat &src, cv::Mat &magnitude, cv::Mat &histog magnitude /= imsmooth( magnitude, gnrmRad ) + 0.01*cv::Mat::ones( magnitude.size(), magnitude.type() ); - int pHistSize = histogram.cols*histogram.channels() - 1; for (int i = 0; i < phase.rows; ++i) { const float *pPhase = phase.ptr(i); @@ -245,8 +244,12 @@ static void gradientHist(const cv::Mat &src, cv::Mat &magnitude, cv::Mat &histog for (int j = 0; j < phase.cols; ++j) { - int index = cvRound((j/pSize + pPhase[j])*nBins); - index = std::max(0, std::min(index, pHistSize)); + int angle = cvRound(pPhase[j]*nBins); + if(angle >= nBins) + { + angle = 0; + } + const int index = (j/pSize)*nBins + angle; pHist[index] += pMagn[j] / CV_SQR(pSize); } }