繁体   English   中英

如何在图像中找到形状的坐标

[英]how to find the cordinates of a shape in a image

在 BGR 图像中,有一个红色圆圈,我必须检测并找到它的坐标。

我已经将bgr图像转换为hsv,然后使用红色的上限和下限将红色与图像分开,现在如何找到那个红色圆圈的坐标

lower_red = np.array([0,150,50]) upper_red = np.array([10,255,255]) mask_img1 = cv2.inRange(img1_HSV,lower_red,upper_red) res=cv2.bitwise_and(img_1,img_1,mask=mask_img1) cv2.imshow('mask',res) cv2.waitKey(0)

使用 Python/OpenCV/Numpy,您可以使用 np.where 或更好的 np.argwhere。 这是一个例子:

输入:

在此处输入图像描述

import cv2
import numpy as np

# load image and set the bounds
img = cv2.imread("red_circle.png")

# get color bounds of red circle
lower =(0,0,255) # lower bound for each channel
upper = (0,0,255) # upper bound for each channel

# create the mask
mask = cv2.inRange(img, lower, upper)

# get coordinates of mask where it is white
coords = np.argwhere(mask == 255)
print(coords)

# write mask to disk
cv2.imwrite("red_circle_mask.png", mask)

# display mask
cv2.imshow("mask", mask)
cv2.waitKey(0)


结果:

[[ 95 100]
 [ 96  98]
 [ 96  99]
 [ 96 100]
 [ 96 101]
 [ 96 102]
 [ 97  97]
 [ 97  98]
 [ 97  99]
 [ 97 100]
 [ 97 101]
 [ 97 102]
 [ 97 103]
 [ 98  96]
 [ 98  97]
 [ 98  98]
 [ 98  99]
 [ 98 100]
 [ 98 101]
 [ 98 102]
 [ 98 103]
 [ 98 104]
 [ 99  96]
 [ 99  97]
 [ 99  98]
 [ 99  99]
 [ 99 100]
 [ 99 101]
 [ 99 102]
 [ 99 103]
 [ 99 104]
 [100  95]
 [100  96]
 [100  97]
 [100  98]
 [100  99]
 [100 100]
 [100 101]
 [100 102]
 [100 103]
 [100 104]
 [100 105]
 [101  96]
 [101  97]
 [101  98]
 [101  99]
 [101 100]
 [101 101]
 [101 102]
 [101 103]
 [101 104]
 [102  96]
 [102  97]
 [102  98]
 [102  99]
 [102 100]
 [102 101]
 [102 102]
 [102 103]
 [102 104]
 [103  97]
 [103  98]
 [103  99]
 [103 100]
 [103 101]
 [103 102]
 [103 103]
 [104  98]
 [104  99]
 [104 100]
 [104 101]
 [104 102]
 [105 100]]

也许使用 simpleblobdetector,上面有一个 stackoverflow 帖子:

如何使用 OpenCV SimpleBlobDetector

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM