简体   繁体   中英

character detection and crop from an image using opencv python

I have a project in which I have to detect Bengali numbers from image. I decided to do an experiment like numbers with spaces and without spaces. My python program can detect all number from with spaces image.

The problem occurred when I gave image without spaces. It couldn't cut number smoothly like the previous one.

here is my code

import cv2

image = cv2.imread("number.png")
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY) 
_,thresh = cv2.threshold(gray,70,255,cv2.THRESH_BINARY_INV) 
kernel = cv2.getStructuringElement(cv2.MORPH_CROSS,(3,3))
dilated = cv2.dilate(thresh,kernel,iterations = 0) 
_,contours, hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE) 

i=5
for contour in contours:

    [x,y,w,h] = cv2.boundingRect(contour)

    cv2.imwrite(str(i)+".jpg",image[y:y+h,x:x+h])
    i=i+1

At first I used dilated in for finding contours but it didn't work for number without space image. Then I use directly thresh output and after that I got most of the numbers but I couldn't cut them perfectly because contour area detect number with some portion of other number. Though it didn't have space in 2nd image but still 2 numbers didn't touch them each other. So why the output like this?

With Space:

有空间

Without Space:

没有空间

Unfortunately I didn't notice that when I cut rectangle portion I added x:x+h instead of x:x+w. That's the main problem. After modifying that the program worked fine. sorry.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM