1 | initial version |
If you want inner circles, but not outer circles. Code:
#!/usr/bin/env python3
#Raspberry pi 3B/+, OpenCV 4.1.0
#Date: 29th April, 2019
import numpy as np
import cv2
img = cv2.imread('SimpleBlobDetector.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blurred = cv2.medianBlur(gray, 5)
_filter = cv2.bilateralFilter(blurred, 9, 75, 75)
adap_thresh = cv2.adaptiveThreshold(_filter, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY_INV, 21, 0)
element = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
dilated = cv2.dilate(adap_thresh, element, iterations=1)
# blob detection
params = cv2.SimpleBlobDetector_Params()
params.filterByColor = False
params.blobColor = 0
params.filterByArea = False
params.minArea = 500
params.maxArea = 5000
params.filterByCircularity = False
params.minCircularity =.4
params.maxCircularity = 1
det = cv2.SimpleBlobDetector_create(params)
keypts = det.detect(dilated)
im_with_keypoints = cv2.drawKeypoints(dilated, keypts, np.array([]),
(255, 0, 0), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imwrite('blob.png', im_with_keypoints)
cv2.imshow("Keypoints", im_with_keypoints)
cv2.waitKey(0)
Output:
Secondly,w/out SimpleBlobDetector:
#!/usr/bin/env python3
#Raspberry pi 3B/+, OpenCV 4.1.0
#Date: 29th April, 2019
import cv2
# read original image
img = cv2.imread('SimpleBlobDetector.jpg')
# create binary image
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (5, 5), 0)
(t, binary) = cv2.threshold(blur, 100, 255, cv2.THRESH_BINARY)
# find contours
contours, _ = cv2.findContours(binary, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# draw contours over original image
cv2.drawContours(img, contours, -1, (0, 0, 255), 5)
# display original image with contours
cv2.imwrite('blop1.jpg', img)
cv2.imshow("output", img)
cv2.waitKey(0)
Output: