python - 如何在opencv(阈值)中改变轮廓形成的区域

标签 python opencv ocr tesseract opencv-contour

我正在尝试构建 OCR 以从图像中提取文本,我正在使用轮廓来形成文本字符的边界,

经过几次更改 cv2.threshold 的试验后,我在形成文本字符的边界时得到了最适合的轮廓。

#files = os.listdir(r'letters/harry.jpeg',0)
file = r'/home/naga/Documents/Naga/Machine Learning/Data_extract/letters/Harry/Harry Potter and the Sorcerer s Stone-page-006.jpg'
im1 = cv2.imread(file,0)
im = cv2.imread(file)

# ret,thresh1 = cv2.threshold(im1,180,278,cv2.THRESH_BINARY)
# _,contours, hierarchy = cv2.findContours(thresh1,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

ret,thresh1 = cv2.threshold(im1,180,278,cv2.THRESH_BINARY)
kernel = np.ones((5,5),np.uint8)
dilated = cv2.dilate(im1,kernel,iterations = 1)
_,contours, hierarchy = cv2.findContours(dilated,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

for cnt in contours:
    x,y,w,h = cv2.boundingRect(cnt)
    #bound the images
    cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),1)

cv2.namedWindow('BindingBox', cv2.WINDOW_NORMAL)
cv2.imwrite('output2/BindingBox4.jpg',im)

Contours on text with Opencv

现在我想在单词上创建轮廓。我需要每个单词的父轮廓。 Open cv 中要更改的属性是什么?

我是opencv的新手,我跟着cv2 threshold但无法理解应用它。请在形成单词轮廓时提供您的输入。

original pic

最佳答案

一个简单的解决方案是在运行 findcontour 函数之前扩大阈值图像的结果。

ret,thresh1 = cv2.threshold(im1,180,255,cv2.THRESH_BINARY_INV)
kernel = np.ones((5,5),np.uint8)
dilated = cv2.dilate(thresh1,kernel,iterations = 2)
contours, hierarchy = cv2.findContours(dilated,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)

膨胀是一种形态函数,可增加二进制 Blob 的面积。它将倾向于将所有附近的 blob 组合成一个单一的 blob,这正是您将文本组合成单词所需要的。

如果不是所有文本都合并为一个单词,则可以增加迭代次数。如果您不确定此处使用的值,则需要进行一些试验和错误。

阅读形态学过程以更好地理解该主题。它是基本图像处理的有用工具。

作为一个额外的提示,尝试在 openCV 中搜索函数adaptivethreshold。在对文本图像进行二值化时,它会让您的生活更轻松。

关于python - 如何在opencv(阈值)中改变轮廓形成的区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52352027/

相关文章:

python - 不跳过的质量错误检查

opencv - 光圈的变化会影响相机校准吗?

python - "Adding"Tesseract eng.traineddata 的新字体

python - 为什么如果我用魔杖从 pdf 中提取图像 jpg,它会将文本变成黑色背景

python - 当两点之间存在视线时,如何计算父节点和邻居节点之间的距离?

Python 正则表达式 - 使用先前匹配的字符来匹配字符序列

python - 在 Django rest 框架中处理请求 header 以获取 header 中传递的 key ?

c++ - Visual Studio 2012 错误 : The application was unable to start correctly (0xc0000007b)

python - 在OpenCV中裁剪垂直轮廓,获取边框边界而不是区域

java - 上传文件到 ocr.space api