0
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.

Input 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?

Output Image

1
  • It looks like you are only filtering by area. I guess that your params.maxarea is too small. Try with 10000. Commented Jan 29, 2018 at 20:55

1 Answer 1

0

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)

Sign up to request clarification or add additional context in comments.

3 Comments

I tried disabling all filters and making minArea = 0 and maxArea really big, but still I can't detect these digits. Instead I get some big random blobs.
Can you try with several values? for example, leave minarea=100 and then try changing slightly maxarea (values 1500, 1550, 1600,...) and see if the results get better. Once you have found the optimal maxarea, try changing slightly the minarea.
If you could create a simple GUI with scrollbars for minarea and maxarea, it would be much easier. You can add scrollbars for minthreshold and maxthreshold, which may be important.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.