Automatically adapt to the determination of the segmentation threshold of the canny algorithm in OpenCV

Source: Internet
Author: User
Tags ranges

In OpenCV, the speed of edge detection by using the canny operator is very fast, but it is a bit uncomfortable that the high and low thresholds need to be input. In matlab, if the threshold value is not specified, it is automatically determined by the function. Therefore, according to the practice in matlab, the system modifies the canny function so that when the user does not specify the threshold value, the threshold value is automatically determined by the function.

I added a function in the OpenCv original code library to determine the high and low thresholds.

View plaincopy to clipboardprint?
  1. // Follow the matlab model to automatically calculate two thresholds
  2. CV_IMPL void AdaptiveFindThreshold (CvMat * dx, CvMat * dy, double * low, double * high)
  3. {
  4. CvSize size;
  5. IplImage * imge = 0;
  6. Int I, j;
  7. CvHistogram * hist;
  8. Int hist_size = 255;
  9. Float range_0 [] = {0,256 };
  10. Float * ranges [] = {range_0 };
  11. Double PercentOfPixelsNotEdges = 0.7;
  12. Size = cvGetSize (dx );
  13. Imge = cvCreateImage (size, IPL_DEPTH_32F, 1 );
  14. // Calculate the edge strength and coexist in the image
  15. Float maxv = 0;
  16. For (I = 0; I <size. height; I ++)
  17. {
  18. Const short * _ dx = (short *) (dx-> data. ptr + dx-> step * I );
  19. Const short * _ DY = (short *) (dy-> data. PTR + dy-> step * I );
  20. Float * _ image = (float *) (imge-> imagedata + imge-> widthstep * I );
  21. For (j = 0; j <size. width; j ++)
  22. {
  23. _ Image [J] = (float) (ABS (_ DX [J]) + ABS (_ dy [J]);
  24. Maxv = maxv <_ image [J]? _ Image [J]: maxv;
  25. }
  26. }
  27. // Calculate the Histogram
  28. Range_0 [1] = maxv;
  29. Hist_size = (int) (hist_size> maxv? Maxv: hist_size );
  30. Hist = cvCreateHist (1, & hist_size, CV_HIST_ARRAY, ranges, 1 );
  31. CvCalcHist (& imge, hist, 0, NULL );
  32. Int total = (int) (size. height * size. width * PercentOfPixelsNotEdges );
  33. Float sum = 0;
  34. Int icount = hist-> mat. dim [0]. size;
  35. Float * h = (float *) cvPtr1D (hist-> bins, 0 );
  36. For (I = 0; I <icount; I ++)
  37. {
  38. Sum + = h [I];
  39. If (sum> total)
  40. Break;
  41. }
  42. // Calculate the high/low threshold
  43. * High = (I + 1) * maxv/hist_size;
  44. * Low = * high * 0.4;
  45. CvReleaseImage (& imge );
  46. CvReleaseHist (& hist );
  47. }
  48. Make the following changes to the cvmoderation function.
  49. In the function body, when the program uses two sobel operators to calculate the gradient strength in both the horizontal and vertical directions, add the following code:
  50. // Adaptive threshold determination
  51. If (low_thresh =-1 & high_thresh =-1)
  52. {
  53. AdaptiveFindThreshold (dx, dy, & low_thresh, & high_thresh );
  54. }

// Based on matlab, adaptive Calculation of Two thresholds <br/> CV_IMPL void AdaptiveFindThreshold (CvMat * dx, CvMat * dy, double * low, double * high) <br/>{< br/> CvSize size; <br/> IplImage * imge = 0; <br/> int I, j; <br/> CvHistogram * hist; <br/> int hist_size = 255; <br/> float range_0 [] = {0,256}; <br/> float * ranges [] = {range_0 }; <br/> double PercentOfPixelsNotEdges = 0.7; <br/> size = cvGetSize (dx); <br/> imge = cvCreateImage (size, IPL_DEPTH_32F, 1); <br/> // calculate the edge strength, coexist in the image <br/> float maxv = 0; <br/> for (I = 0; I <size. height; I ++) <br/>{< br/> const short * _ dx = (short *) (dx-> data. ptr + dx-> step * I); <br/> const short * _ dy = (short *) (dy-> data. ptr + dy-> step * I); <br/> float * _ image = (float *) (imge-> imageData + imge-> widthStep * I ); <br/> for (j = 0; j <size. width; j ++) <br/>{< br/> _ image [j] = (float) (abs (_ dx [j]) + abs (_ Dy [j]); <br/> maxv = maxv <_ image [j]? _ Image [j]: maxv; <br/>}</p> <p> // calculate the histogram <br/> range_0 [1] = maxv; <br/> hist_size = (int) (hist_size> maxv? Maxv: hist_size); <br/> hist = cvCreateHist (1, & hist_size, CV_HIST_ARRAY, ranges, 1); <br/> cvCalcHist (& imge, hist, 0, NULL); <br/> int total = (int) (size. height * size. width * PercentOfPixelsNotEdges); <br/> float sum = 0; <br/> int icount = hist-> mat. dim [0]. size; </p> <p> float * h = (float *) cvPtr1D (hist-> bins, 0); <br/> for (I = 0; I <icount; I ++) <br/>{< br/> sum + = h [I]; <br/> if (sum> total) <Br/> break; <br/>}< br/> // calculate the high/low threshold <br/> * high = (I + 1) * maxv/hist_size; <br/> * low = * high * 0.4; <br/> cvReleaseImage (& imge); <br/> cvReleaseHist (& hist ); <br/>}</p> <p> make the following changes to the cvkan function. <Br/> In the function body, when the program uses two sobel operators to calculate the gradient strength in both the horizontal and vertical directions, add the following code <br/> // adaptive threshold determination <br/> if (low_thresh =-1 & high_thresh =-1) <br/>{< br/> AdaptiveFindThreshold (dx, dy, & low_thresh, & high_thresh); <br/>}

In this way, when you specify the high/low threshold to-1 when calling the cvmoderation function, the cvmoderation function automatically determines the threshold. Finally, don't forget to recompile the cv library and update the lib and dll libraries. That's all! Http://blog.chinaunix.net/u/30231/showart_233944.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.