| 
 | 1 | +'''   | 
 | 2 | +Referred to example 2.6 in the book "Learning OpenCV: Computer Vision with the OpenCV Library"  | 
 | 3 | +
  | 
 | 4 | +Example 2-6. The Canny edge detector writes its output to a single channel (grayscale) image  | 
 | 5 | +
  | 
 | 6 | +Brought to you by Abid.K	--mail me at [email protected]  | 
 | 7 | +'''  | 
 | 8 | +########################################################################################  | 
 | 9 | +import cv  | 
 | 10 | +image=cv.LoadImage("test.jpg")  | 
 | 11 | +cv.NamedWindow("input")  | 
 | 12 | +cv.NamedWindow("output")  | 
 | 13 | + | 
 | 14 | +def doCanny(image,lowThresh,highThresh,aperture):  | 
 | 15 | +# 	i have changed code a little bit from text book. With example in text book, you have to load 		grayscale image. But now you can load any image, no matter if RGB or GRAYSCALE  | 
 | 16 | +#	output image with single channel  | 
 | 17 | +	out=cv.CreateImage(cv.GetSize(image),cv.IPL_DEPTH_8U,1)  | 
 | 18 | +#	canny only handles grayscale images. So convert RGB to grayscale  | 
 | 19 | +	if image.nChannels != 1:  | 
 | 20 | +		cv.CvtColor(image,out,cv.CV_RGB2GRAY)  | 
 | 21 | +		cv.Canny(out,out,lowThresh,highThresh,aperture)  | 
 | 22 | +	else:  | 
 | 23 | +		cv.Canny(image,out,lowThresh,highThresh,aperture)  | 
 | 24 | +	return out  | 
 | 25 | + | 
 | 26 | +image=cv.LoadImage("test.jpg")  | 
 | 27 | +#	some arbitrary values are given as parameters  | 
 | 28 | +out=doCanny(image,100,200,3)  | 
 | 29 | +cv.ShowImage("input",image)  | 
 | 30 | +cv.ShowImage("output",out)  | 
 | 31 | +cv.WaitKey(0)  | 
 | 32 | +cv.DestroyWindow("input")  | 
 | 33 | +cv.DestroyWindow("output")  | 
 | 34 | +########################################################################################  | 
0 commit comments