Count the number of objects OpenCV - Python
<p>This is my first project in Python(3.5.1) and OpenCV (3), so I'm sorry for my mistakes.
I've some pictures like these ones:
<a href="https://s12.postimg.org/ox8gw5l8d/gado.jpg" rel="nofollow">https://s12.postimg.org/ox8gw5l8d/gado.jpg</a></p>

<p>I need to count how many white objects has on this image. I tried to ise SimpleBlobDetector but I didn't work as I was expecting.</p>

<pre><code># Standard imports
import cv2
import numpy as np;

# Read image
im = cv2.imread("C:/opencvTests/cattle.jpg", cv2.IMREAD_GRAYSCALE)

# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()

#filter by color
params.filterByColor = True
params.blobColor = 255

# Filter by Convexity
params.filterByConvexity = True
params.minConvexity = 0.87

# Filter by Inertia
params.filterByInertia = True
params.minInertiaRatio = 0.08

# Create a detector with the parameters
ver = (cv2.__version__).split('.')
if int(ver[0]) &lt; 3 :
   detector = cv2.SimpleBlobDetector(params)
else : 
detector = cv2.SimpleBlobDetector_create(params)

# Detect blobs.
keypoints = detector.detect(im)

# Draw detected blobs as red circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the circle        corresponds to the size of blob
im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]),   (0,0,255),     cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

# Show keypoints
cv2.imwrite("C:/opencvTests/blobSave.jpg",im_with_keypoints)
print("Total of objects")
print(len(keypoints))
</code></pre>

<p>Any help would be really appreciate! Thanks in advance </p>

<p>I'm getting very close to the answer, I believe I just need to change some parameters according to the image.
If someone needs something on this regard, this is my code:</p>

<pre><code># Standard imports
import cv2
import numpy as np;

# Read image
im = cv2.imread("C:/opencvTests/original.jpg", cv2.IMREAD_GRAYSCALE)

#Apply treshold
ret,im = cv2.threshold(im,240,255,cv2.THRESH_BINARY)

kernel = np.ones((6,6),np.uint8)
erosion = cv2.erode(im,kernel,iterations = 1)
opening = cv2.morphologyEx(im, cv2.MORPH_OPEN, kernel)
im = cv2.morphologyEx(opening, cv2.MORPH_CLOSE, kernel)


# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()

#filter by color
params.filterByColor = True
params.blobColor = 255

# Filter by Convexity
params.filterByConvexity = True
params.minConvexity = 0.87

# Filter by Inertia
params.filterByInertia = True
params.minInertiaRatio = 0.08

# Create a detector with the parameters
ver = (cv2.__version__).split('.')
if int(ver[0]) &lt; 3 :
    detector = cv2.SimpleBlobDetector(params)
else : 
    detector = cv2.SimpleBlobDetector_create(params)

# Detect blobs.
keypoints = detector.detect(im)

# Draw detected blobs as red circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures the size of the circle        corresponds to the size of blob
im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]),   (0,0,255),     cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

# Show keypoints
cv2.imwrite("C:/opencvTests/keypoints.jpg",im_with_keypoints)
print("Total of objects")
print(len(keypoints))
</code></pre>

<p>Thank you very much!</p>

python opencv