如何在OpenCV Python中查找特定地标点的坐标

wlwcrazw  于 2022-12-19  发布在  Python
关注(0)|答案(1)|浏览(143)

我想得到一个物体在移动的地标点的坐标。我试着先检测选择一个点。我是OpenCV和python的初学者。不知道任何函数是否存在。

import cvzone
import numpy as np
from cvzone.FaceMeshModule import FaceMeshDetector
from cvzone.PlotModule import LivePlot


idList = [8]
 
cap = cv2.VideoCapture(0)
detector = FaceMeshDetector(maxFaces=1)

while True:
 
    if cap.get(cv2.CAP_PROP_POS_FRAMES) == cap.get(cv2.CAP_PROP_FRAME_COUNT):
        cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
 
    success, img = cap.read()
    img, faces = detector.findFaceMesh(img, draw = False)
    
    if faces:
        face = faces[0]
        leftUp = face[8]
        
        for id in idList:
            cv2.circle(img, face[id], 3,(80,200,120), cv2.FILLED)

    
    img = cv2.resize(img,(640,360))
    cv2.imshow("image", img)
    cv2.waitKey(25)
         
cap.release()
cv2.destroyAllWindows()
4c8rllxm

4c8rllxm1#

如果我没理解错的话,你想检测标志中是否有特定的点在移动.
要做到这一点,我建议首先确定地标的索引,我找不到具体的文档,但您可以使用以下代码段来识别它:

import cv2
from cvzone.FaceMeshModule import FaceMeshDetector

cap = cv2.VideoCapture(0)
detector = FaceMeshDetector(maxFaces=1)

def click_event(event, x, y, faces, detector):
        if event == cv2.EVENT_LBUTTONDOWN and faces[0]:
            lstOfPoints = [detector.findDistance(faces[0][i], (x, y))[0] for i in range(0, 468)]
            print(lstOfPoints.index(min(lstOfPoints)))

while True:
        _, img = cap.read()
        img, faces = detector.findFaceMesh(img, draw=False)
        if faces:
            face = faces[0]
            for i in range(0, 468):
                    cv2.circle(img, (face[i][0], face[i][1]), 1, (0, 255, 0), cv2.FILLED)
            cv2.imshow("Image", img)
            cv2.setMouseCallback('Image', lambda event, x , y, flags, params :click_event(event, x, y, faces, detector))
            cv2.waitKey(1)
        else:
            cv2.imshow("Image", img)
            cv2.waitKey(1)

您可以单击要标识的点,以便打印其索引。
一旦你得到了你要找的点,你可以这样改变代码:

import cv2
from cvzone.FaceMeshModule import FaceMeshDetector
import math
cap = cv2.VideoCapture(0)
detector = FaceMeshDetector(maxFaces=1)
idxOfPoint = 50
i = 10
while True:
        _, img = cap.read()
        img, faces = detector.findFaceMesh(img, draw=False)
        if faces:
            if i != 10 and i % 30 == 0:
                distanceChange = math.sqrt((faces[0][idxOfPoint][0] - pastCoord[0])**2 + (faces[0][idxOfPoint][1] - pastCoord[1])**2)
                print("point moved" if distanceChange > 20 else "point didn't move")
                pastCoord  = faces[0][idxOfPoint]
            if i == 10:
                pastCoord  = faces[0][idxOfPoint]
            cv2.circle(img, (faces[0][idxOfPoint][0], faces[0][idxOfPoint][1]), 1, (0, 255, 0), cv2.FILLED) 
            cv2.imshow("Image", img)
            cv2.waitKey(1)
            i += 1
        else:
            cv2.imshow("Image", img)
            cv2.waitKey(1)

其中,30(单位:i % 30)代表每帧的验证频率,20(单位:distanceChange > 20)代表触发打印的最小距离变化。

相关问题