forked from Panchamy/RegionGrowing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RegionGrowing.py
93 lines (68 loc) · 2.82 KB
/
RegionGrowing.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import cv2, cv
import numpy as np
from matplotlib import pyplot as plt
import sys
def on_mouse(event, x, y, flags, params):
if event == cv.CV_EVENT_LBUTTONDOWN:
print 'Start Mouse Position: ' + str(x) + ', ' + str(y)
s_box = x, y
boxes.append(s_box)
def region_growing(img, seed):
#Parameters for region growing
neighbors = [(-1, 0), (1, 0), (0, -1), (0, 1)]
region_threshold = 0.2
region_size = 1
intensity_difference = 0
neighbor_points_list = []
neighbor_intensity_list = []
#Mean of the segmented region
region_mean = img[seed]
#Input image parameters
height, width = img.shape
image_size = height * width
#Initialize segmented output image
segmented_img = np.zeros((height, width, 1), np.uint8)
#Region growing until intensity difference becomes greater than certain threshold
while (intensity_difference < region_threshold) & (region_size < image_size):
#Loop through neighbor pixels
for i in range(4):
#Compute the neighbor pixel position
x_new = seed[0] + neighbors[i][0]
y_new = seed[1] + neighbors[i][1]
#Boundary Condition - check if the coordinates are inside the image
check_inside = (x_new >= 0) & (y_new >= 0) & (x_new < height) & (y_new < width)
#Add neighbor if inside and not already in segmented_img
if check_inside:
if segmented_img[x_new, y_new] == 0:
neighbor_points_list.append([x_new, y_new])
neighbor_intensity_list.append(img[x_new, y_new])
segmented_img[x_new, y_new] = 255
#Add pixel with intensity nearest to the mean to the region
distance = abs(neighbor_intensity_list-region_mean)
pixel_distance = min(distance)
index = np.where(distance == pixel_distance)[0][0]
segmented_img[seed[0], seed[1]] = 255
region_size += 1
#New region mean
region_mean = (region_mean*region_size + neighbor_intensity_list[index])/(region_size+1)
#Update the seed value
seed = neighbor_points_list[index]
#Remove the value from the neighborhood lists
neighbor_intensity_list[index] = neighbor_intensity_list[-1]
neighbor_points_list[index] = neighbor_points_list[-1]
return segmented_img
if __name__ == '__main__':
boxes = []
filename = 'image.jpg'
img = cv2.imread(filename, 0)
resized = cv2.resize(img,(256,256))
cv2.namedWindow('input')
cv2.setMouseCallback('input', on_mouse, 0,)
cv2.imshow('input', resized)
cv2.waitKey()
print "Starting region growing based on last click"
seed = boxes[-1]
cv2.imshow('input', region_growing(resized, seed))
print "Done. Showing output now"
cv2.waitKey()
cv2.destroyAllWindows()