【问题标题】:How to I crop an image based on a bounding box in the image (programatically)?如何根据图像中的边界框(以编程方式)裁剪图像?
【发布时间】:2014-04-02 00:20:41
【问题描述】:

我正在构建一个 Web 应用程序,该应用程序需要能够根据图像中包含白色边框(且无填充)的 4 边形状的位置裁剪图像。用户上传的所有图像都将变暗,因此它们不应干扰算法来查找边界框。应用程序的这一部分应该能够处理所有类型的四边形,并将拉伸白边形状中包含的任何内容以适应正方形。

我在 Photoshop 中的懒惰再现:http://i.stack.imgur.com/xJjoL.png

我正在使用 Laravel 构建这个应用程序(LAMP 堆栈)。我正在研究 ImageMagick 和 GD 等库,但我对以编程方式编辑图像还是很陌生。我可以调用 IM 或 GD 中的特定函数来帮助我构建应用程序的这一部分吗?有没有更好的 PHP 库可以做到这一点?我不一定要代码;我只是想知道在构建它之前应该使用哪些工具。

【问题讨论】:

    标签: php image image-processing imagemagick gd


    【解决方案1】:

    如果我理解您的要求,Imagemagick 可以用于此类应用程序。要更改图像的视角,请查看:

    http://www.imagemagick.org/Usage/distorts/#perspective

    要在扭曲后裁剪图像,请查看:

    http://www.imagemagick.org/Usage/crop/#crop

    制作一个 bash 循环来完成这两个任务很容易。像这样的:

     ## first distort the images
    
     for i in *.jpg ##or whatever format you have (.tiff, pdf, etc..) 
     do 
     convert $i -distort Perspective out_file.png ## where you give coordinates into the perspective method
     done
    
     ## now to crop the distorted images
     for i in *.png 
     do
     convert $i -crop out_file.jpg ## where you give the coordinates of the new rectangle to the crop method
     done
    

    上面的代码只是一个起点,需要根据我上面提到的链接进行填充。快乐的脚本!

    【讨论】:

      【解决方案2】:

      您可能需要查看 OpenCV 以识别矩形。 GD 和 image magic 擅长使用过滤器编辑图像,但它们并没有做很多(或任何?)对象检测。

      见:https://github.com/ProGM/OpenCV-for-PHP/wiki/Installation-guide

      【讨论】:

        猜你喜欢
        • 2018-10-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-02-19
        • 1970-01-01
        相关资源
        最近更新 更多