I am working on MIAS dataset and for preprocessing I tried to remove the labels that appear on some several images. My approach does work on some images and not on others, can't figure out why... I am using binary threshold and then keep the biggest contour using the mask created:
x = 4
y = 4
for i in range(1, 5):
fig = plt.figure(figsize = (25, 15))
img = cv2.imread(img_add[i], 0) #img_add is an array of image addresses
ax = plt.subplot(x, y, (i - 1) * 4 + 1)
plt.title("orig")
plt.imshow(img, cmap="gray")
ret, thresh1 = cv2.threshold(img, 8, 255, cv2.THRESH_BINARY)
ax = plt.subplot(x, y, (i - 1) * 4 + 2)
plt.title("thresh")
plt.imshow(thresh1, cmap="gray")
im, contours, hierarchy = cv2.findContours(thresh1,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE )
maxContour = 0
for contour in contours:
contourSize = cv2.contourArea(contour)
if contourSize > maxContour:
maxContour = contourSize
maxContourData = contour
mask = np.zeros_like(thresh1)
cv2.fillPoly(mask,[maxContourData],1)
ax = plt.subplot(x, y, (i - 1) * 4 + 3)
plt.title("mask")
plt.imshow(mask, cmap="gray")
img = cv2.bitwise_and(img, img, mask = mask)
ax = plt.subplot(x, y, (i - 1) * 4 + 4)
plt.title("final")
plt.imshow(img, cmap="gray")
It worked on images 1 & 3 but not on 2 & 4
Can someone please tell me what I can do to make this better and work on all images as intended? Seeing the binarised images I feel like the contouring should work properly but I guess it isn't able to identify the label and the breast as separate contours on some images even though they are not connected.