python-3.x - 从图像中的单词中提取字符

标签 python-3.x opencv image-processing roi

我有一个提取的图像作为extracted image,我想裁剪并提取该图像中的各个字母。

我已经尝试了下面的代码,但是它只适用于像name with gap between letters这样写的图像名称,我一次得到的结果是单个字母。

import cv2
import numpy as np

img = cv2.imread('data1/NAME.png')

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh1 = cv2.threshold(gray,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)

kernel = np.ones((3, 3), np.uint8)
imgMorph = cv2.erode(thresh1, kernel, iterations = 1)

contours, hierarchy = cv2.findContours(imgMorph,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

i=1
for cnt in contours:
    x,y,w,h = cv2.boundingRect(cnt)

    if w>10 and w<100 and h>10 and h<100:
        #save individual images
        cv2.imwrite("data1/NAME_{}.png".format((i)),thresh1[y:y+h,x:x+w])
        i=i+1

cv2.imshow('BindingBox',imgMorph)
cv2.waitKey(0)
cv2.destroyAllWindows()

该代码给出以下结果
result1

result2

预期结果expected2expected2像这样。

最佳答案

当公用线与其他字母一样粗时,您无法使用形态学运算来分离触摸或重叠的字母。

您无法分割字母,但是可以使用机器学习等高级OCR技术识别它们。

阅读此http://www.how-ocr-works.com/OCR/word-character-segmentation.html

关于python-3.x - 从图像中的单词中提取字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59065556/

相关文章:

python Pandas : compare two data-frames along one column and return content of rows of both data frames in another data frame

c++ - 处理 OpenCV 中的序列?

c++ - OpenCV:如何绘制一条线,其颜色相对于应绘制的表面相反?

用于图像编辑的 Java 书,例如亮度/对比度等(新手友好)

python - 向 pandas 绘图添加小刻度

python-3.x - 计数向量化器和拟合函数的 Python 列表错误

Python 列表中浮点的成员资格

python - 相机重新对焦时Opencv读取图像(仍然模糊)

c++ - 在 C++ 中读取/操作图像

c++ - 如何在 OpenCV 中对图像进行阈值处理?