Split number touching each other with openCV

asked 2019-04-09 12:11:56 -0500

MaxTheProg gravatar image

updated 2019-04-11 15:29:33 -0500

Hi, i'm looking to split number on image. My main problem is that the number may touch each other. so all technique with contour found online doesn't work.

Here an exemple of image i'm trying to work with:

C:\fakepath\03.jpg

Here the steps :1. Convert my img into Grayscale 2. Treshold inverted to swap color 3. I try different combo of dilatation 4. I try to find contour with cv2.findContour 5. I get bounding box from contour.

And here an exemple of code i use :

import cv2
import numpy as np
image = cv2.imread('imgs/01_tresh_210.jpg')
#cv2.imshow('i', image)
#cv2.waitKey(0)

# grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow('gray', gray)
cv2.waitKey(0)

# binary
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)
cv2.imshow('thresh', thresh)
cv2.waitKey(0)

# dilation
kernel = np.ones((10, 1), np.uint8)  # values set for this image only - need to change for different images
img_dilation = cv2.dilate(thresh, kernel, iterations=1)
cv2.imshow('dilated', img_dilation)
cv2.waitKey(0)

# find contours
im2, ctrs, hier = cv2.findContours(img_dilation.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

# sort contours
sorted_ctrs = sorted(ctrs, key=lambda ctr: cv2.boundingRect(ctr)[0])

for i, ctr in enumerate(sorted_ctrs):
    # Get bounding box
    x, y, w, h = cv2.boundingRect(ctr)

    # Getting ROI
    roi = image[y:y + h, x:x + w]

    # show ROI
    #cv2.imshow('segment no:'+str(i),roi)
    cv2.imwrite('imgs\\roi\\{}.png'.format(i), roi)
    cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 0), 1)
    #cv2.waitKey(0)

    # save only the ROI's which contain a valid information
    if h > 20 and w > 75:
        cv2.imwrite('roi\\{}.png'.format(i), roi)

cv2.imshow('marked areas', image)
cv2.waitKey(0)
edit retag flag offensive close merge delete

Comments

Please show us what you have tried so far.

Grillteller gravatar imageGrillteller ( 2019-04-11 02:02:42 -0500 )edit

Sure, first i inverse color using treshold_binary_inv 1. Convert my img into Grayscale 2. Treshold inverted to swap color 3. I try different combo of dilatation 4. I try to find contour with cv2.findContour 5. I get bounding box from contour.

So it work for image which have number well separated, but not when number are touching each others.

I added my code on the question

MaxTheProg gravatar imageMaxTheProg ( 2019-04-11 15:09:37 -0500 )edit