在图像中查找对象坐标的最简单方法是什么?
What's the easiest way to find the coordinates of an object in a image?
想象一下,在一种颜色的背景上有一张不同颜色的圆圈图像。找到圆心坐标的最简单方法是什么(当然 以编程方式)?
ImageMagick 有一个非常简单的方法,它是免费的,安装在大多数 Linux 发行版上,适用于 macOS 和 Windows - 无需编程!
让我们从这张图片开始:
现在您只需 运行 在终端或命令提示符中执行此操作:
magick input.png -define connected-components:verbose=true -connected-components 8 -auto-level output.png
输出
Objects (id: bounding-box centroid area mean-color):
0: 600x300+0+0 297.4,145.3 128391 srgb(0,0,0) <--- black background
2: 181x181+110+60 200.0,150.0 25741 srgb(0,0,255) <--- blue circle
3: 161x161+420+120 500.0,200.0 20353 srgb(255,0,255) <--- magenta circle
1: 81x81+10+10 50.0,50.0 5166 srgb(0,255,0) <--- green circle
4: 21x21+390+190 400.0,200.0 349 srgb(255,255,0). <--- yellow circle
我在<---
之后添加了上面的评论。
查看蓝色圆圈,您可以看到它的颜色是 srgb(0,0,255),它是蓝色的,尺寸为 181x181 像素 - 所以它的半径是 90 像素。包含矩形的左上角位于 [110,60]
,因此中心位于 [200,150]
,这与为质心给出的 200.00,150.00 匹配。
同样,看黄色圆圈,它的颜色是srgb(255,255,0),也就是黄色。它的高度和宽度为 21 个像素,这意味着半径为 10。包含正方形的左上角位于 [390,190]
,这意味着中心位于 [400,200]
,与给定的质心 400.0,200.0 相匹配.
我也想在 Python 中使用 OpenCV,使用与我的其他答案相同的起始图像。
代码如下所示:
#!/usr/bin/env python3
import numpy as np
import cv2
# Load image
im = cv2.imread('start.png')
# Convert to grayscale and threshold
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,1,255,0)
# Find contours, draw on image and save
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(im, contours, -1, (0,255,0), 3)
cv2.imwrite('result.png',im)
# Show user what we found
for cnt in contours:
(x,y),radius = cv2.minEnclosingCircle(cnt)
center = (int(x),int(y))
radius = int(radius)
print('Contour: centre {},{}, radius {}'.format(x,y,radius))
在终端上给出这个:
Contour: centre 400.0,200.0, radius 10
Contour: centre 500.0,200.0, radius 80
Contour: centre 200.0,150.0, radius 90
Contour: centre 50.0,50.0, radius 40
这是结果图片:
想象一下,在一种颜色的背景上有一张不同颜色的圆圈图像。找到圆心坐标的最简单方法是什么(当然 以编程方式)?
ImageMagick 有一个非常简单的方法,它是免费的,安装在大多数 Linux 发行版上,适用于 macOS 和 Windows - 无需编程!
让我们从这张图片开始:
现在您只需 运行 在终端或命令提示符中执行此操作:
magick input.png -define connected-components:verbose=true -connected-components 8 -auto-level output.png
输出
Objects (id: bounding-box centroid area mean-color):
0: 600x300+0+0 297.4,145.3 128391 srgb(0,0,0) <--- black background
2: 181x181+110+60 200.0,150.0 25741 srgb(0,0,255) <--- blue circle
3: 161x161+420+120 500.0,200.0 20353 srgb(255,0,255) <--- magenta circle
1: 81x81+10+10 50.0,50.0 5166 srgb(0,255,0) <--- green circle
4: 21x21+390+190 400.0,200.0 349 srgb(255,255,0). <--- yellow circle
我在<---
之后添加了上面的评论。
查看蓝色圆圈,您可以看到它的颜色是 srgb(0,0,255),它是蓝色的,尺寸为 181x181 像素 - 所以它的半径是 90 像素。包含矩形的左上角位于 [110,60]
,因此中心位于 [200,150]
,这与为质心给出的 200.00,150.00 匹配。
同样,看黄色圆圈,它的颜色是srgb(255,255,0),也就是黄色。它的高度和宽度为 21 个像素,这意味着半径为 10。包含正方形的左上角位于 [390,190]
,这意味着中心位于 [400,200]
,与给定的质心 400.0,200.0 相匹配.
我也想在 Python 中使用 OpenCV,使用与我的其他答案相同的起始图像。
代码如下所示:
#!/usr/bin/env python3
import numpy as np
import cv2
# Load image
im = cv2.imread('start.png')
# Convert to grayscale and threshold
imgray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
ret,thresh = cv2.threshold(imgray,1,255,0)
# Find contours, draw on image and save
im2, contours, hierarchy = cv2.findContours(thresh,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(im, contours, -1, (0,255,0), 3)
cv2.imwrite('result.png',im)
# Show user what we found
for cnt in contours:
(x,y),radius = cv2.minEnclosingCircle(cnt)
center = (int(x),int(y))
radius = int(radius)
print('Contour: centre {},{}, radius {}'.format(x,y,radius))
在终端上给出这个:
Contour: centre 400.0,200.0, radius 10
Contour: centre 500.0,200.0, radius 80
Contour: centre 200.0,150.0, radius 90
Contour: centre 50.0,50.0, radius 40
这是结果图片: