f0bb62d8019b3bd7beb4740712cd86a5109db56c
[opencv] / samples / python / edge.py
1 #! /usr/bin/env python
2
3 print "OpenCV Python version of edge"
4
5 import sys
6
7 # import the necessary things for OpenCV
8 from opencv import cv
9 from opencv import highgui
10
11 # some definitions
12 win_name = "Edge"
13 trackbar_name = "Threshold"
14
15 # the callback on the trackbar
16 def on_trackbar (position):
17
18     cv.cvSmooth (gray, edge, cv.CV_BLUR, 3, 3, 0)
19     cv.cvNot (gray, edge)
20
21     # run the edge dector on gray scale
22     cv.cvCanny (gray, edge, position, position * 3, 3)
23
24     # reset
25     cv.cvSetZero (col_edge)
26
27     # copy edge points
28     cv.cvCopy (image, col_edge, edge)
29     
30     # show the image
31     highgui.cvShowImage (win_name, col_edge)
32
33 if __name__ == '__main__':
34     filename = "../c/fruits.jpg"
35
36     if len(sys.argv)>1:
37         filename = sys.argv[1]
38
39     # load the image gived on the command line
40     image = highgui.cvLoadImage (filename)
41
42     if not image:
43         print "Error loading image '%s'" % filename
44         sys.exit(-1)
45
46     # create the output image
47     col_edge = cv.cvCreateImage (cv.cvSize (image.width, image.height), 8, 3)
48
49     # convert to grayscale
50     gray = cv.cvCreateImage (cv.cvSize (image.width, image.height), 8, 1)
51     edge = cv.cvCreateImage (cv.cvSize (image.width, image.height), 8, 1)
52     cv.cvCvtColor (image, gray, cv.CV_BGR2GRAY)
53
54     # create the window
55     highgui.cvNamedWindow (win_name, highgui.CV_WINDOW_AUTOSIZE)
56
57     # create the trackbar
58     highgui.cvCreateTrackbar (trackbar_name, win_name, 1, 100, on_trackbar)
59
60     # show the image
61     on_trackbar (0)
62
63     # wait a key pressed to end
64     highgui.cvWaitKey (0)