首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ImageMagick或其他方法:如何在图像中找到物体中心的坐标?

ImageMagick或其他方法:如何在图像中找到物体中心的坐标?
EN

Stack Overflow用户
提问于 2021-05-21 20:16:08
回答 1查看 223关注 0票数 0

我试图在图像(mug)中找到对象的坐标。

最后,我需要导出的是x、y、宽度和高度,其中x和y应该是包含杯子(而不是左上角)的矩形的中心,宽度和高度应该是相对于图像的宽度和高度的浮动值(从0.0到1.0)。

我尝试安装ImageMagick,加载映像,然后在cmd中运行以下命令:

代码语言:javascript
复制
magick image1.jpg -define connected-components:verbose=true -connected-components 8 -auto-level image1.jpg

我得到了错误: error/vision.c/ConnectedComponentsImage/437. magick:太多的对象“Image1.jpg”@

有谁知道如何解决这个问题,或者任何其他相对简单的方法来找到所需的坐标?

EN

回答 1

Stack Overflow用户

发布于 2021-05-21 20:45:21

不幸的是,您的对象与背景颜色相同。因此,简单的图像处理很难做到这一点。您需要人工智能/深度学习方法,比如在http://remove.bg

因此,我使用了http://remove.bg来实现这一点,但是请注意,remove.bg缩小了一些图像。

从中提取alpha通道:

代码语言:javascript
复制
convert mug.png -alpha extract mug_alpha.png

代码语言:javascript
复制
WxH=`convert mug_alpha.png -format "%wx%h" info:`
W=`echo $WxH | cut -dx -f1`
H=`echo $WxH | cut -dx -f2`

bbox=`convert mug_alpha.png \
-type bilevel \
-define connected-components:mean-color=true \
-define connected-components:exclude-header=true \
-define connected-components:verbose=true \
-define connected-components:keep-top=1 \
-connected-components 4 \
null: | grep "gray(255)" | awk '{print $2}' | tr "x" "+"`
wd=`echo $bbox | cut -d+ -f1`
ht=`echo $bbox | cut -d+ -f2`
xoff=`echo $bbox | cut -d+ -f3`
yoff=`echo $bbox | cut -d+ -f4`

xcenter=`convert xc: -format "%[fx:$xoff + $wd/2]" info:`
ycenter=`convert xc: -format "%[fx:$yoff + $ht/2]" info:`
width=`convert xc: -format "%[fx:$wd/$W]" info:`
height=`convert xc: -format "%[fx:$ht/$H]" info:`
echo "xcenter=$xcenter, ycenter=$ycenter, width=$width, height=$height"

xcenter=277,ycenter=260,width=0.563433,height=0.60515

代码语言:javascript
复制
left=$xoff
top=$yoff
right=$((xoff+wd-1))
bottom=$((yoff+ht-1))
convert mug.png -fill none -stroke red -draw "rectangle $left,$top $right,$bottom" -alpha off mug_box.png
convert mug_alpha.png -fill none -stroke red -draw "rectangle $left,$top $right,$bottom" -alpha off mug_alpha_box.png

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67643402

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档