[英]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 帖子:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.