Lab07 Object Segmentation
Lab07 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 [200]: 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 [205]: 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
image_index = image_gray.copy()
for idx, value in enumerate(PixelList):
image_index = np.where(image_index==PixelList[idx], -(idx + 1), image_index)
image_index = -image_index
image_color = np.dstack((image_gray,image_gray,image_gray))
rows, cols = image_color.shape[:2]
for x in range(rows):
for y in range(cols):
index = PixelList.index(image_gray[x,y])
image_color[x, y, :] = colors[index]
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()
step = int(len(contours[0])/n)
points = np.array(contours[0][0])
if step == 0:
step = 5
for i in range (0, len(contours[0]), step):
points = np.append((points), (contours[0][i]), axis=0)
if len(points) > n:
np.delete(points, 0)
return points
In [209]: DataPath = "D:\\MSI DATA (Previous Computer)\\Teaching And Training\\Image Segmentation\\Image Segnemtation D
ataSet\\"
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)
SegDataIMG1 = IMG.copy()
SegDataName1 = all_names
Number of Images: 60
image = SegDataIMG1[idx]
image = ResizeImage(image, DesiredWidth = 300, DesiredHeight = 0)
Selected Image :
Index 55
Name Skin 01.jpg
ShowImage([segments, image_color], 1, 3)
ShowImage([image_mask], 1, 1)
ShowImage([image_output], 1, 1)
XList : [189, 189, 184, 182, 185, 190, 196, 202, 206, 206, 202, 196, 190]
YList : [87, 87, 92, 98, 104, 108, 110, 107, 101, 95, 89, 87, 87]
ShowImage([image_mask], 1, 1)
ShowImage([image_output], 1, 1)
XList : [139, 139, 130, 121, 120, 128, 137, 146, 155, 159, 153, 144]
YList : [113, 113, 115, 121, 130, 135, 138, 138, 132, 123, 116, 113]
image = SegDataIMG1[idx]
image = ResizeImage(image, DesiredWidth = 300, DesiredHeight = 0)
Selected Image :
Index 44
Name Hand Gesture 05.jpg
ShowImage([segments, image_color], 1, 3)
MaskList.append(image_mask)
ImageList.append(image_check)
In [220]: ImageList = []
MaskList = []
image_check = image_color.copy()
image_mask = image_gray * 0
ShowImage(MaskList, 2, 3)
ShowImage(ImageList, 2, 3)
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
ShowImage([image_output], 1, 1)
XList : [167, 167, 164, 138, 133, 141, 157, 184, 190, 196, 197, 171]
YList : [22, 22, 38, 28, 56, 88, 120, 132, 101, 69, 37, 22]