Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Count objects which have border connected

Hi,

I am trying to count number of objects in given image but they are touched with each other and I am not able to sperate them. I have tried different combinations but no success.

Can any one please guide me in it?

I have tried following code at last but no success.

import cv2
import numpy as np
img = cv2.imread('/media/sf_Personal_Data/Workspace/objectextract/IMAGE.jpg')
blur=cv2.blur(img,(3,3))
#cv2.imwrite(blur,"Step 1 Blur.jpg")
th,th_img = cv2.threshold(img,80,255,cv2.THRESH_BINARY)

kernel=cv2.getStructuringElement(cv2.MORPH_RECT,(2,2))
close_img = cv2.morphologyEx(th_img, cv2.MORPH_CLOSE, kernel)
cv2.imshow('Closed',close_img)
erode_img = cv2.erode(close_img,kernel,iterations = 1)
cv2.imshow('Erdoed',erode_img)
#print closing.dtype

gray=cv2.cvtColor(erode_img,cv2.COLOR_BGR2GRAY)

(cimg,contours,h) = cv2.findContours(gray,cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
  cv2.drawContours(img,[cnt],0,(0,0,255),1)

cv2.imshow('Org',img)
cv2.waitKey()

Sample Image

Input Image