1

I am working with ROI and this center element is what I am looking for. But this part of image above I need to remove.

enter image description here

smooth= cv2.GaussianBlur(gray, (3, 3), cv2.BORDER_DEFAULT)
T, thresh = cv2.threshold(smooth, 32, 255, cv2.THRESH_BINARY)

#roi = cv2.selectROI(thresh)
#print(roi)#=(2, 176, 36, 31)
roi_cropped = thresh[176:(176+31),2:(2+36)]

#kernel = np.ones((3, 3), np.uint8)
#erode = cv2.erode(roi_cropped, kernel) 
#show(erode)

contours, hierarchy = cv2.findContours(image=roi_cropped, mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_NONE)

image_copy = img.copy()
cv2.drawContours(image=image_copy[176:(176+31),2:(2+36)], contours=contours, contourIdx=-1, color=(0, 255, 0), thickness=1, lineType=cv2.LINE_AA)
show(image_copy)
user1801745
  • 391
  • 2
  • 18
  • get a bounding box for each contour. check if the bounding box touches the edge of the picture. that is 4 comparisons: box.left == 0? box.left+box.width == width of picture? ... same for y-axis – Christoph Rackwitz Aug 14 '21 at 10:49

2 Answers2

0

It's simple. Check if the bounding box of a contour touches the edges of the picture.

# have `contours` from `roi_cropped`

(height, width) = roi_cropped.shape[:2]

for c in contours:
    (x, y, w, h) = bbox = cv.boundingRect(c)
    touches_edges = (x == 0) or (y == 0) or (x+w == width) or (y+h == height)
    print(touches_edges, ":", bbox)

You can figure out how to erase contours. There are multiple options:

You probably don't even need to erase those blobs. Just ignore them.

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
-1

Here is the code if I don't misunderstand your problem:

output image small image:

import cv2
import numpy as np


# load the image
image = cv2.imread("faces/stacl.png",0)

output = image.copy()

contours, hierarchy = cv2.findContours(image=output, mode=cv2.RETR_TREE, method=cv2.CHAIN_APPROX_NONE)

if len(contours) != 0:
    cv2.drawContours(output, contours, 1, 1, 44) #the key point of the code
#for the small image:
#cv2.drawContours(output, contours, 1, 1, 2)


ret,th=cv2.threshold(output,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
#for the small image:
#kernel=np.ones((2,2),np.uint8)
kernel=np.ones((12,5),np.uint8) 


open=cv2.morphologyEx(th,cv2.MORPH_OPEN,kernel) #bu

dist_transform = cv2.distanceTransform(open,cv2.DIST_L2,5)
cv2.imshow("Result", dist_transform)

cv2.waitKey(0)
AltunE
  • 375
  • 3
  • 10
  • the real image is very small https://ibb.co/t3FzNmv – user1801745 Aug 14 '21 at 01:02
  • I don't understand what do you mean by "real image". Do you wanted to process the image that you've linked in there *ibb.co/t3FzNmv*?. In that case, you could use basically `image=cv2.resize(image,(250,250))` ; or just change the kernel size and contours. I've added code for the small image. – AltunE Aug 14 '21 at 07:13
  • 1
    how does this code check whether a blob/contour touches the edges of the picture? the distance transform appears superfluous, and all you do is apply some morphology that works only in this specific picture. – Christoph Rackwitz Aug 14 '21 at 10:50
  • You're absolutely right, thank you for correcting and pointing my mistake. The bounding box approach that you have pointed is much more efficient for further problems. – AltunE Aug 14 '21 at 11:36