使用ImageMagick删除收据图像边框 [英] Remove receipt image border using ImageMagick

查看:193
本文介绍了使用ImageMagick删除收据图像边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用tesseract-OCR引擎提取文本之前,我正在使用ImageMagick服务预处理收据图像。我需要删除收据的背景。我已经通过蒙版去除了边框这里。 但是我无法为收据创建遮罩。

I'm using ImageMagick service to pre-process the receipt image before using tesseract-OCR engine to extract texts. I need to remove the background of the receipts. I've gone through masking to remove the border here. But I'm unable to create the mask for the receipts.

但是,我试图从收据图像中删除阴影。

However, I've tried to remove shadows from the receipt images.

初始图片(收据示例)

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

应用代码后:

我已经尝试了下面的代码来制作除白色到黑色之外的所有颜色,但这似乎没有完全搞定了photocopy.png的背景。

I've tried the code below to make all colors except white to black but this does not seem to be totally blacking out the background of photocopy.png.

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

有没有办法删除收据图像的边框?或者从图像中创建任何类型的蒙版?注意:我需要删除具有不同背景的多个图像的噪点和边框。

Is there any way to remove the border of the receipt image? Or create any kind of masks out of the image? Note: I need to remove noise and border for multiple images with different backgrounds.

推荐答案

回答你的问题


有没有办法删除收据图像的边框?或者从图像中创建任何类型的蒙版?

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

The following command (based on your own code) will create an image which you can use to derive the dimensions of an applicable mask:

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

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

You can use that mask-image to create a monochrome (black) mask -- in one command:

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

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

Here are the results of above two commands, side by side:

 

您可以使用识别获取 mask-image.png 的几何以及 b lack-mask.png

You can use identify to get the geometry of mask-image.png as well as black-mask.png:

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

所以图像画布宽2322像素,高4128像素。在我们的 -trim 操作之后,两个图像的可见部分当然都较小。 ( + 366 + 144 部分表示距原始图像左上角的水平/垂直偏移。)

So the image canvases are 2322 pixels wide and 4128 pixels high. The visible parts both images are of course smaller, following our -trim operation. (The +366+144 part indicates a horizontal/vertical offset from the top left corner of the original image.)

其他评论: 说完这一切:你应该真正考虑从收据中创建更好的照片! (如果您的相机可以创建4128像素高度的图像,这应该不是问题。如果您要处理的收据太多,那么获得一块可以制作的小平板玻璃可能是个好主意。放在纸上,以便在拍摄时将其拉直...)

这篇关于使用ImageMagick删除收据图像边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆