You are on page 1of 31

5/13/2020 Lab02-Object Segmentation

LAB02 - Image Segmentation

Dr. Tran Anh Tuan,

Faculty of Mathematics and Computer Science,

University of Science, HCMC


In [1]:

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

localhost:8888/lab 1/31
5/13/2020 Lab02-Object Segmentation

In [2]:

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 [3]:

import os
import pandas as pd

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

localhost:8888/lab 2/31
5/13/2020 Lab02-Object Segmentation

In [4]:

def SegmentColorImageByMask(IM, Mask):


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

In [5]:

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

localhost:8888/lab 3/31
5/13/2020 Lab02-Object Segmentation

In [94]:

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

localhost:8888/lab 4/31
5/13/2020 Lab02-Object Segmentation

In [7]:

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, 10, 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

localhost:8888/lab 5/31
5/13/2020 Lab02-Object Segmentation

In [8]:

DataPath = "D:\\MSI DATA (Previous Computer)\\Teaching And Training\\Image Segmentation\\Image Segnemtation DataSet\\"

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)

SegDataIMG = IMG.copy()
SegDataName = all_names

Number of Images: 60

localhost:8888/lab 6/31
5/13/2020 Lab02-Object Segmentation

In [11]:

FileName = 'Football 02.jpg'


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

image = SegDataIMG[idx]
image_orig = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image_gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
ShowImage([image_orig, image_gray, image_hsv], 1, 3)

Selected Image :
Index 36
Name Football 02.jpg

localhost:8888/lab 7/31
5/13/2020 Lab02-Object Segmentation

In [16]:

image_index, image_kmeans = SegmentByKmeans(image_orig, nClusters = 3)


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

ShowImage([image_orig, image_index, image_kmeans], 1, 3)


ShowImage([image_list[0], image_list[1], image_list[2]], 1, len(image_list))

localhost:8888/lab 8/31
5/13/2020 Lab02-Object Segmentation

In [45]:

from skimage.morphology import erosion, dilation, opening, closing, white_tophat


from skimage.morphology import black_tophat, skeletonize, convex_hull_image
from skimage.morphology import disk
from scipy import ndimage as ndi

image_mask = image_list[2]

from skimage.morphology import disk


se = disk(abs(2))
print(se)

image_dilate = dilation(image_mask, se)


image_erosion = erosion(image_mask, se)
ShowImage([image_mask, image_dilate, image_erosion], 1, 3)

image_opening = opening(image_mask, se)


image_closing = closing(image_mask, se)
ShowImage([image_mask, image_opening, image_closing], 1, 3)

image_wtophat = white_tophat(image_mask, se)


image_btophat = black_tophat(image_mask, se)
ShowImage([image_mask, image_wtophat, image_btophat], 1, 3)

image_skel = skeletonize(image_mask)
image_convex = convex_hull_image(image_mask)
ShowImage([image_mask, image_skel, image_convex], 1, 3)

from scipy import ndimage as ndi


image_fill01 = ndi.binary_fill_holes(image_mask)
image_fill02 = ndi.binary_fill_holes(image_dilate)
ShowImage([image_mask, image_fill01, image_fill02], 1, 3)

localhost:8888/lab 9/31
5/13/2020 Lab02-Object Segmentation

[[0 0 1 0 0]
[0 1 1 1 0]
[1 1 1 1 1]
[0 1 1 1 0]
[0 0 1 0 0]]

localhost:8888/lab 10/31
5/13/2020 Lab02-Object Segmentation

localhost:8888/lab 11/31
5/13/2020 Lab02-Object Segmentation

In [40]:

from skimage.morphology import (square, rectangle, diamond, disk, cube,


octahedron, ball, octagon, star)

se_square = square(5)
se_rect = rectangle(2, 5)
se_diamond = diamond(2)
se_disk = disk(3)
se_oct = octagon(3, 2)
se_star = star(3)

print(se_square, "\n")
print(se_rect,"\n")
print(se_diamond,"\n")
print(se_disk,"\n")
print(se_oct,"\n")
print(se_star,"\n")

localhost:8888/lab 12/31
5/13/2020 Lab02-Object Segmentation

[[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]]

[[1 1 1 1 1]
[1 1 1 1 1]]

[[0 0 1 0 0]
[0 1 1 1 0]
[1 1 1 1 1]
[0 1 1 1 0]
[0 0 1 0 0]]

[[0 0 0 1 0 0 0]
[0 1 1 1 1 1 0]
[0 1 1 1 1 1 0]
[1 1 1 1 1 1 1]
[0 1 1 1 1 1 0]
[0 1 1 1 1 1 0]
[0 0 0 1 0 0 0]]

[[0 0 1 1 1 0 0]
[0 1 1 1 1 1 0]
[1 1 1 1 1 1 1]
[1 1 1 1 1 1 1]
[1 1 1 1 1 1 1]
[0 1 1 1 1 1 0]
[0 0 1 1 1 0 0]]

[[0 0 0 0 1 0 0 0 0]
[0 1 1 1 1 1 1 1 0]
[0 1 1 1 1 1 1 1 0]
[0 1 1 1 1 1 1 1 0]
[1 1 1 1 1 1 1 1 1]
[0 1 1 1 1 1 1 1 0]
[0 1 1 1 1 1 1 1 0]
[0 1 1 1 1 1 1 1 0]
[0 0 0 0 1 0 0 0 0]]

localhost:8888/lab 13/31
5/13/2020 Lab02-Object Segmentation

In [46]:

se = se_square
print(se)

image_dilate = dilation(image_mask, se)


image_erosion = erosion(image_mask, se)
ShowImage([image_mask, image_dilate, image_erosion], 1, 3)

image_opening = opening(image_mask, se)


image_closing = closing(image_mask, se)
ShowImage([image_mask, image_opening, image_closing], 1, 3)

image_wtophat = white_tophat(image_mask, se)


image_btophat = black_tophat(image_mask, se)
ShowImage([image_mask, image_wtophat, image_btophat], 1, 3)

image_skel = skeletonize(image_mask)
image_convex = convex_hull_image(image_mask)
ShowImage([image_mask, image_skel, image_convex], 1, 3)

image_fill01 = ndi.binary_fill_holes(image_mask)
image_fill02 = ndi.binary_fill_holes(image_dilate)
ShowImage([image_mask, image_fill01, image_fill02], 1, 3)

localhost:8888/lab 14/31
5/13/2020 Lab02-Object Segmentation

[[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]
[1 1 1 1 1]]

localhost:8888/lab 15/31
5/13/2020 Lab02-Object Segmentation

localhost:8888/lab 16/31
5/13/2020 Lab02-Object Segmentation

In [51]:

from skimage.measure import label, regionprops

se = se_square
image_mask = image_list[2]

image_mask01 = dilation(image_mask, se)


image_mask02 = ndi.binary_fill_holes(image_mask01)
image_mask03 = erosion(image_mask02)

ShowImage([image_mask, image_mask01, image_mask02, image_mask03], 1, 4)

localhost:8888/lab 17/31
5/13/2020 Lab02-Object Segmentation

In [78]:

image_label = label(image_mask03);
regions = regionprops(image_label, intensity_image=image_gray)
AttDataFrame = pd.DataFrame(columns = ["LabelIndex", "Area", "Ratio", "BoundingBox"])

rowidx = 0
for props in regions:
labelidx = props.label
mask = image_label == labelidx
area = props.area
ratio = (area / (image_label.shape[0] * image_label.shape[1])) * 100
bbox = props.bbox

print("Region in Index : ", labelidx)


print("Area of Region : ", area)
print("Ratio of Region : ", ratio)

if(ratio > 1):


ShowImage([image_mask03, mask], 1, 3)

AttDataFrame.loc[rowidx, "LabelIndex"] = labelidx


AttDataFrame.loc[rowidx, "Area"] = area
AttDataFrame.loc[rowidx, "Ratio"] = ratio
AttDataFrame.loc[rowidx, "BoundingBox"] = bbox
rowidx = rowidx + 1

display(AttDataFrame)

localhost:8888/lab 18/31
5/13/2020 Lab02-Object Segmentation

Region in Index : 1
Area of Region : 122
Ratio of Region : 0.043973471741637835
Region in Index : 2
Area of Region : 108
Ratio of Region : 0.03892733564013841
Region in Index : 3
Area of Region : 12
Ratio of Region : 0.004325259515570934
Region in Index : 4
Area of Region : 42463
Ratio of Region : 15.305291234140714

Region in Index : 5
Area of Region : 9
Ratio of Region : 0.003243944636678201
Region in Index : 6
Area of Region : 20
Ratio of Region : 0.00720876585928489
Region in Index : 7
Area of Region : 9
Ratio of Region : 0.003243944636678201
Region in Index : 8
Area of Region : 165
Ratio of Region : 0.05947231833910035
Region in Index : 9
Area of Region : 11388
Ratio of Region : 4.104671280276817

localhost:8888/lab 19/31
5/13/2020 Lab02-Object Segmentation

localhost:8888/lab 20/31
5/13/2020 Lab02-Object Segmentation

Region in Index : 10
Area of Region : 9
Ratio of Region : 0.003243944636678201
Region in Index : 11
Area of Region : 556
Ratio of Region : 0.20040369088811996
Region in Index : 12
Area of Region : 9
Ratio of Region : 0.003243944636678201
Region in Index : 13
Area of Region : 9
Ratio of Region : 0.003243944636678201
Region in Index : 14
Area of Region : 431
Ratio of Region : 0.1553489042675894
Region in Index : 15
Area of Region : 115
Ratio of Region : 0.04145040369088812
Region in Index : 16
Area of Region : 12
Ratio of Region : 0.004325259515570934
Region in Index : 17
Area of Region : 185
Ratio of Region : 0.06668108419838523
Region in Index : 18
Area of Region : 12
Ratio of Region : 0.004325259515570934
Region in Index : 19
Area of Region : 2056
Ratio of Region : 0.7410611303344867
Region in Index : 20
Area of Region : 19
Ratio of Region : 0.006848327566320645
Region in Index : 21
Area of Region : 495
Ratio of Region : 0.17841695501730104
Region in Index : 22
Area of Region : 4710
Ratio of Region : 1.6976643598615917

localhost:8888/lab 21/31
5/13/2020 Lab02-Object Segmentation

Region in Index : 23
Area of Region : 104
Ratio of Region : 0.03748558246828143
Region in Index : 24
Area of Region : 360
Ratio of Region : 0.12975778546712802
Region in Index : 25
Area of Region : 31
Ratio of Region : 0.011173587081891579
Region in Index : 26
Area of Region : 163
Ratio of Region : 0.05875144175317185
Region in Index : 27
Area of Region : 211
Ratio of Region : 0.0760524798154556
Region in Index : 28
Area of Region : 9
Ratio of Region : 0.003243944636678201

localhost:8888/lab 22/31
5/13/2020 Lab02-Object Segmentation

LabelIndex Area Ratio BoundingBox

0 1 122 0.0439735 (23, 390, 48, 407)

1 2 108 0.0389273 (42, 369, 49, 394)

2 3 12 0.00432526 (53, 401, 56, 405)

3 4 42463 15.3053 (55, 260, 357, 552)

4 5 9 0.00324394 (59, 296, 62, 299)

5 6 20 0.00720877 (73, 292, 79, 296)

6 7 9 0.00324394 (187, 521, 190, 524)

7 8 165 0.0594723 (196, 252, 212, 265)

8 9 11388 4.10467 (206, 0, 302, 193)

9 10 9 0.00324394 (223, 531, 226, 534)

10 11 556 0.200404 (230, 45, 258, 71)

11 12 9 0.00324394 (235, 535, 238, 538)

12 13 9 0.00324394 (260, 399, 263, 402)

13 14 431 0.155349 (264, 363, 308, 386)

14 15 115 0.0414504 (273, 185, 285, 198)

15 16 12 0.00432526 (280, 214, 283, 218)

16 17 185 0.0666811 (281, 261, 301, 279)

17 18 12 0.00432526 (289, 208, 292, 212)

18 19 2056 0.741061 (297, 207, 373, 265)

19 20 19 0.00684833 (303, 388, 308, 392)

20 21 495 0.178417 (305, 145, 323, 182)

21 22 4710 1.69766 (305, 377, 402, 479)

22 23 104 0.0374856 (325, 348, 339, 361)

23 24 360 0.129758 (328, 153, 351, 178)

24 25 31 0.0111736 (345, 281, 350, 288)

25 26 163 0.0587514 (353, 288, 370, 305)

localhost:8888/lab 23/31
5/13/2020 Lab02-Object Segmentation

LabelIndex Area Ratio BoundingBox

26 27 211 0.0760525 (376, 211, 393, 236)

27 28 9 0.00324394 (390, 255, 393, 258)

localhost:8888/lab 24/31
5/13/2020 Lab02-Object Segmentation

In [82]:

# Get Max Region based on Ratio


AttDataFrame = AttDataFrame.sort_values(by = "Ratio", ascending = False)
AttDataFrame = AttDataFrame.reset_index(drop = True)
display(AttDataFrame.head(5))

object_mask = (image_label == AttDataFrame.loc[0, "LabelIndex"])

img = image_orig.copy()
BBox = AttDataFrame.loc[0, "BoundingBox"]
minr, minc, maxr, maxc = BBox
left_top = (minc, minr)
right_bottom = (maxc, maxr)
cv2.rectangle(img,left_top, right_bottom, (0,255,0),5)

ShowImage([image_label, object_mask, img], 1, 3)

localhost:8888/lab 25/31
5/13/2020 Lab02-Object Segmentation

LabelIndex Area Ratio BoundingBox

0 4 42463 15.3053 (55, 260, 357, 552)

1 9 11388 4.10467 (206, 0, 302, 193)

2 22 4710 1.69766 (305, 377, 402, 479)

3 19 2056 0.741061 (297, 207, 373, 265)

4 11 556 0.200404 (230, 45, 258, 71)

localhost:8888/lab 26/31
5/13/2020 Lab02-Object Segmentation

In [95]:

FileName = 'Iris 01.jpg'


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

image = SegDataIMG[idx]
image = ResizeImage(image, 0, 300)
image_orig = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image_gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
image_hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
ShowImage([image_orig, image_gray, image_hsv], 1, 3)

image_index, image_kmeans = SegmentByKmeans(image_orig, nClusters = 3)


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

ShowImage([image_orig, image_index, image_kmeans], 1, 3)


ShowImage([image_list[0], image_list[1], image_list[2]], 1, len(image_list))

localhost:8888/lab 27/31
5/13/2020 Lab02-Object Segmentation

Selected Image :
Index 45
Name Iris 01.jpg

localhost:8888/lab 28/31
5/13/2020 Lab02-Object Segmentation

In [103]:

from skimage.measure import label, regionprops


image_mask = image_list[2]
image_mask01 = ndi.binary_fill_holes(image_mask)

se_square = square(30)
image_mask02 = erosion(image_mask01, se_square)
se_square = square(30)
image_mask03 = dilation(image_mask02, se_square)

ShowImage([image_mask, image_mask01, image_mask02, image_mask03], 1, 4)

image_label = label(image_mask03);
regions = regionprops(image_label, intensity_image=image_gray)
AttDataFrame = pd.DataFrame(columns = ["LabelIndex", "Area", "Ratio", "BoundingBox"])
rowidx = 0
for props in regions:
labelidx = props.label
mask = image_label == labelidx
area = props.area
ratio = (area / (image_label.shape[0] * image_label.shape[1])) * 100
bbox = props.bbox

print("Region in Index : ", labelidx)


print("Area of Region : ", area)
print("Ratio of Region : ", ratio)

if(ratio > 1):


ShowImage([image_mask03, mask], 1, 3)

AttDataFrame.loc[rowidx, "LabelIndex"] = labelidx


AttDataFrame.loc[rowidx, "Area"] = area
AttDataFrame.loc[rowidx, "Ratio"] = ratio
AttDataFrame.loc[rowidx, "BoundingBox"] = bbox
rowidx = rowidx + 1

display(AttDataFrame)

localhost:8888/lab 29/31
5/13/2020 Lab02-Object Segmentation

Region in Index : 1
Area of Region : 31034
Ratio of Region : 18.053519488074464

Region in Index : 2
Area of Region : 3083
Ratio of Region : 1.7934845840605003

localhost:8888/lab 30/31
5/13/2020 Lab02-Object Segmentation

LabelIndex Area Ratio BoundingBox

0 1 31034 18.0535 (35, 198, 223, 413)

1 2 3083 1.79348 (93, 455, 155, 518)

In [104]:

# Get Max Region based on Ratio


AttDataFrame = AttDataFrame.sort_values(by = "Ratio", ascending = False)
AttDataFrame = AttDataFrame.reset_index(drop = True)
display(AttDataFrame.head(5))

object_mask = (image_label == AttDataFrame.loc[0, "LabelIndex"])

img = image_orig.copy()
BBox = AttDataFrame.loc[0, "BoundingBox"]
minr, minc, maxr, maxc = BBox
left_top = (minc, minr)
right_bottom = (maxc, maxr)
cv2.rectangle(img,left_top, right_bottom, (0,255,0),5)

ShowImage([image_label, object_mask, img], 1, 3)

LabelIndex Area Ratio BoundingBox

0 1 31034 18.0535 (35, 198, 223, 413)

1 2 3083 1.79348 (93, 455, 155, 518)

localhost:8888/lab 31/31

You might also like