【问题标题】:Crop darkspace from photo in PHP在 PHP 中从照片中裁剪暗空间
【发布时间】:2012-01-19 22:51:25
【问题描述】:

这篇文章是几年前在这里提出的一个问题的延伸: Crop whitespace from image in PHP

那篇文章很好地解释了如何使用 php 从图像周围裁剪纯白色空间。这里的问题是它期望图像周围有“纯白色”。这是我正在尝试做的事情以及它的不同之处:

我正在使用我的手机作为“扫描仪”来为文件拍照,然后将它们上传到我的服务器(别担心......这里没有“秘密代理人”的东西......它是实际上主要是午餐收据,有时是名片)。就我而言,我总是尝试使用深色背景与浅色纸形成对比。然而,图像永远不会以“纯”黑/白颜色出现。我的图像始终为 JPG 格式。我正在试验上面引用的代码(但交换来测试暗/黑色边框),想知道是否有一种方法可以概括该函数以更广泛地应用?我可以安装 ImageMagick 并使用 trimImage 和其他一些东西,但我宁愿坚持使用 GD,因为这对更多 StackOverflow 用户更有用。到目前为止,这是我的想法:

想法 #1:将图像转换为灰度并将对比度调高...在此处进行比较(可能与当时的纯黑色进行比较)...然后使用原始坐标(非灰度)图片。你觉得……好方法?它会起作用吗? 我会使用 imagefilter($img, IMG_FILTER_GRAYSCALE);然后是 imagefilter($img, IMG_FILTER_CONTRAST, -90);

想法 #2:通用。这是我最需要帮助的想法。如果我能从边界自动检测颜色的大致范围,那就太酷了。这个想法是从四个角的每一个中采样一个(或几个)像素,然后“平均”它们以找出背景。这意味着有一天,如果我想在棕色背景、红色背景或绿色背景下拍照……没问题。我认为朝这个方向迈出的第一步将涉及使用 imagecolorclosest() 但坦率地说,我对如何选择、比较、平均然后重新比较颜色感到头疼。理想情况下,有一种方法可以将其中的一些逻辑插入到原始函数中(来自上面的链接),然后无论图像中的背景颜色是什么(假设它相当一致),它都可以通用地工作,并且会使用照片(因此背景颜色并非始终完全相同的颜色值的图像)。

所以...两部分问题:我的想法 #1 是最好的方法吗?我应该注意哪些问题或对改进/实施提出建议?而且...有没有人想尝试一下 IDEA #2,至少给我足够的代码让我朝着正确的方向前进?

【问题讨论】:

    标签: php image-processing gd


    【解决方案1】:

    所以我继续尝试使用 IDEA#1,它运行得非常好。我已经准备好了,目前不需要进一步的帮助......虽然仍然认为 IDEA #2 是一个更好的全方位解决方案,所以如果有人想为它提供一些指示,我会尝试这些想法并尝试想出额外的代码示例发布在这里。现在,这是我上面描述的 IDEA#1 的工作版本,并在几个手机图像上进行了测试:

    //MANY THANKS TO: http://stackoverflow.com/questions/1669683/crop-whitespace-from-image-in-php    
    
    $OriginalImageFileLocation = 'sampleimage.jpg';
    
        //load the image into a variable (and throw an error if image does not exist)
        if( !$img = imagecreatefromjpeg("$OriginalImageFileLocation") )
        { exit("Could not use image $OriginalImageFileLocation"); } 
    
        // This function only can detect the background if it is "pure black"...so convert to greyscale then crank the contrast
        imagefilter($img, IMG_FILTER_GRAYSCALE);
        imagefilter($img, IMG_FILTER_CONTRAST, -100);
    
    
        //find the size of the borders
        $b_top = 0;
        $b_btm = 0;
        $b_lft = 0;
        $b_rt = 0;
    
        //top
        for(; $b_top < imagesy($img); ++$b_top) {
          for($x = 0; $x < imagesx($img); ++$x) {
            if(imagecolorat($img, $x, $b_top) != 0x000000) {
               break 2; //out of the 'top' loop
            }
          }
        }
    
        //bottom
        for(; $b_btm < imagesy($img); ++$b_btm) {
          for($x = 0; $x < imagesx($img); ++$x) {
            if(imagecolorat($img, $x, imagesy($img) - $b_btm-1) != 0x000000) {
               break 2; //out of the 'bottom' loop
            }
          }
        }
    
        //left
        for(; $b_lft < imagesx($img); ++$b_lft) {
          for($y = 0; $y < imagesy($img); ++$y) {
            if(imagecolorat($img, $b_lft, $y) != 0x000000) {
               break 2; //out of the 'left' loop
            }
          }
        }
    
        //right
        for(; $b_rt < imagesx($img); ++$b_rt) {
          for($y = 0; $y < imagesy($img); ++$y) {
            if(imagecolorat($img, imagesx($img) - $b_rt-1, $y) != 0x000000) {
               break 2; //out of the 'right' loop
            }
          }
        }
    
        //now re-initialize the original image since the greyscaled version was just for gathering cordinates
        $img = imagecreatefromjpeg("$OriginalImageFileLocation");
    
        // OPTIONAL: make the final image a bit prettier (and greyscale for long-term consistency)
        imagefilter($img, IMG_FILTER_GRAYSCALE);
        imagefilter($img, IMG_FILTER_BRIGHTNESS, +15);
        imagefilter($img, IMG_FILTER_CONTRAST, -20);
    
    
        //copy the contents, excluding the border
        $newimg = imagecreatetruecolor( imagesx($img)-($b_lft+$b_rt), imagesy($img)-($b_top+$b_btm) );
        imagecopy($newimg, $img, 0, 0, $b_lft, $b_top, imagesx($newimg), imagesy($newimg));
    
        //finally, output the image
        header("Content-Type: image/jpeg");
        imagejpeg($newimg);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-22
      • 1970-01-01
      • 2010-12-12
      • 1970-01-01
      • 1970-01-01
      • 2012-10-29
      • 1970-01-01
      相关资源
      最近更新 更多