图像自动裁剪(自动识别图像与背景)
我正在寻找一种裁剪上传图像的方法。我花了很多时间搜索该网站,如果用户提供要裁剪的尺寸/位置,我会找到很多好的解决方案。
我的照片大多是在较暗的背景下拍摄的纸片。想象一下在较暗(但不是标准)背景下拍摄的驾驶执照、信用卡、票根的手机照片。我正在寻找一种在上传之前或之后让我的代码识别应该裁剪的内容的方法。
在完美的世界中,一些聪明人会提出关于裁剪什么的建议,并让用户有机会给予最终同意。这将允许不太完美的裁剪算法。
在根据第一个答案查找“修剪”而不是裁剪后,似乎我可以使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看看下面的结果是否令人满意。我在 Mathematica 中实现了一个小程序来做到这一点。如果您喜欢结果,我可以进一步解释算法,或者您可以自己在线查看 Mathematica 帮助文件。
我们从这些图像开始:
并定义以下函数:
结果为:
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:
And define the following function:
And the results are:
HTH!
请注意,在每个像素的基础上,您想要裁剪的背景并不一致。存在细微(有时不那么细微)的变化,您需要使用容差来考虑这些变化。
如果您熟悉 Adobe 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.
可能有用的一个短语是“显着性”——识别图像的“有趣”区域。开始研究这个问题的地方是微软研究院 - 您可以使用概述了“有趣部分”的图像示例。这能达到你的要求吗?
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?