What Is The Objective Of This Topic: The objective of this topic is to apply region growing and k means clustering algorithm to segment an image.
What Is The Description Of This Topic: Region Growing is also another kind of region-based segmentation method. For region growing, a “seed” point is selected. Whenever this seed is encountered in an image, its surrounding neighboring pixels are checked and a decision is made whether a neighboring pixel should be added to the region or not following a selection criterion. One criterion that can be used is that only that pixel is kept from the neighbors that has the same intensity value as the pixel itself.
1.Write a program that take dermoscopic image as input (images used in assignment 1) and applies region growing on it. You have to segment out lesions. Take center of the image as seed point. Display the resultant image.
Code
import numpy as np
import cv2 as cv
import math as m
img = cv.imread('freestudystrom.bmp',0) # Read input image
dims = np.shape(img) #image dimensions
rows = dims[0] #rows of image
cols = dims[1] #columns of image
m = np.min(img) #minimum pixel value
seed_val = m+6 #seed value
Epsilon =10 #epsilon
padded_img =np.pad(img,1,'constant')
output= np.copy(padded_img)
def Again(seed,i,j):
for y in range(i - 1, i + 2):
for z in range(j - 1, j + 2):
if (output[y, z] != 255):
print('Call again',y,z)
Neigh(seed, y, z)
def Neigh(seed, i, j):
while (i-1>0 and j-1>0):
for k in range(i - 1, i + 2):
for f in range(j - 1, j + 2):
#calculate distance of the neighbouring pixels from the centre one
diff = abs(int(padded_img[k, f] - padded_img[i,j] ))
if diff <= Epsilon:
output[k, f] = 255
else:
output[k, f] = padded_img[k, f]
if (i>1 and j>1):
Again(seed_val, i, j)
else:
break
#main program
for i in range(1, rows):
for j in range(1, cols):
if padded_img[i,j] == seed_val: #centre of region to be detected
cx =i
cy=j
print('Seed value at [',cx,'][', cy,']')
Neigh (seed_val,cx,cy) #call the function
cv.imshow('Region Growing Algorithm Image',output)
cv.waitKey(0)
Output
2.Write a function that takes dermoscopic skin image (used in task 3 as well) as input and segments the image using K-mean clustering with and k=2. Display the resulting image.
Code
import numpy as np
import cv2 as cv
img = cv.imread('freestudystrom.bmp',0) # Read input image
output1 =
np.copy(img) #image for mean
thresholding/segmentation
output2 =
np.copy(img) #image for binary
segmentation
dims =
np.shape(img) #image dimensions
rows = dims[0] #rows of image
cols = dims[1] #cols of image
M1 =
np.min(img) + 10 #Initial Mean for Cluster 1
M2 =
np.max(img) - 20 #Initial Mean for Cluster 2
Cluster1 =
[] #Cluster 1 defined
Cluster2 =
[] #Cluster 2 defined
while(1):
for i in range(0, rows):
for j in range(0, cols):
diff1 = abs(img[i, j] - M1) #difference of pixel value and Mean 1
diff2 = abs(img[i, j] - M2) #difference of pixel value and Mean 2
decide = min(diff1, diff2) #Comparative minimum difference
if decide == diff1: #Closer to Mean 1
Cluster1.append(img[i, j]) #Add to Cluster 1
else: #Closer to Mean 2
Cluster2.append(img[i, j]) #Add to Cluster 2
N1 = np.mean(Cluster1) #Calculate mean for Cluster 1
N2 = np.mean(Cluster2) #Calculate mean for Cluster 2
if (N1==M1) and (N2==M2): #End when means become
consistent
break
else: #Update Means and Make Clusters
empty again
M1 = N1
M2 = N2
Cluster1 = []
Cluster2 = []
L1 = len(Cluster1) #length of Cluster 1
L2 = len(Cluster2) #length of Cluster 2
for k in range(0, L1):
output1[output1 == Cluster1[k]] =
N1 #assign Mean 1 to Cluster 1
pixels
for k in range(0, L2):
output1[output1 == Cluster2[k]] =
N2 #assign Mean 2 to Cluster 2
pixels
for k in range(0, L1):
output2[output2 == Cluster1[k]] = 0 #assign 0 to Cluster 1 pixels
for k in range(0, L2):
output2[output2 == Cluster2[k]] = 255 #assign 255 to Cluster 1 pixels
cv.imshow('Mean Thresholded Image',output1)
cv.imshow('Final Binary Image',output2)
cv.waitKey(0)
Output
No comments:
Post a Comment