当前位置:   article > 正文

For OCR | 基于 OpenCV 的图像预处理

opencv ocr前的预处理

欢迎关注 “小白玩转Python”,发现更多 “有趣”

如果你的图像有随机噪声,不均匀的光照,前面的物体上的洞,等等。在将图片发布到计算机视觉 API 之前,有几件事情是你可以做的。在本文中,我们将介绍几种使用 OpenCV 提高 OCR 处理效果的技术。

安装 OpenCV

使用首选的包管理器安装 OpenCV

  1. conda install -c conda-forge opencv
  2. pip install opencv-python

读取图片

我将以我最喜欢的一本书的第一版的封面为例。让我们首先读取图像,指定要读取图像的颜色类型,这将读取图像的默认颜色格式为 OpenCV 中的 BGR (即蓝绿红)。然后我们将颜色空间转换为更常见的 RGB 顺序(为了可视化) ,最后,编写一个小函数来显示图像:

  1. import cv2
  2. import matplotlib.pyplot as plt
  3. img = cv2.imread(
  4. filename='hands-on-machine-learning.jpg',
  5. flags=cv2.IMREAD_COLOR,
  6. )
  7. h, w, c = img.shape
  8. print(f'Image shape: {h}H x {w}W x {c}C')
  9. img = cv2.cvtColor(
  10. src=img,
  11. code=cv2.COLOR_BGR2RGB,
  12. )
  13. def show_image(img, **kwargs):
  14. """
  15. Show an RGB numpy array of an image without any interpolation
  16. """
  17. plt.subplot()
  18. plt.axis('off')
  19. plt.imshow(
  20. X=img,
  21. interpolation='none',
  22. **kwargs
  23. )
  24. show_image(img)

裁剪图像

大多数情况下,您要么在文本周围有一个框坐标(来自标签工具) ,要么您只对图像的一部分感兴趣。我们的图像是一个 3D numpy 数组。要裁剪它,我们可以简单地沿着高度和宽度切片:

  1. ymin, ymax = 200, 780
  2. xmin, xmax = 100, 1000
  3. img = img[
  4. int(ymin): int(ymax),
  5. int(xmin): int(xmax),
  6. ]
  7. h, w, c = img.shape
  8. print(f'Image shape: {h}H x {w}W x {c}C')
  9. show_image(img)

扩充边界

这对于使用针对文档进行 OCR 模型训练的 API (文档通常有白色边框)可能很有用

  1. img_border = cv2.copyMakeBorder(
  2. src=img,
  3. top=10,
  4. bottom=10,
  5. left=10,
  6. right=10,
  7. borderType=cv2.BORDER_CONSTANT,
  8. value=(255, 255, 255),
  9. )
  10. h, w, c = img_border.shape
  11. print(f'Image shape: {h}H x {w}W x {c}C')
  12. show_image(img_border)

调整图像大小

API 将为输入图像设置一个最大尺寸。如果图像需要调整大小,应该保留长宽比

  1. MAX_PIX = 800
  2. def resize_image(img, flag):
  3. """
  4. Resize an RGB numpy array of an image, either along the height or the width, and keep its aspect ratio. Show restult.
  5. """
  6. h, w, c = img.shape
  7. if flag == 'h':
  8. dsize = (int((MAX_PIX * w) / h), int(MAX_PIX))
  9. else:
  10. dsize = (int(MAX_PIX), int((MAX_PIX * h) / w))
  11. img_resized = cv2.resize(
  12. src=img,
  13. dsize=dsize,
  14. interpolation=cv2.INTER_CUBIC,
  15. )
  16. h, w, c = img_resized.shape
  17. print(f'Image shape: {h}H x {w}W x {c}C')
  18. show_image(img_resized)
  19. return img_resized
  20. if h > MAX_PIX:
  21. img_resized = resize_image(img, 'h')
  22. if w > MAX_PIX:
  23. img_resized = resize_image(img, 'w')

图像形态学操作

  • Opening:腐蚀后使用 5×5 的内核膨胀操作,有助于消除噪声

  • Closing:膨胀后使用 5×5 的内核腐蚀操作,以用来堵住小洞

  1. def apply_morphology(img, method):
  2. """
  3. Apply a morphological operation, either opening (i.e. erosion followed by dilation) or closing (i.e. dilation followed by erosion). Show result.
  4. """
  5. if method == 'open':
  6. op = cv2.MORPH_OPEN
  7. elif method == 'close':
  8. op = cv2.MORPH_CLOSE
  9. img_morphology = cv2.morphologyEx(
  10. src=img,
  11. op=op,
  12. kernel=np.ones((5, 5), np.uint8),
  13. )
  14. show_image(img_morphology)
  15. return img_morphology

高斯模糊操作

图像模糊(又名平滑)是有用的消除高频(如噪声)的操作。注意,内核越大,模糊效果越差。

  1. img_gaussian = cv2.GaussianBlur(
  2. src=img,
  3. ksize=(5, 5),
  4. sigmaX=0,
  5. sigmaY=0,
  6. )

自适应阈值操作

阈值化将灰度图像转换为二值图像。自适应阈值是有用的时候,图像有不同的照明条件在不同的地区,因为它计算不同的阈值不同的区域。

下面的函数使用以下两种方法中的一种来自适应阈值化:

  • 高斯:阈值是邻域值的加权和,其中权重是一个高斯窗口

  • 平均值:阈值是邻域面积的平均值

  1. def apply_adaptive_threshold(img, method):
  2. """
  3. Apply adaptive thresholding, either Gaussian (threshold value is the weighted sum of neighbourhood values where weights are a Gaussian window) or mean (threshold value is the mean of neighbourhood area). Show result.
  4. """
  5. img = cv2.cvtColor(
  6. src=img,
  7. code=cv2.COLOR_RGB2GRAY,
  8. )
  9. if method == 'gaussian':
  10. adaptive_method = cv2.ADAPTIVE_THRESH_GAUSSIAN_C
  11. elif method == 'mean':
  12. adaptive_method = cv2.ADAPTIVE_THRESH_MEAN_C
  13. img_adaptive = cv2.adaptiveThreshold(
  14. src=img,
  15. maxValue=255,
  16. adaptiveMethod=adaptive_method,
  17. thresholdType=cv2.THRESH_BINARY,
  18. blockSize=11,
  19. C=2,
  20. )
  21. show_image(img_adaptive, cmap='gray')
  22. return img_adaptive

Sobel 滤波器操作

Sobel 算子结合了高斯平滑和微分(图像沿 x 或 y 的一阶导数)。它们有助于检测水平或垂直边缘,并能抵抗噪声。为了检测水平和垂直边缘,我们可以沿着 x 和 y 分别使用 Sobel 滤波器操作,然后查看结果。

  1. def apply_sobel(img, direction):
  2. img = cv2.cvtColor(
  3. src=img,
  4. code=cv2.COLOR_RGB2GRAY,
  5. )
  6. if direction == 'h':
  7. dx, dy = 0, 1
  8. elif direction == 'v':
  9. dx, dy = 1, 0
  10. img_sobel = cv2.Sobel(
  11. src=img,
  12. ddepth=cv2.CV_64F,
  13. dx=dx,
  14. dy=dy,
  15. ksize=5,
  16. )
  17. return img_sobel

拉普拉斯滤波器操作

与 Sobel 算子类似,拉普拉斯算子使用微分进行处理。但是,他们使用图像沿 x 和 y 的二阶导数(通过内部加上使用 Sobel 算子计算的二阶 x 和 y 导数)。拉普拉斯算子用于检测边缘(以及其他二阶导数为0的可能无意义的位置,所以它们应该只适用于需要的地方)。

  1. def apply_laplacian(img):
  2. img = cv2.cvtColor(
  3. src=img,
  4. code=cv2.COLOR_RGB2GRAY,
  5. )
  6. img_laplacian = np.uint8(
  7. np.absolute(
  8. cv2.Laplacian(
  9. src=img,
  10. ddepth=cv2.CV_64F,
  11. )
  12. )
  13. )
  14. show_image(img_laplacian, cmap='gray')
  15. return img_laplacian

其他代码

最后,当你的图像中有一些不同区域的框坐标时(例如,你使用了一个标签工具来标注标题、副标题、作者等) ,你不希望: 裁剪每个区域 > 保存裁剪后的图像本地 > 使用本地图像发布一个 API 请求,因为这会拖慢你的速度。

为了解决这个问题,您可以改为:裁剪每个区域 > 编码裁剪图像到内存缓冲区 > 后请求使用内存缓冲区中的图像,这比第一种方法更快。

下面展示了如何将图像编码到 buffer 中,然后使用 buffer 为 OCR 请求准备好数据(以及如果需要的话如何将图像解码回来) :

  1. _, buf = cv2.imencode(
  2. ext=".jpg",
  3. img=img,
  4. )
  5. data = buf.tostring()
  6. img = cv2.imdecode(
  7. buf=buf,
  8. flags=cv2.IMREAD_UNCHANGED,
  9. )

·  END  ·

HAPPY LIFE

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/繁依Fanyi0/article/detail/802209
推荐阅读
相关标签
  

闽ICP备14008679号