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 Answers1

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)

user2518618
  • 1,360
  • 13
  • 32
  • 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. –  Jan 30 '18 at 08:21
  • 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. – user2518618 Jan 30 '18 at 11:22
  • 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. – user2518618 Jan 30 '18 at 11:26