Intel® Integrated Performance Primitives
Community support and discussions relating to developing high-performance vision, signal, security, and storage applications.
This community is designed for sharing of public information. Please do not share Intel or third-party confidential information here.
6625 Discussions

Determining the thresholds automatically for Canny Edge Detector


Hello All, 

I am working on Canny Edge Detection. I need to determine the high and the low thresholds automatically. I was planning to use median value auto-thresholding. I am trying to find the histogram of the image, sort histogram index by value and take the median value. 

In my below code, I calculate the median without sorting the histogram. I tried using the IPP sort functions but that seems to not work for my problem. How do I go about sorting the histogram index by value so that while calculating the median, I get the correct index value ?

Kindly please help me solve this issue. 

Note: I use templates for the image functions (histogrameven, sobel, canny) just for clarification. 

#define GrayLevels 256

int  main()
	std::string path("C:\\Images\\Edge\\");
	std::string inputImageFile = path + "lena_gray.bmp";

	IppImage<Ipp8u,1> imgsrc;
	IppImage<Ipp8u,1> imgDst(imgsrc.Size());
	IppImage<Ipp8u,1> dx(imgsrc.Size());
	IppImage<Ipp8u,1> dy(imgsrc.Size());
	Ipp32s imHist[GrayLevels];
	Ipp32s imLevels[GrayLevels+1]; 
	int scaleFactor = 1.2;
	Ipp32f high;
	Ipp32f low; 
	int i;
	IppiSize imgSize;
	int w = imgsrc.Width();
	int h = imgsrc.Height();
	IppStatus status;
	imgSize.width = w;
	imgSize.height = h;
	Ipp32s cumSum;
	cumSum = 0;
	IppImage<Ipp8u,1> imgDst1(w,h);

	status = ippiHistogramEven(imgsrc, imgSize, imHist, imLevels, GrayLevels+1, 0, GrayLevels);
	status = ippiSobelFilterNegVertBorder(imgsrc, dx, imgSize, ippMskSize3x3, ippBorderRepl, 0);
	status = ippiSobelFilterHorizBorder(imgsrc, dy, imgSize, ippMskSize3x3, ippBorderRepl, 0);
//Calculating the high and the low thresholds 

	for (i = 0; i < GrayLevels; i++)
		cumSum += imHist;
		if (cumSum >= (Ipp32s)(0.5 * w * h)) 
			high = i;
			low = 0.5 * high;
	status = ippiCanny(dx, dy, imgDst, imgSize, low, high);
	status = ippiAddSfs(imgsrc, imgDst, imgDst1, scaleFactor);
	std::string out4f = path + "lena_gray_detected.bmp";



0 Kudos
0 Replies