Ask Your Question
0

Count objects which have border connected

asked 2017-10-29 03:40:23 -0600

pmsomani gravatar image

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

edit retag flag offensive close merge delete

Comments

1 answer

Sort by ยป oldest newest most voted
-2

answered 2017-10-29 09:01:09 -0600

supra56 gravatar image

updated 2017-10-29 09:11:41 -0600

Here is counting object:

#!/usr/bin/python35
#OpenCV 3.3.1
#Date: 29th October, 2017

import cv2
import numpy as np

src = cv2.imread('board.jpg')
img = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY)
img = cv2.medianBlur(img, 5)
cimg = src.copy() # numpy function

circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT, 1, 10, np.array([]), 100, 30, 1, 30)    

![image description](/upfiles/15092856528003828.jpg)if circles is not None: # Check if circles have been found and only then iterate over these and add them to the image
a, b, c = circles.shape
for i in range(b):
    cv2.circle(cimg, (circles[0][i][0], circles[0][i][1]), circles[0][i][2], (0, 0, 255), 3, cv2.LINE_AA)
    cv2.circle(cimg, (circles[0][i][0], circles[0][i][1]), 2, (0, 255, 0), 3, cv2.LINE_AA)  # draw center of circle of green
    print(i)

cv2.imshow("detected circles", cimg)
cv2.imshow("source", src)
cv2.waitKey(0)

C:\fakepath\board.jpg

edit flag offensive delete link more

Comments

In board, can count 40 circles. But in coin.jpg, I merely counted up to o 15. because of poor lightning.

supra56 gravatar imagesupra56 ( 2017-10-29 09:08:25 -0600 )edit

you can't solve this problem with HoughCircles

sturkmen gravatar imagesturkmen ( 2017-10-29 15:46:37 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-10-29 03:40:23 -0600

Seen: 2,610 times

Last updated: Oct 29 '17