gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 17:26:51 27 4
gpt4 key购买 nike

在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。这是一个例子:

输入:

enter image description here

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]]

关于python - 如何在图像中找到形状的坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58785975/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com