File: pyramid_segmentation.py

package info (click to toggle)
opencv 2.4.9.1%2Bdfsg-1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 126,800 kB
  • ctags: 62,729
  • sloc: xml: 509,055; cpp: 490,794; lisp: 23,208; python: 21,174; java: 19,317; ansic: 1,038; sh: 128; makefile: 72
file content (41 lines) | stat: -rwxr-xr-x 1,279 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/python
import cv2.cv as cv

class PyrSegmentation:
    def __init__(self, img0):
        self.thresh1 = 255
        self.thresh2 = 30
        self.level =4
        self.storage = cv.CreateMemStorage()
        cv.NamedWindow("Source", 0)
        cv.ShowImage("Source", img0)
        cv.NamedWindow("Segmentation", 0)
        cv.CreateTrackbar("Thresh1", "Segmentation", self.thresh1, 255, self.set_thresh1)
        cv.CreateTrackbar("Thresh2", "Segmentation",  self.thresh2, 255, self.set_thresh2)
        self.image0 = cv.CloneImage(img0)
        self.image1 = cv.CloneImage(img0)
        cv.ShowImage("Segmentation", self.image1)

    def set_thresh1(self, val):
        self.thresh1 = val
        self.on_segment()

    def set_thresh2(self, val):
        self.thresh2 = val
        self.on_segment()

    def on_segment(self):
        comp = cv.PyrSegmentation(self.image0, self.image1, self.storage, \
                            self.level, self.thresh1+1, self.thresh2+1)
        cv.ShowImage("Segmentation", self.image1)

    def run(self):
        self.on_segment()
        cv.WaitKey(0)

if __name__ == "__main__":
    img0 = cv.LoadImage("../c/fruits.jpg", 1)

    # segmentation of the color image
    PyrSegmentation(img0).run()
    cv.DestroyAllWindows()