ruby - 使用 ImageMagick 删除收据图像边框

标签 ruby imagemagick ocr tesseract image-masking

在使用 tesseract-OCR 引擎提取文本之前,我使用 ImageMagick 服务对收据图像进行预处理。我需要删除收据的背景。我已经通过 mask 去除了边框 here. 但我无法为收据创建掩码。

但是,我已经尝试去除收据图像中的阴影。

初始图片(收据示例)

enter image description here

convert input.png -colorspace gray \
      \( +clone -blur 0x2 \) +swap -compose divide -composite \
      -linear-stretch 5%x0%   photocopy.png

应用代码后:

enter image description here

我已经尝试使用下面的代码将除白色以外的所有颜色变为黑色,但这似乎并没有完全遮掉 photocopy.png 的背景。

convert receipt.jpg -fill black -fuzz 20% +opaque "#ffffff" black_border.jpg

enter image description here

请问有什么方法可以去掉收据图片的边框吗?或者从图像中创建任何类型的蒙版?注意:我需要为具有不同背景的多个图像去除噪点和边框。

最佳答案

回答你的问题

"Is there any way to remove the border of the receipt image? Or create any kind of masks out of the image?"

以下命令(基于您自己的代码)将创建一个图像,您可以使用它来导出适用掩码的尺寸:

convert                     \
   origscan.jpg             \
  -colorspace gray          \
   \( +clone 0 -blur 0x2 \) \
  +swap                     \
  -compose divide           \
  -composite                \
  -linear-stretch 5%x0%     \
  -threshold 5%             \
  -trim                     \
   mask-image.png

您可以使用该蒙版图像创建单色(黑色)蒙版——在一个命令中:

convert                     \
   origscan.jpg             \
  -colorspace gray          \
   \( +clone 0 -blur 0x2 \) \
  +swap                     \
  -compose divide           \
  -composite                \
  -linear-stretch 5%x0%     \
  -threshold 5%             \
   \(                       \
      -clone 0              \
      -fill '#000000'       \
      -colorize 100         \
   \)                       \
  -delete 0                 \
   black-mask.png

以下是上述两个命令的并排结果:

您可以使用identify 获取mask-image.pngblack-mask.png 的几何形状:

identify -format "%g\n" *mask*.png
  2322x4128+366+144
  2322x4128+366+144

因此图像 Canvas 的宽度为 2322 像素,高度为 4128 像素。在我们的 -trim 操作之后,两个图像的可见部分当然更小。 (+366+144 部分表示相对于原始图像左上角的水平/垂直偏移量。)


附加评论: 说了这么多:您真的应该考虑用收据制作更好的照片! (如果你有一个可以创建 4128 像素高度图像的相机,这应该不是问题。如果你有这么多收据要处理,那么最好买一个小压盘玻璃,你可以放在纸上,以便在拍照时将其拉直......)

关于ruby - 使用 ImageMagick 删除收据图像边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27798753/

相关文章:

ruby - 读取嵌套哈希并生成类似结构的单独哈希

ruby-on-rails - 使用 rSpec 测试 delayed_job 链的最佳方法是什么?

imagemagick - RMagick 无法读取远程图像

image - 将图像分成几部分

javascript - node.js 将 imagemagick 的输出上传到 AWS S3

python - 后处理并读取模糊的数字-OpenCV/tesseract

ios - 从 ios 上的图像中获取文本(图像处理)

ruby-on-rails - 设计提供陈旧 token 的 token 身份验证

ruby-on-rails - 如果我直接命中 'localhost:3000/404'端点,Rails config.exceptions_app无法正常工作

python - 检测屏幕截图中的文本区域