Ask Your Question
0

wateshed not working

asked 2016-08-11 13:43:05 -0600

vasu12360 gravatar image

updated 2016-08-12 01:03:33 -0600

I am trying to use watershed given in python example ,i am able to select the marker but it is not painting i.e when i am trying to drag on image no region is getting selected.I have not made any changes in the code provided in the example. i am using python 2.7.12 and opencv 2.4.13.

watershed.py:

#!/usr/bin/env python

'''
Watershed segmentation
=========

This program demonstrates the watershed segmentation algorithm
in OpenCV: watershed().

Usage
-----
watershed.py [image filename]

Keys
----
  1-7   - switch marker color
  SPACE - update segmentation
  r     - reset
  a     - toggle autoupdate
  ESC   - exit

'''




import numpy as np
import cv2
from common import Sketcher

class App:
    def __init__(self, fn):
        self.img = cv2.imread(fn)
        h, w = self.img.shape[:2]
        self.markers = np.zeros((h, w), np.int32)
        self.markers_vis = self.img.copy()
        self.cur_marker = 1
        self.colors = np.int32( list(np.ndindex(2, 2, 2)) ) * 255

        self.auto_update = True
        self.sketch = Sketcher('img', [self.markers_vis, self.markers], self.get_colors)

    def get_colors(self):
        return map(int, self.colors[self.cur_marker]), self.cur_marker

    def watershed(self):
        m = self.markers.copy()
        cv2.watershed(self.img, m)
        overlay = self.colors[np.maximum(m, 0)]
        vis = cv2.addWeighted(self.img, 0.5, overlay, 0.5, 0.0, dtype=cv2.CV_8UC3)
        cv2.imshow('watershed', vis)

    def run(self):
        while True:
            ch = 0xFF & cv2.waitKey(50)
            if ch == 27:
                break
            if ch >= ord('1') and ch <= ord('7'):
                self.cur_marker = ch - ord('0')
                print 'marker: ', self.cur_marker
            if ch == ord(' ') or (self.sketch.dirty and self.auto_update):
                self.watershed()
                self.sketch.dirty = False
            if ch in [ord('a'), ord('A')]:
                self.auto_update = not self.auto_update
                print 'auto_update if', ['off', 'on'][self.auto_update]
            if ch in [ord('r'), ord('R')]:
                self.markers[:] = 0
                self.markers_vis[:] = self.img
                self.sketch.show()
        cv2.destroyAllWindows()


if __name__ == '__main__':
    import sys
    try: fn = sys.argv[1]
    except: fn = '../cpp/fruits.jpg'
    print __doc__
    App(fn).run()

common.py:

#!/usr/bin/env python

'''
This module contais some common routines used by other samples.
'''

import numpy as np
import cv2
import os
from contextlib import contextmanager
import itertools as it

image_extensions = ['.bmp', '.jpg', '.jpeg', '.png', '.tif', '.tiff', '.pbm', '.pgm', '.ppm']

class Bunch(object):
    def __init__(self, **kw):
        self.__dict__.update(kw)
    def __str__(self):
        return str(self.__dict__)

def splitfn(fn):
    path, fn = os.path.split(fn)
    name, ext = os.path.splitext(fn)
    return path, name, ext

def anorm2(a):
    return (a*a).sum(-1)
def anorm(a):
    return np.sqrt( anorm2(a) )

def homotrans(H, x, y):
    xs = H[0, 0]*x + H[0, 1]*y + H[0, 2]
    ys = H[1, 0]*x + H[1, 1]*y + H[1, 2]
    s  = H[2, 0]*x + H[2, 1]*y + H[2, 2]
    return xs/s, ys/s

def to_rect(a):
    a = np.ravel(a)
    if len(a) == 2:
        a = (0, 0, a[0], a[1])
    return np.array(a, np.float64).reshape(2, 2)

def rect2rect_mtx(src, dst):
    src, dst = to_rect(src), to_rect(dst ...
(more)
edit retag flag offensive close merge delete

Comments

can you show us your code

Cheqrd gravatar imageCheqrd ( 2016-08-11 22:24:36 -0600 )edit

i have added it in the question part

vasu12360 gravatar imagevasu12360 ( 2016-08-12 01:04:12 -0600 )edit

are you sure, you're pressing the right keys ?

berak gravatar imageberak ( 2016-08-12 02:00:58 -0600 )edit

yep i am quite sure about that

vasu12360 gravatar imagevasu12360 ( 2016-08-12 02:06:11 -0600 )edit

from what i have seen watershed is only importing sketcher from common. In sketcher problem might be most probably in onmouse function, in which "cv2.EVENT_LBUTTONDOWN" is working perfectly fine as i have checked it myself it seems problem exist in "self.prev_pt and flags & cv2.EVENT_FLAG_LBUTTON". see if that helps

vasu12360 gravatar imagevasu12360 ( 2016-08-12 02:53:30 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2016-08-12 01:26:51 -0600

Cheqrd gravatar image

Select a marker first by clicking numbers 1-9

edit flag offensive delete link more

Comments

that's exactly what i am doing

vasu12360 gravatar imagevasu12360 ( 2016-08-12 02:08:22 -0600 )edit

if it runs when you type python watershed.py on terminal. There will be an image of fruits and it should work. try rebuilding your opencv.

Cheqrd gravatar imageCheqrd ( 2016-08-12 02:51:18 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-08-11 13:43:05 -0600

Seen: 981 times

Last updated: Aug 12 '16