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

Lab01 Object Segmentation

This document discusses image segmentation techniques. It defines functions for displaying images, segmenting images using masks, and applying morphological operations. It loads a dataset of 60 images and selects one image ("Flower 01.jpg") for segmentation. The document applies global thresholding on grayscale and HSV color channels to generate masks and segment the image into two classes. It evaluates threshold values visually and segments the selected image for demonstration.

Uploaded by

Dũng Hi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Lab01 Object Segmentation

This document discusses image segmentation techniques. It defines functions for displaying images, segmenting images using masks, and applying morphological operations. It loads a dataset of 60 images and selects one image ("Flower 01.jpg") for segmentation. The document applies global thresholding on grayscale and HSV color channels to generate masks and segment the image into two classes. It evaluates threshold values visually and segments the selected image for demonstration.

Uploaded by

Dũng Hi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

4/25/2020 Lab01-Object Segmentation

LAB01 - 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/24
4/25/2020 Lab01-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/24
4/25/2020 Lab01-Object Segmentation

In [4]:

def SegmentColorImageByMask(IM, Mask):


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

localhost:8888/lab 3/24
4/25/2020 Lab01-Object Segmentation

In [5]:

def ShowHistogram(image, threshold = 50, segment = 1):


if(len(image.shape) >= 3):
image_process = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
else:
image_process = image.copy()

class1_mask = image_process <= threshold


class2_mask = image_process >= threshold

if(segment == 0):
plt.figure()
hist = cv2.calcHist([image_process],[0],None,[256],[0,256])
plt.hist(image_process.ravel(),256,[0,256])
plt.axvline(x=threshold, color='r', linestyle='dashed', linewidth=2)
plt.title('Histogram for gray scale picture')
plt.show()
else:
plt.figure()
plt.figure(figsize=(20,2))
plt.subplot(1, 4, 1)

hist = cv2.calcHist([image_process],[0],None,[256],[0,256])
plt.hist(image_process.ravel(),256,[0,256])
plt.axvline(x=threshold, color='r', linestyle='dashed', linewidth=2)
plt.title('Histogram for gray scale picture')

plt.subplot(1, 4, 2)
plt.imshow(image_process, plt.cm.gray)
plt.subplot(1, 4, 3)
plt.imshow(class1_mask, plt.cm.gray)
plt.subplot(1, 4, 4)
plt.imshow(class2_mask, plt.cm.gray)
plt.show()

return class1_mask, class2_mask

localhost:8888/lab 4/24
4/25/2020 Lab01-Object Segmentation

In [6]:

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

In [21]:

In [8]:

def morphology_process(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

localhost:8888/lab 5/24
4/25/2020 Lab01-Object Segmentation

In [9]:

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/24
4/25/2020 Lab01-Object Segmentation

In [10]:

display(SegDataName)

localhost:8888/lab 7/24
4/25/2020 Lab01-Object Segmentation

['DefectA 01.bmp',
'DefectA 02.bmp',
'DefectA 03.bmp',
'DefectA 04.bmp',
'DefectA 05.bmp',
'DefectB 01.bmp',
'DefectB 02.bmp',
'DefectB 03.bmp',
'DefectB 04.bmp',
'DefectB 05.bmp',
'DrivingPlate 01.jpg',
'DrivingPlate 02.jpg',
'DrivingPlate 03.jpg',
'DrivingPlate 04.jpg',
'DrivingPlate 05.jpg',
'Eye 01.jpg',
'Eye 02.jpg',
'Eye 03.jpg',
'Eye 04.jpg',
'Eye 05.jpg',
'Face 01.jpg',
'Face 02.jpg',
'Face 03.jpg',
'Face 04.jpg',
'Face 05.jpg',
'Fire 01.jpg',
'Fire 02.jpg',
'Fire 03.jpg',
'Fire 04.jpg',
'Fire 05.jpg',
'Flower 01.jpg',
'Flower 02.jpg',
'Flower 03.jpg',
'Flower 04.jpg',
'Flower 05.jpg',
'Football 01.jpg',
'Football 02.jpg',
'Football 03.jpg',
'Football 04.jpg',
'Football 05.jpg',
'Hand Gesture 01.jpg',
'Hand Gesture 02.jpg',
'Hand Gesture 03.jpg',
localhost:8888/lab 8/24
4/25/2020 Lab01-Object Segmentation

'Hand Gesture 04.jpg',


'Hand Gesture 05.jpg',
'Iris 01.jpg',
'Iris 02.jpg',
'Iris 03.jpg',
'Iris 04.jpg',
'Iris 05.jpg',
'Lung 01.png',
'Lung 02.png',
'Lung 03.png',
'Lung 04.jpg',
'Lung 05.jpg',
'Skin 01.jpg',
'Skin 02.jpg',
'Skin 03.jpg',
'Skin 04.jpg',
'Skin 05.jpg']

localhost:8888/lab 9/24
4/25/2020 Lab01-Object Segmentation

In [11]:

FileName = 'Flower 01.jpg'


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

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

Selected Image :
Index 30
Name Flower 01.jpg

localhost:8888/lab 10/24
4/25/2020 Lab01-Object Segmentation

In [12]:

class1_mask, class2_mask = ShowHistogram(image_gray, threshold = 90, segment = 1)


class1_mask, class2_mask = ShowHistogram(image_gray, threshold = 130, segment = 1)
class1_mask, class2_mask = ShowHistogram(image_gray, threshold = 170, segment = 1)

<Figure size 432x288 with 0 Axes>

<Figure size 432x288 with 0 Axes>

<Figure size 432x288 with 0 Axes>

localhost:8888/lab 11/24
4/25/2020 Lab01-Object Segmentation

In [13]:

class1_mask, class2_mask = ShowHistogram(image_gray, threshold = 170, segment = 1)


image_class1 = SegmentColorImageByMask(image_orig, class1_mask)
image_class2 = SegmentColorImageByMask(image_orig, class2_mask)
ShowImage([image_orig, image_class1, image_class2], 1, 3)

<Figure size 432x288 with 0 Axes>

localhost:8888/lab 12/24
4/25/2020 Lab01-Object Segmentation

In [14]:

# Method 1 : Global Thresholding


class1_mask, class2_mask = ShowHistogram(image_hsv[:,:,0], threshold = 80, segment = 1)
image_class1 = SegmentColorImageByMask(image_orig, class1_mask)
image_class2 = SegmentColorImageByMask(image_orig, class2_mask)
ShowImage([image_orig, image_class1, image_class2], 1, 3)

<Figure size 432x288 with 0 Axes>

localhost:8888/lab 13/24
4/25/2020 Lab01-Object Segmentation

In [15]:

# Method 2 : Local Thresholding

FileName = 'Eye 02.jpg'


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

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

adaptive01_mask = cv2.adaptiveThreshold(image_gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,11,2)/255
adaptive02_mask = cv2.adaptiveThreshold(image_gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,15,2)/255
adaptive03_mask = cv2.adaptiveThreshold(image_gray,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,21,2)/255

ShowImage([image_gray, adaptive01_mask, adaptive02_mask, adaptive03_mask], 1, 4)


ShowImage([image_gray, 1 - adaptive01_mask, 1 - adaptive02_mask, 1 - adaptive03_mask], 1, 4)

localhost:8888/lab 14/24
4/25/2020 Lab01-Object Segmentation

Selected Image :
Index 16
Name Eye 02.jpg

localhost:8888/lab 15/24
4/25/2020 Lab01-Object Segmentation

In [16]:

FileName = 'Fire 03.jpg'


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

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

Selected Image :
Index 27
Name Fire 03.jpg

localhost:8888/lab 16/24
4/25/2020 Lab01-Object Segmentation

In [17]:

# Method 3 : Otsu Thresholding by 1 times

image_process = image_gray.copy()
# Otsu's thresholding
mask = (image_process > 0).astype(int)
image = image_process.copy()
otsu_thresh = SegmentationByOtsu(image_process, mask)
print(otsu_thresh)

class1_mask, class2_mask = ShowHistogram(image_process, threshold = otsu_thresh, segment = 1)


image_class1 = SegmentColorImageByMask(image_orig, class1_mask)
image_class2 = SegmentColorImageByMask(image_orig, class2_mask)
ShowImage([image_orig, image_class1, image_class2], 1, 3)

95

<Figure size 432x288 with 0 Axes>

localhost:8888/lab 17/24
4/25/2020 Lab01-Object Segmentation

In [18]:

FileName = 'Skin 04.jpg'


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

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

Selected Image :
Index 58
Name Skin 04.jpg

localhost:8888/lab 18/24
4/25/2020 Lab01-Object Segmentation

In [19]:

# Method 4 : Otsu Thresholding more than 1 times

image_process = image_gray.copy()
# Otsu's thresholding fist time
mask = (image_process > 0).astype(int)
image = image_process.copy()
otsu_thresh = SegmentationByOtsu(image_process, mask)
print(otsu_thresh)

class1_mask, class2_mask = ShowHistogram(image_process, threshold = otsu_thresh, segment = 1)


image_class1 = SegmentColorImageByMask(image_orig, class1_mask)
image_class2 = SegmentColorImageByMask(image_orig, class2_mask)
ShowImage([image_orig, image_class1, image_class2], 1, 3)

first_class1_mask = class1_mask.copy()
first_class2_mask = class2_mask.copy()

# Otsu's thresholding second time


mask = class1_mask
image = image_process.copy()
otsu_thresh = SegmentationByOtsu(image_process, mask)
print(otsu_thresh)
class1_mask, class2_mask = ShowHistogram(image_process, threshold = otsu_thresh, segment = 1)

image_class1 = SegmentColorImageByMask(image_orig, class1_mask)


image_class2 = SegmentColorImageByMask(image_orig, class2_mask)
ShowImage([image_orig, image_class1, image_class2], 1, 3)

second_class1_mask = class1_mask.copy()
second_class2_mask = class2_mask.copy()

second_class1_update_mask = morphology_process(second_class1_mask, 2)
image_colormark = mark_boundaries(image_orig, second_class1_update_mask, color = (0,1,0))

ShowImage([image_colormark, second_class1_mask, second_class1_update_mask], 1, 3)

localhost:8888/lab 19/24
4/25/2020 Lab01-Object Segmentation

105

<Figure size 432x288 with 0 Axes>

64

<Figure size 432x288 with 0 Axes>

localhost:8888/lab 20/24
4/25/2020 Lab01-Object Segmentation

localhost:8888/lab 21/24
4/25/2020 Lab01-Object Segmentation

In [42]:

def SegmentationBySTD(image, mask, rate_lower = 0.1, rate_upper = 0.1):


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

image_process[image_mask == 0] = 0
ListPixel = image_process.ravel()
ListPixel = ListPixel[ListPixel > 0]
std_thresh_Lower = int(ListPixel.mean()) - rate_lower * int(ListPixel.std())
std_thresh_Upper = int(ListPixel.mean()) + rate_upper * int(ListPixel.std())
image_mask_Lower = image_process > std_thresh_Lower
image_mask_Uppper = image_process < std_thresh_Upper
image_mask = image_mask_Lower * image_mask_Uppper

class1_mask, class2_mask = ShowHistogram(image, threshold = std_thresh_Lower, segment = 1)


class1_mask, class2_mask = ShowHistogram(image, threshold = std_thresh_Upper, segment = 1)

return image_mask, image_mask_Lower, image_mask_Uppper

localhost:8888/lab 22/24
4/25/2020 Lab01-Object Segmentation

In [49]:

# Method 5 : Standard Deviation Thresholding


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

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

image_process = image_gray.copy()
mask = (image_process > 0).astype(int)

image_mask, image_mask_Lower, image_mask_Uppper = SegmentationBySTD(image_process, mask, rate_lower = 1.0, rate_upper =


0.5)
ShowImage([image_mask_Lower, image_mask_Uppper, image_mask], 1, 3)

localhost:8888/lab 23/24
4/25/2020 Lab01-Object Segmentation

Selected Image :
Index 47
Name Iris 03.jpg

<Figure size 432x288 with 0 Axes>

<Figure size 432x288 with 0 Axes>

localhost:8888/lab 24/24

You might also like