python - 如何检测质心并测量两个物体之间的距离?使用鸟瞰?
问题描述
我想检测移动的人及其质心(不使用质心跟踪器),我有走路人的视频,
这是我的代码
import cv2
face_cascade=cv2.CascadeClassifier('haarcascade_fullbody.xml')
frame=cv2.imread('person.png')
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 3)
cX = int((x+ w) / 2.0)
cY = int((y + h) / 2.0)
print(cX)
print(cY)
cv2.circle(frame, (cX, cY), 10, (205, 0, 255), -1)
cv2.imshow('img',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cv2.waitKey()
cv2.destroyAllWindows()
解决方案
这是您可以检测人并找到其质心+距离的代码。
import cv2
import math
face_cascade=cv2.CascadeClassifier('haarcascade_fullbody.xml')
frame=cv2.imread('dis456.png')
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
print(faces)
p1 = 361 # image pont
p2 = 237
cv2.circle(frame, (p1, p2), 5, ((139,0,0)), -1)
for face in faces:
(x, y, w, h) = face
(x1, y1, w1, h1) = faces[0]
cx1, cy1 = int(x + w / 2), int(y + h / 2)
cx2, cy2 = int(x1 + w1 / 2), int(y1 + h1 / 2)
cv2.circle(frame, (cx1, cy1), 2, (0, 245, 30), -1)
cv2.circle(frame, (cx2, cy2), 2, (0, 245, 30), -1)
# dist=math.sqrt(((cx1-cx2)**2)+(cy2-cy1)**2))
cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 3)
dis = (math.sqrt(((cx2 - cx1) ** 2) + ((cy2 - cy1) ** 2)) ** 0.5)
dis = int(dis)
print("distance is ",dis)
cv2.line(frame, (cx1, cy1), (p1,p2 ), (0, 234, 76), 2)
(x,y,w,h)=face
cv2.putText(frame, 'distance={}'.format(dis), (x, y), cv2.FONT_HERSHEY_COMPLEX, 0.6,
(139, 0, 0))
cv2.imshow('img',frame)
cv2.waitKey(0)
cv2.destroyAllWindows()
推荐阅读
- python - 尝试编译 Python 可执行文件时遇到奇怪的错误
- python - 我正在关注 [Python + OpenCV + mediapipe] Hand Tracking 的教程,但出现以下错误:
- python - 在 python 中绘图时遇到问题
- python - 使用光栅显示 tiff 图像的特定部分,而无需加载整个文件
- r - 尝试进行主题建模时,R 上的错误消息:“未找到数据集 'X'”,尽管我已经将该数据用于其他技术
- python - 如何将多个单独的图形放入一个 Tkinter 窗口?
- assembly - 从用户那里获取输入并在程序集中显示(nasm)
- python - 使用 OpenCV-Python 进行对象检测
- python - Django perform_update 不更新条目
- python - 通过调用字典中的值获取嵌套字典中的键