#!/usr/bin/python
# Standard imports
import cv2
import numpy as np;
# Read image
#im = cv2.imread("blob.jpg", cv2.IMREAD_GRAYSCALE)
cap=cv2.VideoCapture(0)
params = cv2.SimpleBlobDetector_Params()
# Change thresholds
params.minThreshold = 10
params.maxThreshold = 200
# Filter by Area.
params.filterByArea = True
params.minArea = 1200
# Filter by Circularity
params.filterByCircularity = True
params.minCircularity = 0.1
# Filter by Convexity
params.filterByConvexity = True
params.minConvexity = 0.87
# Filter by Inertia
params.filterByInertia = True
params.minInertiaRatio = 0.01
while True :
sucess,img=cap.read()
#im=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
im= cv2.convertScaleAbs(img)
# Setup SimpleBlobDetector parameters.
# Create a detector with the parameters
ver = (cv2.__version__).split('.')
if int(ver[0]) < 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)
text="blobs:%d"%(len(keypoints))
ret = cv2.putText(im_with_keypoints, text, (5, 50), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (100, 200, 200), 2)
# Show blobs
cv2.imshow("Keypoints", ret)
cv2.waitKey(1)