Lab01 Object Segmentation
Lab01 Object Segmentation
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]:
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]:
localhost:8888/lab 3/24
4/25/2020 Lab01-Object Segmentation
In [5]:
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()
localhost:8888/lab 4/24
4/25/2020 Lab01-Object Segmentation
In [6]:
image_process[image_mask == 0] = 0
ListPixel = image_process.ravel()
ListPixel = ListPixel[ListPixel > 0]
return otsu_thresh
In [21]:
In [8]:
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
localhost:8888/lab 9/24
4/25/2020 Lab01-Object Segmentation
In [11]:
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]:
localhost:8888/lab 11/24
4/25/2020 Lab01-Object Segmentation
In [13]:
localhost:8888/lab 12/24
4/25/2020 Lab01-Object Segmentation
In [14]:
localhost:8888/lab 13/24
4/25/2020 Lab01-Object Segmentation
In [15]:
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
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]:
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]:
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)
95
localhost:8888/lab 17/24
4/25/2020 Lab01-Object Segmentation
In [18]:
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]:
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)
first_class1_mask = class1_mask.copy()
first_class2_mask = class2_mask.copy()
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))
localhost:8888/lab 19/24
4/25/2020 Lab01-Object Segmentation
105
64
localhost:8888/lab 20/24
4/25/2020 Lab01-Object Segmentation
localhost:8888/lab 21/24
4/25/2020 Lab01-Object Segmentation
In [42]:
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
localhost:8888/lab 22/24
4/25/2020 Lab01-Object Segmentation
In [49]:
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)
localhost:8888/lab 23/24
4/25/2020 Lab01-Object Segmentation
Selected Image :
Index 47
Name Iris 03.jpg
localhost:8888/lab 24/24