Community
cancel
Showing results for 
Search instead for 
Did you mean: 
93 Views

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;
	ippiLoadBMP(inputImageFile.c_str(),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;
			break;
		}
	}
	
	imgDst.ResizeNE(w,h);
	status = ippiCanny(dx, dy, imgDst, imgSize, low, high);
	status = ippiAddSfs(imgsrc, imgDst, imgDst1, scaleFactor);
	std::string out4f = path + "lena_gray_detected.bmp";
	imgDst.ResizeNE(imgsrc.Width(),imgsrc.Height());
	imgDst.ResizeNE(w,h);
	ippiSaveBMP(out4f.c_str(),imgDst);
	return(EXIT_SUCCESS); 
}

Thanks, 

Sharath 

0 Kudos
0 Replies
Reply