Skip to content

Commit 295b1ee

Browse files
committed
canny edge detection
1 parent 9f9b149 commit 295b1ee

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

example_2_6.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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

Comments
 (0)