Professional Documents
Culture Documents
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]:
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]:
In [5]:
image_process[image_mask == 0] = 0
ListPixel = image_process.ravel()
ListPixel = ListPixel[ListPixel > 0]
return otsu_thresh
localhost:8888/lab 3/31
5/13/2020 Lab02-Object Segmentation
In [94]:
OrigWidth = float(IM.shape[1])
OrigHeight = float(IM.shape[0])
Width = DesiredWidth
Height = DesiredHeight
if(Width == 0):
Width = int((OrigWidth * Height)/OrigHeight)
if(Height == 0):
Height = int((OrigHeight * Width)/OrigWidth)
localhost:8888/lab 4/31
5/13/2020 Lab02-Object Segmentation
In [7]:
# convert to np.float32
Z = np.float32(Z)
image_index = label2
image_kmeans = res2
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()
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]:
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]:
localhost:8888/lab 8/31
5/13/2020 Lab02-Object Segmentation
In [45]:
image_mask = image_list[2]
image_skel = skeletonize(image_mask)
image_convex = convex_hull_image(image_mask)
ShowImage([image_mask, image_skel, image_convex], 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]:
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_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]:
se = se_square
image_mask = image_list[2]
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
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
localhost:8888/lab 23/31
5/13/2020 Lab02-Object Segmentation
localhost:8888/lab 24/31
5/13/2020 Lab02-Object Segmentation
In [82]:
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)
localhost:8888/lab 25/31
5/13/2020 Lab02-Object Segmentation
localhost:8888/lab 26/31
5/13/2020 Lab02-Object Segmentation
In [95]:
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)
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]:
se_square = square(30)
image_mask02 = erosion(image_mask01, se_square)
se_square = square(30)
image_mask03 = dilation(image_mask02, se_square)
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
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
In [104]:
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)
localhost:8888/lab 31/31