简体   繁体   中英

how to find the cordinates of a shape in a image

in a BGR image, there is a red color circle, which i have to detect and find its cordinates.

i have converted the bgr image to hsv, then using upper and lower limit of red seperated the red color from image, now how to find the cordinates of that red circle

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)

Using Python/OpenCV/Numpy, you can use np.where or better np.argwhere. Here is an example:

Input:

在此处输入图像描述

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)


Results:

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

Maybe use simpleblobdetector, there is a stackoverflow post on it:

How to use OpenCV SimpleBlobDetector

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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