简体   繁体   English

使用opencv python从图像进行字符检测和裁剪

[英]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. 我的python程序可以使用空格图像检测所有数字。

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. 尽管它在第二张图像中没有空格,但仍有2个数字彼此不接触。 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. 不幸的是,我没有注意到当我切割矩形部分时我添加了x:x + h而不是x:x + w。 That's the main problem. 那是主要问题。 After modifying that the program worked fine. 修改后,程序运行正常。 sorry. 抱歉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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