Lab04 Object Segmentation
Lab04 Object Segmentation
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 [4]: import os
import pandas as pd
def get_subfiles(dir):
"Get a list of immediate subfiles"
return next(os.walk(dir))[2]
image_process[image_mask == 0] = 0
ListPixel = image_process.ravel()
ListPixel = ListPixel[ListPixel > 0]
return otsu_thresh
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)
# 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()
In [9]: 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
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
#combine the horizontal and vertical scores into a total edge score
edge_score = (vertical_score**2 + horizontal_score**2)**.5
#remap the values in the 0-1 range in case they went out of bounds
edges_img = edges_img/edges_img.max()
edges_img = (edges_img[:,:,0]*255).astype(int)
return edges_img
In [12]: DataPath = "D:\\MSI DATA (Previous Computer)\\Teaching And Training\\Image Segmentation\\Image Segmentation D
ataSet1\\"
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: 45
image = SegDataIMG[idx]
image = ResizeImage(image, DesiredWidth = 300, DesiredHeight = 0)
Selected Image :
Index 3
Name Bill 04.jpg
s = image_hsv[:,:,1]
image_mask_object = image_mask_fill.copy()
image_mask_object[s > 50] = 0
image_mask_object = FillHoles(image_mask_object)
image = SegDataIMG[idx]
image = ResizeImage(image, DesiredWidth = 300, DesiredHeight = 0)
Selected Image :
Index 15
Name Cross 01.jpg
image = SegDataIMG[idx]
image = ResizeImage(image, DesiredWidth = 300, DesiredHeight = 0)
Selected Image :
Index 41
Name TrafficSign 02.jpg
img = image_orig.copy()
for a in ax.ravel():
a.set_axis_off()
plt.tight_layout()
plt.show()
In [55]: h = image_hsv[:,:,0]
s = image_hsv[:,:,1]
v = image_hsv[:,:,2]
y = image_ycbcr[:,:,0]
cb = image_ycbcr[:,:,1]
cr = image_ycbcr[:,:,2]
ShowImage([h, s, v], 1, 3)
ShowImage([y, cb, cr], 1, 3)
ShowImage([h < 10, (cb > 200)], 1, 2)
h = image_hsv[:,:,0]
s = image_hsv[:,:,1]
v = image_hsv[:,:,2]
y = image_ycbcr[:,:,0]
cb = image_ycbcr[:,:,1]
cr = image_ycbcr[:,:,2]
image = SegDataIMG[idx]
image = ResizeImage(image, DesiredWidth = 300, DesiredHeight = 0)
Selected Image :
Index 20
Name KFC 01.jpg
img = image_orig.copy()
for a in ax.ravel():
a.set_axis_off()
plt.tight_layout()
plt.show()
In [73]: h = image_hsv[:,:,0]
s = image_hsv[:,:,1]
v = image_hsv[:,:,2]
y = image_ycbcr[:,:,0]
cb = image_ycbcr[:,:,1]
cr = image_ycbcr[:,:,2]
ShowImage([h, s, v], 1, 3)
ShowImage([y, cb, cr], 1, 3)
ShowImage([s > 150, (cr < 100) & (cr > 50)], 1, 2)
h = image_hsv[:,:,0]
s = image_hsv[:,:,1]
v = image_hsv[:,:,2]
y = image_ycbcr[:,:,0]
cb = image_ycbcr[:,:,1]
cr = image_ycbcr[:,:,2]