0% found this document useful (0 votes)
59 views

Lab07 Object Segmentation

This document contains code for performing image segmentation tasks such as: - Segmenting an image based on a mask using OpenCV bitwise operations. - Applying Otsu's thresholding method to segment objects in an image. - Resizing images to desired widths and heights using OpenCV. - Performing k-means clustering segmentation on color images. - Labeling segmented objects by drawing bounding boxes, centroids, or boundaries. - Selecting segmentation masks based on minimum and maximum region area thresholds. The code includes functions for common image processing tasks like morphology operations, thresholding, labeling connected regions, and plotting/displaying results. It aims to provide tools for segmenting objects in images in

Uploaded by

Dũng Hi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Lab07 Object Segmentation

This document contains code for performing image segmentation tasks such as: - Segmenting an image based on a mask using OpenCV bitwise operations. - Applying Otsu's thresholding method to segment objects in an image. - Resizing images to desired widths and heights using OpenCV. - Performing k-means clustering segmentation on color images. - Labeling segmented objects by drawing bounding boxes, centroids, or boundaries. - Selecting segmentation masks based on minimum and maximum region area thresholds. The code includes functions for common image processing tasks like morphology operations, thresholding, labeling connected regions, and plotting/displaying results. It aims to provide tools for segmenting objects in images in

Uploaded by

Dũng Hi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

8/12/2020 Lab07-Object Segmentation

LAB07 - Image Segmentation

Dr. Tran Anh Tuan,

Faculty of Mathematics and Computer Science,

University of Science, HCMC


In [197]: import numpy as np
import cv2
from matplotlib import pyplot as plt
from skimage.color import rgb2gray
from skimage.filters import threshold_otsu
from skimage.measure import label, regionprops
from skimage.segmentation import mark_boundaries
from scipy import ndimage as ndi
import pandas as pd
import json
import os
import timeit
import random

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 1/31


8/12/2020 Lab07-Object Segmentation

In [198]: def ShowImage(ImageList, nRows = 1, nCols = 2, WidthSpace = 0.00, HeightSpace = 0.00):


from matplotlib import pyplot as plt
import matplotlib.gridspec as gridspec

gs = gridspec.GridSpec(nRows, nCols)
gs.update(wspace=WidthSpace, hspace=HeightSpace) # set the spacing between axes.
plt.figure(figsize=(20,20))
for i in range(len(ImageList)):
ax1 = plt.subplot(gs[i])
ax1.set_xticklabels([])
ax1.set_yticklabels([])
ax1.set_aspect('equal')

plt.subplot(nRows, nCols,i+1)

image = ImageList[i].copy()
if (len(image.shape) < 3):
plt.imshow(image, plt.cm.gray)
else:
plt.imshow(image)
plt.title("Image " + str(i))
plt.axis('off')

plt.show()

In [199]: def FillHoles(Mask):


Result = ndi.binary_fill_holes(Mask)
return Result

def morphology(Mask, Size):


from skimage.morphology import erosion, dilation, opening, closing, white_tophat
from skimage.morphology import disk
selem = disk(abs(Size))
if(Size > 0):
result = dilation(Mask, selem)
else:
result = erosion(Mask, selem)
return result

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 2/31


8/12/2020 Lab07-Object Segmentation

In [200]: import os
import pandas as pd

def get_subfiles(dir):
"Get a list of immediate subfiles"
return next(os.walk(dir))[2]

In [201]: def SegmentColorImageByMask(IM, Mask):


Mask = Mask.astype(np.uint8)
result = cv2.bitwise_and(IM, IM, mask = Mask)
return result

In [202]: def SegmentationByOtsu(image, mask):


image_process = image.copy()
image_mask = mask.copy()

image_process[image_mask == 0] = 0
ListPixel = image_process.ravel()
ListPixel = ListPixel[ListPixel > 0]

from skimage.filters import threshold_otsu


otsu_thresh = threshold_otsu(ListPixel)

return otsu_thresh

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 3/31


8/12/2020 Lab07-Object Segmentation

In [203]: def ResizeImage(IM, DesiredWidth, DesiredHeight):


from skimage.transform import rescale, resize

OrigWidth = float(IM.shape[1])
OrigHeight = float(IM.shape[0])
Width = DesiredWidth
Height = DesiredHeight

if((Width == 0) & (Height == 0)):


return IM

if(Width == 0):
Width = int((OrigWidth * Height)/OrigHeight)

if(Height == 0):
Height = int((OrigHeight * Width)/OrigWidth)

dim = (Width, Height)


# print(dim)
resizedIM = cv2.resize(IM, dim, interpolation = cv2.INTER_NEAREST)
# imshows([IM, resizedIM], ["Image", "resizedIM"],1,2)
return resizedIM

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 4/31


8/12/2020 Lab07-Object Segmentation

In [204]: def SegmentByKmeans(image_orig, nClusters = 3):


img = image_orig.copy()
Z = img.reshape((-1,3))

# convert to np.float32
Z = np.float32(Z)

# define criteria, number of clusters(K) and apply kmeans()


criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 100, 1.0)
K = nClusters
ret,labellist,center=cv2.kmeans(Z,K,None,criteria,10,cv2.KMEANS_RANDOM_CENTERS)

# Now convert back into uint8, and make original image


center = np.uint8(center)
res = center[labellist.flatten()]
res2 = res.reshape((img.shape))
label2 = labellist.reshape((img.shape[:2]))

image_index = label2
image_kmeans = res2

# Sort to make sure the index is stable


AreaList = []
for idx in range(image_index.max() + 1):
mask = image_index == idx
AreaList.append(mask.sum().sum())

sort_index = np.argsort(AreaList)[::-1]
index = 0
image_index1 = image_index * 0
for idx in sort_index:
image_index1[image_index == idx] = index
index = index + 1

image_index = image_index1.copy()

return image_index, image_kmeans

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 5/31


8/12/2020 Lab07-Object Segmentation

In [205]: def LabelObjectByMask(image_input, image_mask, type = "BBox", color = (0,255,0), thick = 2):
# image_input = image_orig.copy()
image_output = image_input.copy()

label_img = label(image_mask)
regions = regionprops(label_img)
for props in regions:
minr, minc, maxr, maxc = props.bbox
left_top = (minc, minr)
right_bottom = (maxc, maxr)
at_row, at_col = props.centroid

if(type == "Center"):
cv2.drawMarker(image_output, (int(at_col), int(at_row)),color, markerType=cv2.MARKER_STAR, marker
Size=15, thickness= 1, line_type=cv2.LINE_AA)
if(type == "BBox"):
cv2.rectangle(image_output,left_top, right_bottom, color ,thick)

if(type == "Boundary"):
color = [(number / 255) for number in color]
image_mask = morphology(image_mask, 1)
image_output = mark_boundaries(image_output, image_mask, color = color, mode='thick')

if(type == "Fill"):
image_output[image_mask > 0] = color

return image_output

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 6/31


8/12/2020 Lab07-Object Segmentation

In [206]: def SelectMaskByThreshArea(Mask, minArea = 300, maxArea = 100000):


import pandas as pd
from skimage.measure import label, regionprops

mask = Mask.copy()
mask_output = mask * 0
bboxList = []

label_img = label(mask)
regions = regionprops(label_img)
for props in regions:
area = props.area
label = props.label
if((area > minArea) and (area < maxArea)):
mask_output = mask_output + (label_img == label).astype(int)

return mask_output

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 7/31


8/12/2020 Lab07-Object Segmentation

In [207]: def ConvertGrayToIndex(image_gray):


PixelList = list(np.unique(image_gray.flatten()))

random_color = lambda: (int(random()*255), int(random()*255), int(random()*255))


colors = [random_color() for i in range(len(PixelList))]

image_index = image_gray.copy()
for idx, value in enumerate(PixelList):
image_index = np.where(image_index==PixelList[idx], -(idx + 1), image_index)
image_index = -image_index

image_color = np.dstack((image_gray,image_gray,image_gray))
rows, cols = image_color.shape[:2]
for x in range(rows):
for y in range(cols):
index = PixelList.index(image_gray[x,y])
image_color[x, y, :] = colors[index]

# Sort to make sure the index is stable


AreaList = []
for idx in range(image_index.max() + 1):
mask = image_index == idx
AreaList.append(mask.sum().sum())

sort_index = np.argsort(AreaList)[::-1]
index = 0
image_index1 = image_index * 0
for idx in sort_index:
image_index1[image_index == idx] = index
index = index + 1
image_index = image_index1.copy()

return image_index, image_color

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 8/31


8/12/2020 Lab07-Object Segmentation

In [208]: def pointsList(new_mask, n):


gray = (new_mask*255).astype(np.uint8)

thresh, im_bw = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)


contours, hierarchy = cv2.findContours(im_bw, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

step = int(len(contours[0])/n)
points = np.array(contours[0][0])

if step == 0:
step = 5
for i in range (0, len(contours[0]), step):
points = np.append((points), (contours[0][i]), axis=0)
if len(points) > n:
np.delete(points, 0)

return points

In [209]: DataPath = "D:\\MSI DATA (Previous Computer)\\Teaching And Training\\Image Segmentation\\Image Segnemtation D
ataSet\\"

path = DataPath
all_names = get_subfiles(path)
print("Number of Images:", len(all_names))
IMG = []
for i in range(len(all_names)):
tmp = cv2.imread(path + all_names[i])
IMG.append(tmp)

SegDataIMG1 = IMG.copy()
SegDataName1 = all_names

Number of Images: 60

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 9/31


8/12/2020 Lab07-Object Segmentation

In [210]: FileName = 'Skin 01.jpg'


idx = SegDataName1.index(FileName)
print("Selected Image : ", "\nIndex ", idx, "\nName ", SegDataName1[idx])

image = SegDataIMG1[idx]
image = ResizeImage(image, DesiredWidth = 300, DesiredHeight = 0)

image_orig = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)


image_gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
image_ycbcr = cv2.cvtColor(image, cv2.COLOR_BGR2YCR_CB)
ShowImage([image_orig, image_gray, image_hsv, image_ycbcr], 1, 4)

Selected Image :
Index 55
Name Skin 01.jpg

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 10/31


8/12/2020 Lab07-Object Segmentation

In [211]: image_output = image_orig.copy()


rows,cols = image_orig.shape[:2]
at_row = 120;
at_col = 140;
mouse_click = (int(at_col), int(at_row))
cv2.drawMarker(image_output, mouse_click ,color = (0, 255, 0),
markerType=cv2.MARKER_STAR, markerSize=10, thickness= 1, line_type=cv2.LINE_AA)
ShowImage([image_output], 1, 2)

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 11/31


8/12/2020 Lab07-Object Segmentation

In [212]: # import the necessary packages


from skimage.segmentation import slic
from skimage.segmentation import mark_boundaries
from skimage.util import img_as_float
from skimage import io
import matplotlib.pyplot as plt

# load the image and convert it to a floating point data type


image = img_as_float(image_orig)
# loop over the number of segments
SegmentIndexList = []
SegmentColorList = []
numSegments = 100
# apply SLIC and extract (approximately) the supplied number
# of segments
segments = slic(image, n_segments = numSegments, sigma = 7)
image_color = mark_boundaries(image, segments)

ShowImage([segments, image_color], 1, 3)

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 12/31


8/12/2020 Lab07-Object Segmentation

In [213]: def GetPointsLabelling(image, image_mask, nPoints = 10):


image_label = label(image_mask)
XList = []
YList = []

for idx in range(image_label.max()):


image_mask = image_label == (idx + 1)
n_points_polygon = round(nPoints)
points = pointsList(image_mask, n_points_polygon)
X = [i[0].item() for i in points]
Y = [i[1].item() for i in points]
XList = XList + X
YList = YList + Y

print("XList : ", XList)


print("YList : ", YList)
image_points = image.copy()
cv2.polylines(image_points,[points],True,(0,255,0))
for col_idx, row_idx in zip(XList, YList):
cv2.drawMarker(image_points, (col_idx, row_idx),(0,0,255), markerType=cv2.MARKER_STAR, markerSize=2,
thickness=1, line_type=cv2.LINE_AA)

return image_points, XList, YList

In [214]: image_output = image_orig.copy()

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 13/31


8/12/2020 Lab07-Object Segmentation

In [215]: at_row = 95;


at_col = 195;
image_mask = (segments == segments[at_row, at_col])

mouse_click = (int(at_col), int(at_row))


cv2.drawMarker(image_output, mouse_click ,color = (0, 255, 0),
markerType=cv2.MARKER_STAR, markerSize=10, thickness= 1, line_type=cv2.LINE_AA)
image_output, XList, YList = GetPointsLabelling(image_output, image_mask, nPoints = 10)

ShowImage([image_mask], 1, 1)
ShowImage([image_output], 1, 1)

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 14/31


8/12/2020 Lab07-Object Segmentation

XList : [189, 189, 184, 182, 185, 190, 196, 202, 206, 206, 202, 196, 190]
YList : [87, 87, 92, 98, 104, 108, 110, 107, 101, 95, 89, 87, 87]

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 15/31


8/12/2020 Lab07-Object Segmentation

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 16/31


8/12/2020 Lab07-Object Segmentation

In [216]: at_row = 120;


at_col = 140;
image_mask = (segments == segments[at_row, at_col])

mouse_click = (int(at_col), int(at_row))


cv2.drawMarker(image_output, mouse_click ,color = (0, 255, 0),
markerType=cv2.MARKER_STAR, markerSize=10, thickness= 1, line_type=cv2.LINE_AA)
image_output, XList, YList = GetPointsLabelling(image_output, image_mask, nPoints = 10)

ShowImage([image_mask], 1, 1)
ShowImage([image_output], 1, 1)

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 17/31


8/12/2020 Lab07-Object Segmentation

XList : [139, 139, 130, 121, 120, 128, 137, 146, 155, 159, 153, 144]
YList : [113, 113, 115, 121, 130, 135, 138, 138, 132, 123, 116, 113]

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 18/31


8/12/2020 Lab07-Object Segmentation

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 19/31


8/12/2020 Lab07-Object Segmentation

In [217]: FileName = 'Hand Gesture 05.jpg'


idx = SegDataName1.index(FileName)
print("Selected Image : ", "\nIndex ", idx, "\nName ", SegDataName1[idx])

image = SegDataIMG1[idx]
image = ResizeImage(image, DesiredWidth = 300, DesiredHeight = 0)

image_orig = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)


image_gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
image_ycbcr = cv2.cvtColor(image, cv2.COLOR_BGR2YCR_CB)
ShowImage([image_orig, image_gray, image_hsv, image_ycbcr], 1, 4)

Selected Image :
Index 44
Name Hand Gesture 05.jpg

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 20/31


8/12/2020 Lab07-Object Segmentation

In [218]: # import the necessary packages


from skimage.segmentation import slic
from skimage.segmentation import mark_boundaries
from skimage.util import img_as_float
from skimage import io
import matplotlib.pyplot as plt

# load the image and convert it to a floating point data type


image = img_as_float(image_orig)
# loop over the number of segments
SegmentIndexList = []
SegmentColorList = []
numSegments = 100
# apply SLIC and extract (approximately) the supplied number
# of segments
segments = slic(image, n_segments = numSegments, sigma = 7)
image_color = mark_boundaries(image, segments)

ShowImage([segments, image_color], 1, 3)

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 21/31


8/12/2020 Lab07-Object Segmentation

In [219]: def doClick(image_check, image_mask, mouse_click, MaskList, ImageList):


image_mask = (image_mask + (segments == segments[at_row, at_col])) > 0
mouse_click = (int(at_col), int(at_row))
cv2.drawMarker(image_check, mouse_click ,color = (0, 255, 0),
markerType=cv2.MARKER_STAR, markerSize=10, thickness= 1, line_type=cv2.LINE_AA)

MaskList.append(image_mask)
ImageList.append(image_check)

return image_check, image_mask, MaskList, ImageList

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 22/31


8/12/2020 Lab07-Object Segmentation

In [220]: ImageList = []
MaskList = []
image_check = image_color.copy()
image_mask = image_gray * 0

at_row = 85; at_col = 170;


image_check, image_mask, MaskList, ImageList = doClick(image_check, image_mask,
mouse_click, MaskList, ImageList)

at_row = 60; at_col = 165;


image_check, image_mask, MaskList, ImageList = doClick(image_check, image_mask,
mouse_click, MaskList, ImageList)

at_row = 50; at_col = 150;


image_check, image_mask, MaskList, ImageList = doClick(image_check, image_mask,
mouse_click, MaskList, ImageList)

at_row = 70; at_col = 150;


image_check, image_mask, MaskList, ImageList = doClick(image_check, image_mask,
mouse_click, MaskList, ImageList)

at_row = 100; at_col = 170;


image_check, image_mask, MaskList, ImageList = doClick(image_check, image_mask,
mouse_click, MaskList, ImageList)

at_row = 30; at_col = 180;


image_check, image_mask, MaskList, ImageList = doClick(image_check, image_mask,
mouse_click, MaskList, ImageList)

ShowImage(MaskList, 2, 3)
ShowImage(ImageList, 2, 3)

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 23/31


8/12/2020 Lab07-Object Segmentation

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 24/31


8/12/2020 Lab07-Object Segmentation

Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 25/31


8/12/2020 Lab07-Object Segmentation

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 26/31


8/12/2020 Lab07-Object Segmentation

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 27/31


8/12/2020 Lab07-Object Segmentation

In [221]: image_output = image_orig.copy()


image_output, XList, YList = GetPointsLabelling(image_output, image_mask, nPoints = 10)

ShowImage([image_output], 1, 1)

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 28/31


8/12/2020 Lab07-Object Segmentation

XList : [167, 167, 164, 138, 133, 141, 157, 184, 190, 196, 197, 171]
YList : [22, 22, 38, 28, 56, 88, 120, 132, 101, 69, 37, 22]

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 29/31


8/12/2020 Lab07-Object Segmentation

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 30/31


8/12/2020 Lab07-Object Segmentation

file:///D:/MSI DATA (Previous Computer)/Teaching And Training/Image Segmentation/Lab07-Object Segmentation.html 31/31

You might also like