简体   繁体   English

OpenCV Python激光点跟踪提取x和y坐标并将其存储到不同的变量

[英]OpenCV Python laser dot tracking extracting x and y coordinates and store it to different variables

enter image description here I have a code here that tracks a laser dot.What I want is to get the x and y coordinates of the laser dot and store it to separate variables. 在这里输入图像描述这里有一个跟踪激光点的代码。我想要的是获取激光点的x和y坐标并将其存储到单独的变量中。 Here is the code: 这是代码:

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while (1):

    # Take each frame
    ret, frame = cap.read()
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    lower_red = np.array([0, 0, 255])
    upper_red = np.array([255, 255, 255])
    mask = cv2.inRange(hsv, lower_red, upper_red)
    cv2.imshow('mask', mask)
    cv2.imshow('Track Laser', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

For multiple points or noisier data, you might want to consider clustering algorithms. 对于多点或嘈杂的数据,您可能需要考虑聚类算法。

However, the image you attached is quite clear. 但是,您附加的图像非常清晰。 All you need to do is find the center of it. 您需要做的就是找到它的中心。 That corresponds the the first geometrical moment (aka mean): 这对应于第一个几何矩(即均值):

moments = cv2.moments(hsv[:, :, 2])
x = int(moments['m10'] / moments['m00'])
y = int(moments['m01'] / moments['m00'])

This lets us find the center fairly accurately, provided there are no outliers (eg noise or other circles) affecting the distribution. 只要不存在影响分布的异常值(例如噪声或其他圆圈),我们就可以相当准确地找到中心。

在此处输入图片说明


Perhaps a more robust method (to noise and number of circles) would make use of Hough circles. 也许更健壮的方法(去噪和圈数)将使用霍夫圆。

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

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