I working on region growing algorithm implementation in python. But when I run this code on output I get black image with no errors. Use CV threshold function on input image and for seed value I use mouse click to store x,y values in tuple.
def get8n(x, y, shape):
out = []
if y-1 > 0 and x-1 > 0:
out.append( (y-1, x-1) )
if y-1 > 0 :
out.append( (y-1, x))
if y-1 > 0 and x+1 < shape[1]:
out.append( (y-1, x+1))
if x-1 > 0:
out.append( (y, x-1))
if x+1 < shape[1]:
out.append( (y, x+1))
if y+1 < shape[0] and x-1 > 0:
out.append( ( y+1, x-1))
if y+1 < shape[0] :
out.append( (y+1, x))
if y+1 < shape[0] and x+1 < shape[1]:
out.append( (y+1, x+1))
return out
def region_growing(img, seed):
list = []
outimg = np.zeros_like(img)
list.append((seed[0], seed[1]))
while(len(list)):
pix = list[0]
outimg[pix[0], pix[1]] = 255
for coord in get8n(pix[0], pix[1], img.shape):
if img[coord[0], coord[1]] > 0:
outimg[coord[0], coord[1]] = 255
list.append((coord[0], coord[1]))
list.pop(0)
return outimg
def on_mouse(event, x, y, flags, params):
if event == cv2.EVENT_LBUTTONDOWN:
print 'Seed: ' + str(x) + ', ' + str(y)
clicks.append((y,x))
clicks = []
image = cv2.imread('lena.jpg', 0)
ret, img = cv2.threshold(image, 200, 255, cv2.THRESH_BINARY)
cv2.namedWindow('Input')
cv2.setMouseCallback('Input', on_mouse, 0, )
cv2.imshow('Input', img)
cv2.waitKey()
seed = clicks[-1]
cv2.imshow('Region Growing', region_growing(img, seed))
cv2.waitKey()
cv2.destroyAllWindows()
I had some trouble with your get8n() function so I rewrote it. I believe that the code below does what you were asking. There are two lines in the region_growing() function that are commented out. If you uncomment them they will show an animation of what is happening during the processing. It is a good way to visualize the code and give you a sense of where things are failing.
Also, in your code you left the possibility of adding pixels that you had already processed to your "to be processed" list. This was causing an infinite loop. I've added a check that prevents pixels that have already been processed from being added back into the list.
import cv2
import numpy as np
def get8n(x, y, shape):
out = []
maxx = shape[1]-1
maxy = shape[0]-1
#top left
outx = min(max(x-1,0),maxx)
outy = min(max(y-1,0),maxy)
out.append((outx,outy))
#top center
outx = x
outy = min(max(y-1,0),maxy)
out.append((outx,outy))
#top right
outx = min(max(x+1,0),maxx)
outy = min(max(y-1,0),maxy)
out.append((outx,outy))
#left
outx = min(max(x-1,0),maxx)
outy = y
out.append((outx,outy))
#right
outx = min(max(x+1,0),maxx)
outy = y
out.append((outx,outy))
#bottom left
outx = min(max(x-1,0),maxx)
outy = min(max(y+1,0),maxy)
out.append((outx,outy))
#bottom center
outx = x
outy = min(max(y+1,0),maxy)
out.append((outx,outy))
#bottom right
outx = min(max(x+1,0),maxx)
outy = min(max(y+1,0),maxy)
out.append((outx,outy))
return out
def region_growing(img, seed):
seed_points = []
outimg = np.zeros_like(img)
seed_points.append((seed[0], seed[1]))
processed = []
while(len(seed_points) > 0):
pix = seed_points[0]
outimg[pix[0], pix[1]] = 255
for coord in get8n(pix[0], pix[1], img.shape):
if img[coord[0], coord[1]] != 0:
outimg[coord[0], coord[1]] = 255
if not coord in processed:
seed_points.append(coord)
processed.append(coord)
seed_points.pop(0)
#cv2.imshow("progress",outimg)
#cv2.waitKey(1)
return outimg
def on_mouse(event, x, y, flags, params):
if event == cv2.EVENT_LBUTTONDOWN:
print 'Seed: ' + str(x) + ', ' + str(y), img[y,x]
clicks.append((y,x))
clicks = []
image = cv2.imread('lena.bmp', 0)
ret, img = cv2.threshold(image, 128, 255, cv2.THRESH_BINARY)
cv2.namedWindow('Input')
cv2.setMouseCallback('Input', on_mouse, 0, )
cv2.imshow('Input', img)
cv2.waitKey()
seed = clicks[-1]
out = region_growing(img, seed)
cv2.imshow('Region Growing', out)
cv2.waitKey()
cv2.destroyAllWindows()
Here is the result when clicking on the left side of her hat:

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With