【问题标题】:Counting number of object in an PNG image using ImageMagick使用 ImageMagick 计算 PNG 图像中的对象数量
【发布时间】:2018-09-11 06:04:18
【问题描述】:

我正在尝试计算图像中对象的数量

我有一张测试图片

使用 php 魔术函数后,我可以将其转换为二进制图像

我需要的是一个函数,在这种情况下返回图像中白色物体的数量 8 我对图像了解不多,但是在 imagemagic 上有一个已删除的帖子,其中使用以下命令

var_dump( 
        exec("convert out.pbm -define connected-components:verbose=true  -define connected-components:area-threshold=50 \ -connected-components 4 -auto-level -depth 8 test.png")
);

【问题讨论】:

  • 我已经发布了 40 多个答案,请在 StackOverflow 搜索框中输入以下内容user:2836621 connected components
  • @MarkSetchell 我在看你的这篇文章,我认为这将是我的解决方案,但是 exec 函数没有返回任何内容,我厌倦了一个简单的转换命令,它正在工作
  • 我厌倦了不同的命令块 exec("convert binary.jpg -colorspace gray -negate -t​​hreshold 10% -define connected-components:verbose=true -define connected-components:area-threshold=100 output.png", $o) 它的工作和 output.png 被创建(但是我在 php 中没有得到任何回报)但是当我添加最终命令时 -connected-components 8 -auto-level 输出文件没有创建所以这意味着有一些错误

标签: php imagemagick


【解决方案1】:

您的 ImageMagick 版本是多少? -connected-components 至少需要 6.8.9.10 版本。取出 \ 并使其全部成为一条长线。使用新行 \ 可能会混淆 PHP exec()。

试试这个方法,将你的 area-threshold 增加到 150:

<?php
exec("convert out.pbm -define connected-components:verbose=true -define connected-components:area-threshold=150 -connected-components 4 -auto-level -depth 8 test.png 2>&1",$out,$returnval);
foreach($out as $text)
{echo "$text<br>";}
?>


然后它应该返回:

Objects (id: bounding-box centroid area mean-color):
  22: 665x500+0+0 332.0,241.1 295195 gray(0)
  7605: 125x101+86+380 150.6,431.3 10246 gray(255)
  6995: 139x105+476+350 541.7,401.0 10087 gray(255)
  5560: 94x62+133+233 182.0,265.4 4622 gray(255)
  5196: 106x61+434+217 483.3,246.8 4608 gray(255)
  3470: 76x42+162+145 201.4,164.9 2448 gray(255)
  3023: 76x40+401+126 438.7,145.5 2391 gray(255)
  1420: 58x28+186+75 215.5,88.7 1315 gray(255)
  992: 61x24+385+64 414.3,75.7 1146 gray(255)
  2: 33x18+0+0 12.9,6.6 391 gray(0)


如果您只想要没有图像的列表,您可以将 test.png 替换为 null:。

如果您希望输出为二进制而不是对 id 编号进行灰度编码,则添加 -define connected-components:mean-color=true:

<?php
exec("convert out.pbm -define connected-components:verbose=true -define connected-components:area-threshold=150 -define connected-components:mean-color=true -connected-components 4 -auto-level -depth 8 test.png 2>&1",$out,$returnval);
foreach($out as $text)
{echo "$text<br>";}
?>


如果您只想要计数和二进制输出,请尝试:

<?php
exec("convert image.jpg -define connected-components:verbose=true -define connected-components:area-threshold=150 -define connected-components:mean-color=true -connected-components 4 -auto-level -depth 8 test.png 2>&1 | grep "gray(255)" | wc -l | sed 's/^[ ]*//' ",$out,$returnval);
foreach($out as $text)
{echo "$text<br>";}
?>


哪个应该返回 8。

https://www.imagemagick.org/script/connected-components.php

【讨论】:

    【解决方案2】:

    这似乎可以满足您的需要:

    <?php
    $output=shell_exec("convert -size 1000x1000 xc:black -fill white -draw \"rectangle 10,10 900,900\" -define connected-components:verbose=true -connected-components 4 -auto-level -depth 8 test.png");
    echo $output;
    ?>
    

    一定要解析输出,如下所示:

    Objects (id: bounding-box centroid area mean-color):
    1: 891x891+10+10 455.0,455.0 793881 srgba(100%,100%,100%,1.08255)
    0: 1000x1000+0+0 670.9,670.9 206119 srgba(0%,0%,0%,1.31795)
    

    并注意颜色(如果您想要白色对象)和区域 - 字段/列在输出的第一行中带有标题/标签。所以你应该注意到检测到了 2 个物体,第一个是白色的并且更小,第二个是黑色和整个图像的大小。

    【讨论】:

    • 仍然注意到,它返回空字符串,命令正在运行并创建文件
    • 我不在可以测试的机器上,试试$output = shell_exec("convert ... 2&gt;&amp;1")也许
    • 代码和文件结构awmark.website/nadeem_img_processing/code.pngshell_exec return null
    • 去掉多余的反斜杠。
    • Mmm... 在 shell/Terminal 中,尝试使用 which convert 找到 convert 的完整路径,然后在代码中使用完整路径。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多