简体   繁体   English

显示object跟踪包围盒的中心坐标

[英]Show the center coordinate of the object tracking bounding box

I would like to know how I can print the tracking coordinate rectangle onto the video.我想知道如何将跟踪坐标矩形打印到视频上。 I want the coordinate of the center point of the rectangle and, of course, I want: if the tracker is moving, the coordinate is also updated.我想要矩形中心点的坐标,当然,我想要:如果跟踪器在移动,坐标也会更新。

I tried to use:我尝试使用:

 cv2.putText(frame, **??** , (100,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2);

somewhat like this inside the loop, but I cannot figure it out.在循环中有点像这样,但我无法弄清楚。

import cv2
import sys

(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')

if __name__ == '__main__' :

    # Set up tracker.
    # Instead of MIL, you can also use

    tracker_types = ['BOOSTING', 'MIL','KCF', 'TLD', 'MEDIANFLOW', 'GOTURN', 'MOSSE', 'CSRT']
    tracker_type = tracker_types[2]

    if int(minor_ver) < 3:
        tracker = cv2.Tracker_create(tracker_type)
    else:
        if tracker_type == 'BOOSTING':
            tracker = cv2.TrackerBoosting_create()
        if tracker_type == 'MIL':
            tracker = cv2.TrackerMIL_create()
        if tracker_type == 'KCF':
            tracker = cv2.TrackerKCF_create()
        if tracker_type == 'TLD':
            tracker = cv2.TrackerTLD_create()
        if tracker_type == 'MEDIANFLOW':
            tracker = cv2.TrackerMedianFlow_create()
        if tracker_type == 'GOTURN':
            tracker = cv2.TrackerGOTURN_create()
        if tracker_type == 'MOSSE':
            tracker = cv2.TrackerMOSSE_create()
        if tracker_type == "CSRT":
            tracker = cv2.TrackerCSRT_create()

    # Read video
    video = cv2.VideoCapture('video.mp4')

    # Exit if video not opened.
    if not video.isOpened():
        print ("Could not open video")
        sys.exit()

    # Read first frame.
    ok, frame = video.read()
    if not ok:
        print ('Cannot read video file')
        sys.exit()
    
    # Define an initial bounding box
    bbox = (287, 23, 86, 320)

    # Uncomment the line below to select a different bounding box
    bbox = cv2.selectROI(frame, False)

    # Initialize tracker with first frame and bounding box
    ok = tracker.init(frame, bbox)

    while True:
        # Read a new frame
        ok, frame = video.read()
        if not ok:
            break
        
        # Start timer
        timer = cv2.getTickCount()

        # Update tracker
        ok, bbox = tracker.update(frame)

        # Calculate Frames per second (FPS)
        fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer);

        # Draw bounding box
        if ok:
            # Tracking success
            p1 = (int(bbox[0]), int(bbox[1]))
            p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
            cv2.rectangle(frame, p1, p2, (255,0,0), 2, 1)
        else :
            # Tracking failure
            cv2.putText(frame, "Tracking failure detected", (100,80), cv2.FONT_HERSHEY_SIMPLEX, 0.75,(0,0,255),2)

        # Display tracker type on frame
        cv2.putText(frame, tracker_type + " Tracker", (100,20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50),2);
    
        # Display FPS on frame
        cv2.putText(frame, "FPS : " + str(int(fps)), (100,50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50,170,50), 2);

        # Display result
        cv2.imshow("Tracking", frame)

        # Exit if ESC pressed
        k = cv2.waitKey(1) & 0xff
        if k == 27 : break

You can obtain the center of rectangle by dividing across the length and breadth of the rectangle obtained from variables p1 and p2 .您可以通过划分从变量p1p2获得的矩形的长度和宽度来获得矩形的中心。

Coordinate: p1 --> (x1, y1)坐标: p1 --> (x1, y1)

Coordinate: p2 --> (x2, y2)坐标: p2 --> (x2, y2)

To find the center of the rectangle divide (x1+x2)/2 and (y1+y2)/2找到矩形除以(x1+x2)/2(y1+y2)/2的中心

rectangle_center = (int(p1[0] + p2[0])/2, int(p1[1] + p2[1])/2)

Within cv2.putText() place the above variable as a string --> str(rectangle_center)cv2.putText()中将上述变量作为字符串放置 --> str(rectangle_center)

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

相关问题 如何在Python中找到旋转边界框的坐标? - How to find coordinate of a rotated bounding box in Python? 实时 object 跟踪 - 如何让视频在开始播放,让用户暂停,绘制边界框,然后开始跟踪? - Real time object tracking - how to let the video play in the beginning, let the user pause it, draw the bounding box, and then begin the tracking? 获取边界框的上中心 opencv - get the upper center of bounding box opencv 如何使用 Tensorflow 在视频中的 object 检测中获取预测边界框的坐标(甚至中心点) - How to get coordinates(or even center point) of predicted bounding box in object detection in a video using Tensorflow 检查一个 object 的边界框是否在另一个 object 的边界框内 - Check to see if a bounding box of an object is inside the bounding box of another object 从边界框获取对象 [对象检测] - Get object from bounding box [Object Detection] 在视频中的对象上创建边界框 - Creating Bounding Box across an object in a video 检索geodjango多面体对象的边界框 - retrieve bounding box of a geodjango multipolygon object 获取边界框内的 object 轮廓 - Obtaining object contour inside bounding box 创建对象检测边界框的最佳方法 - Best Way to create a bounding box for object detection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM