【问题标题】:Detect text position in an image and crop it in Python检测图像中的文本位置并在 Python 中对其进行裁剪
【发布时间】:2020-01-22 17:18:53
【问题描述】:

我有这张照片 Text in an image

我想检测该文本位置,并裁剪仅聚焦该文本的图像。

这是我的代码:

from PIL import Image 
# Opens a image in RGB mode 
im = Image.open(r"image.jpg") 
# Size of the image in pixels (size of orginal image) 
# (This is not mandatory) 
width, height = im.size 
print(im.size)
# Setting the points for cropped image 
left = 5
top = height / 4
right = 164
bottom = 3 * height / 4
# Cropped image of above dimension 
# (It will not change orginal image) 
im1 = im.crop((left, top, right, bottom)) 
# Shows the image in image viewer 
im1.save("new.jpg")

此代码工作正常,但图像中文本的位置不是静态的。 我希望代码自动检测文本的位置然后裁剪它。

【问题讨论】:

  • 到目前为止,您尝试了哪些方法来解决实际问题?
  • 我尝试将图像拆分为 (height/30) 部分(注意:30 表示可以读取图像中数字的框的高度)而不是进入“for”循环并全部读取,如果脚本找到数字它会打破循环。但是 Mark Setchell,给了我有史以来最好的解决方案,请阅读下面的正确答案。

标签: python python-3.x image-processing python-imaging-library crop


【解决方案1】:

你可以使用getbbox()获取边界框:

image=Image.open('text.jpg') 
x1,y1,x2,y2=image.getbbox() 
print(x1,y1,x2,y2)   

输出

16 192 208 216

【讨论】:

  • 这是有史以来最好的解决方案,非常感谢。
【解决方案2】:

您可以使用基于深度学习的模型“EAST”来检测图像上的文本。 OpenCV 的 EAST 文本检测器基于一种新颖的架构和训练模式。它能够

(1) 在 720p 图像上以 13 FPS 接近实时运行,并且

(2) 获得state-of-the-art的文本检测精度。参考链接:https://www.pyimagesearch.com/2018/08/20/opencv-text-detection-east-text-detector/

【讨论】:

  • 虽然此链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接答案可能会失效。 - From Review
猜你喜欢
  • 2020-06-07
  • 2017-11-07
  • 2012-06-22
  • 2018-01-27
  • 1970-01-01
  • 2021-05-27
  • 2015-11-09
  • 2022-01-01
相关资源
最近更新 更多