Reputation:
import cv2
import numpy as np
im_m = cv2.imread("primary.png", cv2.IMREAD_GRAYSCALE)
# 50, 130
# Otsu's thresholding after Gaussian filtering
blur = cv2.GaussianBlur(im_m,(25,25),0)
#(thresh, im2) = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# Black region overflowing into digit regions with otsu
(thresh, im) = cv2.threshold(im_m, 55, 130, cv2.THRESH_BINARY)
kernel = np.ones((5,5), np.uint8)
img_dilation = cv2.dilate(im, kernel, iterations=1)
img_erosion = cv2.erode(img_dilation, kernel, iterations=1)
params = cv2.SimpleBlobDetector_Params()
params.minThreshold = 10;
params.maxThreshold = 225;
params.filterByArea = True
params.minArea = 100
params.maxArea = 1500
params.filterByCircularity = False
params.minCircularity = 0.5
params.filterByConvexity = False
params.minConvexity = 0.2
params.filterByInertia = False
params.minInertiaRatio = 0.01
ver = (cv2.__version__).split('.')
if int(ver[0]) < 3 :
detector = cv2.SimpleBlobDetector(params)
else :
detector = cv2.SimpleBlobDetector_create(params)
keypoints = detector.detect(im)
im_key = cv2.drawKeypoints(im, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow("Keypoints", im_key)
cv2.imshow("Erosion", img_erosion)
cv2.imshow("Dilation", img_dilation)
cv2.waitKey(0)
I am pretty new to image processing with opencv. I am trying to detect digits from a noisy image.
I did some experiments, tuning thresholds and filtering by parameters but can't detect the blobs with 3, 4, 0 and sometimes 5, 7 and 2. I just need to detect the digits. Are there any other better way than blob detection for this task?
Upvotes: 0
Views: 1027
Reputation: 1408
In general you should try with different values for most of the parameters. I usually have a GUI with some scrollbars in order to modify the parameters interactively.
In you case, I guess that the maxarea is too small. You can make it 10000 and if it works, make it smaller until it starts to fail. If it doesn't work you can try making the minarea smaller.
I think that you don't have other parameters, but if you did, you should disable all filters, make all ranges wider and then enable and narrow them down one by one (always looking at your image results)
Upvotes: 0