图像自动裁剪(自动识别图像与背景)

发布于 2024-10-16 09:53:30 字数 437 浏览 4 评论 0原文

我正在寻找一种裁剪上传图像的方法。我花了很多时间搜索该网站,如果用户提供要裁剪的尺寸/位置,我会找到很多好的解决方案。

我的照片大多是在较暗的背景下拍摄的纸片。想象一下在较暗(但不是标准)背景下拍摄的驾驶执照、信用卡、票根的手机照片。我正在寻找一种在上传之前或之后让我的代码识别应该裁剪的内容的方法。

在完美的世界中,一些聪明人会提出关于裁剪什么的建议,并让用户有机会给予最终同意。这将允许不太完美的裁剪算法。

在根据第一个答案查找“修剪”而不是裁剪后,似乎我可以使用 Imagemagick 来做到这一点 imagemagick.org/script/api.php

Imagemagick 似乎在 PHP 和 PHP 中有接口。红宝石。它支持修剪“模糊”图像,其中边界不一致。我正在寻求建议的问题是对具有良好逻辑来查找图像边缘的图形引擎的建议。还有比 imagemagick 更好的引擎吗?

I am looking for a method of cropping uploaded images. I spent a lot of time searching the site and find plenty of good solutions if the user is providing the dimensions/location of what is to be cropped.

My pictures are mostly slips of paper taken against a darker background. Think camera phone shots of drivers licenses, credit cards, ticket stubs against a somewhat darker (but not standard) background. I am looking for a method to either before or after upload to have the my code identify what should be cropped.

In a perfect world some smarts would make a suggestion as to what to crop and give the user a chance to give it a final okay. This would allow for a less then perfect cropping algorithm.

After looking up 'trim' instead of crop based on the first answer it seems like I could possible do this with Imagemagick imagemagick.org/script/api.php

Imagemagick seems to have intefaces in PHP & Ruby. It supports a trimming of 'fuzzy' images, one in which the boarder is not consistent. The problem that I am looking for advise on is a recommendation on a graphics engine that has good logic to find the edges of the images. Is there a better engine that I should be looking at then imagemagick?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尸血腥色 2024-10-23 09:53:30

看看下面的结果是否令人满意。我在 Mathematica 中实现了一个小程序来做到这一点。如果您喜欢结果,我可以进一步解释算法,或者您可以自己在线查看 Mathematica 帮助文件。

我们从这些图像开始:

在此处输入图像描述在此处输入图像描述

并定义以下函数:

f[image_] := 
 ImageCrop@
   ImageMultiply[image, 
    ColorNegate@
     Erosion[Dilation[
       DeleteSmallComponents[Binarize@ColorNegate@image, 10000], 3], 
      3]] // ImageAdjust  

结果为:

在此处输入图像描述在此处输入图像描述

HTH!

See if the following results are satisfactory. I implemented a small program in Mathematica to do it. If you like the results, I can explain further the algorithms, or you may check the Mathematica help files on line yourself.

We start with these images:

enter image description hereenter image description here

And define the following function:

f[image_] := 
 ImageCrop@
   ImageMultiply[image, 
    ColorNegate@
     Erosion[Dilation[
       DeleteSmallComponents[Binarize@ColorNegate@image, 10000], 3], 
      3]] // ImageAdjust  

And the results are:

enter image description hereenter image description here

HTH!

辞慾 2024-10-23 09:53:30

请注意,在每个像素的基础上,您想要裁剪的背景并不一致。存在细微(有时不那么细微)的变化,您需要使用容差来考虑这些变化。

如果您熟悉 Adob​​e Photoshop,那么有一个功能可以完全执行您所建议的功能(图像 -> 修剪)。它的工作原理是查看用户选择的角像素(左上角、右上角等),然后确定不属于该颜色的最外围像素。

在编程中,您需要迭代图像中的像素并确定不属于所选颜色的最顶部和最底部像素的 Y 坐标、最左侧和最右侧的 X 坐标。这些坐标然后确定作物尺寸。

在确定像素是否属于所选颜色时,您可以通过排除百分比差异来引入容差。换句话说,如果背景不是一致的白色(RGB 255、255、255),您可能需要允许一些变化。

您没有提到编程语言,因此您需要研究您可能想要利用的图形库和函数。

Realize that, on a per pixel basis, the background you want to crop out is not consistent. Subtle (sometimes less than subtle) variations exist which you need to account for using a tolerance.

If you are familiar with Adobe Photoshop, there is a function that performs exactly what you are proposing (Image -> Trim). It works by looking at a user-selected corner pixel (top left, top right, etc.) and then determining the most outlying pixels that are not of that color.

In programming, you would need to iterate over the pixels in the image and determine the Y-coordinate of the topmost and bottommost pixel that is not of the selected color, the X-coordinate of the leftmost and rightmost. Those coordinates then determine the crop dimension.

When deciding if a pixel is or is not of the chosen color, you can introduce tolerance by allowing a percentage difference to factor out. In other words, if the background is not a consistent white (RGB 255, 255, 255), you may want to allow for some variation.

You didn't mention the programming language so it is up to you to research the graphic libraries and functions you may want to take advantage of.

调妓 2024-10-23 09:53:30

可能有用的一个短语是“显着性”——识别图像的“有趣”区域。开始研究这个问题的地方是微软研究院 - 您可以使用概述了“有趣部分”的图像示例。这能达到你的要求吗?

One phrase which may be useful is "saliency" - identifying "interesting" areas of the image. And a place to start researching this is Microsoft Research - you can examples of images with the "interesting part" outlined. Does that achieve what you require?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文