0

I am working on a project and I would like to know if it was possible to detect object in a picture that has the same color as the background. Like this :

1

I would like to detect every units from this pfd without lines between them. Because some of my codes detects this lines as object since it makes loops.

I tried a black pixel concentration code on the image but I could not have good results. I also tried to use gaussian blur and threshold but it was worse (code below). This code comes from another topic (How to detect an object that blends with the background?). I tried to change k1,s1,k2 and s2 to find good ones but it was no success.

img = cv2.imread(path0, cv2.IMREAD_UNCHANGED)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Function to perform Difference of Gaussians
def difference_of_Gaussians(img, k1, s1, k2, s2):
    b1 = cv2.GaussianBlur(img,(k1, k1), s1)
    b2 = cv2.GaussianBlur(img,(k2, k2), s2)
    return b1 - b2
DoG_img = difference_of_Gaussians(gray, i, i, j, k)
# Applying Otsu Threshold and finding contours
th = cv2.threshold(DoG_img ,127,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)[1]
contours, hierarchy = cv2.findContours(th, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

# Create copy of original image
img1 = img.copy()
if len(contours)>7:
# for each contour above certain area and extent, draw minimum bounding box  
for c in contours:
      area = cv2.contourArea(c)
      if area > 1500:
            x,y,w,h = cv2.boundingRect(c)
            extent = int(area)/w*h              
            if extent > 2000:
                   rect = cv2.minAreaRect(c)
                   box = cv2.boxPoints(rect)
                   box = np.intp(box)
                   cv2.drawContours(img1,[box],0,(0,255,0),4)
Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36

0 Answers0