Search code examples
pythonopencvocr

How to put the contours in a image numbers? (OCR)


I've been studying CV for a few months now but I ran into a problem on my second project, I needed to remove the noise from a sequence of numbers, in order to apply ocr. I managed to clean it up, but the numbers lost some internal pixels.

See the initial and current final image.

Initial

Final

Code used:

blur = cv2.GaussianBlur(img, (15, 15), 2)
hsv = cv2.cvtColor(blur, cv2.COLOR_BGR2HSV)
lower_gray = np.array([1, 1, 1])
upper_gray = np.array([102, 102, 102])
mask = cv2.inRange(hsv, lower_gray, upper_gray)

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
opened_mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
masked_img = cv2.bitwise_and(img, img, mask=opened_mask)
coloured = masked_img.copy()
coloured[mask == 0] = (255, 255, 255)

gray = cv2.cvtColor(coloured, cv2.COLOR_BGR2GRAY)

des = cv2.bitwise_not(gray)

contour, hier = cv2.findContours(des, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)

for cnt in contour:
    cv2.drawContours(des, [cnt], 0, 255, -1)

#des is the final image

Is there a better way to clean the background for OCR, or maybe close the lost pixels in the characters?


Solution

  • It is easy to get a clean background by morphological dilation or closing, a smooth outline by Gaussian filtering, and then binarization. But I found no way to separate the characters.

    enter image description here