1

I'm trying to segment the numbers and/or characters of the following image then converting each individual num/char to text using ocr:

enter image description here

This is the code (in python) used:

new, contours, hierarchy = cv2.findContours(gray, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)

digitCnts = []

final = gray.copy()    

# loop over the digit area candidates
for c in contours:

    (x, y, w, h) = cv2.boundingRect(c)
    # if the contour is sufficiently large, it must be a digit
    if (w >= 20 and w <= 290) and h >= (gray.shape[0]>>1)-15:
        x1 = x+w
        y1 = y+h
        digitCnts.append([x,x1,y,y1])
        #print(x,x1,y,y1)
        # Drawing the selected contour on the original image
        cv2.rectangle(final,(x,y),(x1,y1),(0, 255, 0), 2)

plt.imshow(final, cmap=cm.gray, vmin=0, vmax=255)

I get the following output:

enter image description here

You see that all are detected correctly except the middle 2 with only the top part has bounding box on it and not around the whole digit. I cannot figure out why only this one not detected correctly especially that it is similar to the others. Any idea how to resolve this?

4
  • Try to draw the contours, to see what it extracts. Oct 24, 2018 at 21:26
  • I drawed the bouding rect. I draw contours instead? I'll try and see
    – mj1261829
    Oct 24, 2018 at 21:28
  • 1
    The image should be binary, not gray, if it's gray better convert it to a black and white with some threshold. Oct 24, 2018 at 21:32
  • Well it is binary. Actually, I resolved the problem when I delayed the image to binary conversion just before trying to detect. The Image was initially rotated after the conversion. Maybe, the rotation after binary conversion has more effect on digit detection than before. Maybe I need to just to always delay the binary conversion to the end.
    – mj1261829
    Oct 24, 2018 at 21:49

2 Answers 2

1

As far as I know, most of OpenCV methods for binary images operate white objects on the black background.

Src:

enter image description here

Threahold INV and morph-open:

enter image description here

Filter by height and draw on the src:

enter image description here


#!/usr/bin/python3
# 2018/10/25 08:30 
import cv2
import numpy as np

# (1) src 
img = cv2.imread( "car.png")
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# (2) threshold-inv and morph-open 
th, threshed = cv2.threshold(gray, 100, 255, cv2.THRESH_OTSU|cv2.THRESH_BINARY_INV)
morphed = cv2.morphologyEx(threshed, cv2.MORPH_OPEN, np.ones((2,2)))
# (3) find and filter contours, then draw on src 
cnts = cv2.findContours(morphed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]

nh, nw = img.shape[:2]
for cnt in cnts:
    x,y,w,h = bbox = cv2.boundingRect(cnt)
    if h < 0.3 * nh:
        continue
    cv2.rectangle(img, (x,y), (x+w, y+h), (255, 0, 255), 1, cv2.LINE_AA)

cv2.imwrite("dst.png", img)
cv2.imwrite("morphed.png", morphed)
0

Your image is a bit noisy, therefore binarizing it would do the trick.

cv2.threshold(gray,  127, 255, cv2.THRESH_BINARY, gray)
new, contours, hierarchy = cv2.findContours(gray, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)

# cv2.drawContours(gray, contours, -1, 127, 5)
digitCnts = []

final = gray.copy()

# loop over the digit area candidates
for c in contours:
    (x, y, w, h) = cv2.boundingRect(c)
    # if the contour is sufficiently large, it must be a digit
    if (w >= 20 and w <= 290) and h >= (gray.shape[0]>>1)-15:
        x1 = x+w
        y1 = y+h
        digitCnts.append([x,x1,y,y1])
        #print(x,x1,y,y1)
        # Drawing the selected contour on the original image
        cv2.rectangle(final,(x,y),(x1,y1),(0, 255, 0), 2)

enter image description here

2
  • Yah it is bit noisy. Although I tried to remove the noise first, it did not work. I already resove it differently. Thanks, however.
    – mj1261829
    Oct 24, 2018 at 21:55
  • @mj1261829 well done. If your own answer solves your problem in a better way - you are encouraged to add solution as an answer and mark it as an accepted one.
    – Dmitrii Z.
    Oct 24, 2018 at 23:18

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.