Pet*_*r S 5 python opencv image image-processing computer-vision
我目前正在开展一个项目,需要处理 OCR 图像。我设置并安装了过滤器,以使 OCR 的工作尽可能简单,但图像的一个方面我不知道如何修复。在包含的图像中,您可以看到我正在尝试阅读的文本(“PRTraining Tissue...”),并且图像周围有一个黑色边框,需要将其删除才能使我的倾斜校正代码正常工作。有没有什么简单的方法可以快速用白色填充这个黑色边框而不影响文本?
未过滤的图像:
过滤后的图像:
我已经编写了一些代码来删除大部分背景,但大黑点仍然保留为边框。包含的代码是我的图像裁剪脚本,它删除了大部分图像黑色边框并尝试尽可能地隔离文本,但不幸的是,它仍然留下相当多的黑色,与我的倾斜校正脚本混淆。
def boarderRemoval(img):
"""
Takes in a numpy array and crops the image down to isolate the text (Still leaves a small black border that varys from image to image\n
Vars:\n
- img <- numpy array of the label\n
Returns:\n
- Cropped down image with smaller black borders
"""
contours, hierarchy = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
x,y,w,h = cv2.boundingRect(cnt)
correctedImage = img[y: y + h, x: x + w]
return correctedImage
Run Code Online (Sandbox Code Playgroud)
从过滤后的图像开始,这是一个简单的方法
转换为灰度后,我们找到要保留的主要轮廓,并将该部分绘制到蒙版上。然后,我们反转蒙版以获得该图像,该图像代表填充白色的所需边框部分
现在我们只需cv2.bitwise_or()
使用原始图像即可得到我们的结果
import cv2
import numpy as np
image = cv2.imread('1.png')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
mask = np.zeros(image.shape, dtype=np.uint8)
cnts = cv2.findContours(gray, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cv2.fillPoly(mask, cnts, [255,255,255])
mask = 255 - mask
result = cv2.bitwise_or(image, mask)
cv2.imshow('mask', mask)
cv2.imshow('result', result)
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3136 次 |
最近记录: |