0
$\begingroup$

Let say you are given this image and want to detect and recognize the digits printed on the fuses.

for all CV-operations i used OpenCV 3.4.4 and Python I did already these things:

enter image description here

Detection:

1) Thresholding + Canny (using OpenCV 3.4.4): -> Best thresholding result i could achieve was by using Binary or adaptive-gaussian. Here is the result:

enter image description here

As you can see two didigt are really clear segmented out but the remaining are mixed wihth the edges of the fuse.

2) My second attmept was just using Canny-Edge-Detector

enter image description here

The result is somehow better, but still not really perfect.

Recognition

Here i tried to just feed the tesseract with the above images, but the output was horrible. Not even one digit was recognized.

Any suggestion, hint or link is welcome, to get the digits detected and recognized correctly.

Here is my code right now: import cv2 import numpy as np

def canny_threshold(val):
    global im_gray, im
    im_tmp = im_gray.copy()
    low_thresh = cv2.getTrackbarPos("Min Thresh", "main")
    max_thresh = cv2.getTrackbarPos("Max Thresh", "main")
    im_edges = cv2.Canny(im_tmp, low_thresh, low_thresh * 3)
    mask = im_edges != 0
    dst = im * (mask[:,:,None].astype(im.dtype))
    cv2.imshow("main", dst)

# read and prepare and mask image
lower_range = np.array([0, 0, 110])
upper_range = np.array([180, 115, 255])

im = cv2.imread("./resources/img6_s.jpg")
# convert color and cull out of range colors
#im_hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)
#mask = cv2.inRange(im_hsv, lower_range, upper_range)
#im = cv2.bitwise_and(im, im, mask=mask)

# successor functions need a gray image, create it here
im_gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
im_gray = cv2.GaussianBlur(im_gray, (9, 9), 0)

# We know the position of the first fuse and the difference between them
# calculate a mask and apply it to the gray image
mask = np.zeros_like(im_gray)
nx = 60
ny = 55
block_init_pos = [256, 117]
block_diff_x = 510
block_diff_y = 260
for i in range(0, 2):
    for j in range(0, 3):
        if (i * 4) + j == 12:
            continue
        idx_x = block_init_pos[0] + block_diff_x * i
        idx_y = block_init_pos[1] + block_diff_y * j
        mask[idx_y - ny: idx_y + ny, idx_x - nx: idx_x + nx] = 255

# apply mask to the image
im_gray = cv2.bitwise_and(im_gray, im_gray, mask=mask)
ret, im_gray = cv2.threshold(im_gray, 120, 255, cv2.THRESH_BINARY)
#im_gray = cv2.adaptiveThreshold(im_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 7, 2)
cv2.imshow("im_gray", im_gray)

cv2.namedWindow("main")
cv2.createTrackbar("Min Thresh", "main", 0, 100, canny_threshold)
cv2.createTrackbar("Max Thresh", "main", 0, 255, canny_threshold)
canny_threshold(0)
while True:
    k = cv2.waitKey(1)
    if k == ord('q'):
        cv2.destroyAllWindows()
        break
exit(0)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.