How can I detect borders of a piece of paper inside of an image and crop it using ImageMagick?

不想你离开。 提交于 2019-12-25 03:53:45

问题


I'm trying to crop paper documents from photographs. For example, someone takes a picture of a document and sends it to the server and it will get edited to look like a scanned document. How can I detect the border of the document and crop it using ImageMagick?

Thanks


回答1:


You want to use the convert command with the -trim option to crop out the solid color borders, and since the images are scanned, the -fuzz option to make sure trim completely crops the border even if the color isn't perfectly solid.

So something like this:

convert input.jpg -fuzz 2% -trim output.jpg

Translating that into PHP code, you'd end up with this:

$image = new Imagick('input.jpg');
$image->trimImage(2); // Trim the image with a 2% fuzz
$image->writeImage('output.jpg');


来源:https://stackoverflow.com/questions/7063415/how-can-i-detect-borders-of-a-piece-of-paper-inside-of-an-image-and-crop-it-usin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!