du20150401
2016-01-22 05:23
浏览 227
已采纳

php - 如何使用imagick或其他库找到图像中边缘的坐标?

given an image composed of a plain black field with a single white rectangle inside it somewhere, how can i extract the coordinates of the rectangle ? i've googled and googled and found numerous articles on edge detection using hough lines, convolution, morphology, etc., but all of these actually edge the image or convert it in some way. i don't want to change the image, all i want to do is find where the edges are.

obviously, i could simply iterate over the entire width+height of the image and look at the pixel colours (as some posts suggest), but that seems horribly inefficient. is there no built-in algorithm ? it seems like this must be part of imagick somewhere, otherwise how could it actually find and draw the edges of internal images ? but i'm having trouble finding out how to get at it.

图片转代码服务由CSDN问答提供 功能建议

给定一个由纯黑色字段组成的图像,其中有一个白色矩形,我如何提取坐标 矩形? 我用谷歌搜索和谷歌搜索,发现许多关于边缘检测的文章使用霍夫线,卷积,形态等,但所有这些实际上边缘图像或以某种方式转换它。 我不想改变图像,我想做的就是找到边缘的位置。

显然,我可以简单地遍历图像的整个宽度+高度并查看 在像素颜色(如一些帖子所示),但这似乎非常低效。 没有内置算法吗? 似乎这必须成为想象力的一部分,否则它怎么能真正找到并画出内部图像的边缘? 但我很难找到如何实现它。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douxueke5653 2016-01-22 21:45
    已采纳
    $base = new Imagick(realpath('./trim.png'));
    $base->trimImage(0);
    // get the new image size
    $geometry = $base->getImageGeometry();
    // Retrieve the trim info
    $pageInfo = $base->getImagePage();
    

    A similar example is on the manual page: http://php.net/manual/en/imagick.trimimage.php#111332

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • donglingyi4679 2016-01-25 19:05

    it appears the complete solution to the problem as posted is as follows :

    $im->trimImage(0);
    $pagedata = $im->getImagePage();
    $x = $pagedata['x'];
    $y = $pagedata['y'];
    $im->setImagePage(0, 0, 0, 0);
    $w = $im->width;
    $h = $im->height;
    

    not setting the image page before getting the height and width return the height and width of the entire original image, as the note on your referenced page suggests.

    it turns out the solution is even easier if you're working with an image that uses transparency ; it's just the getImagePage() function alone :

    $im = new Imagick(realpath('./image.png'));
    $pagedata = $im->getImagePage();
    $x = $pagedata['x'];
    $y = $pagedata['y'];
    $w = $pagedata['width'];
    $h = $pagedata['height'];
    print("x,y: $x, $y<br>
    ");
    print("w,h: $w, $h<br>
    ");
    

    for some reason, if trimImage(0) is used with this transparent image, the (x,y) coordinates are set to (-1,-1). unfortunately i don't know enough about imagick to say why this happens. maybe you could answer this, @danack ?

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题