简体   繁体   English

OpenCV 透视变换

[英]OpenCV perspective transform

I am currently working on creating a python software that tracks players on a soccer field.我目前正在开发一个 python 软件来跟踪足球场上的球员。 I got the player detection working with YoloV3 and was able to output quite a nice result with players centroids and boxes drawn.我让玩家检测与 YoloV3 一起工作,并且能够在绘制玩家质心和框的情况下 output 取得相当不错的结果。 What i want to do now is translate the players position and project their centroids onto a png/jpg of a soccerfield.我现在要做的是翻译球员 position 并将他们的质心投影到足球场的 png/jpg 上。 For this I inteded to use two arrays with refrence points one for the soccerfield-image and one for the source video.为此,我打算使用两个 arrays,其中一个用于足球场图像,一个用于源视频。 But my question now is how do I translate the coordinates of the centroids to the soccerfield image.但我现在的问题是如何将质心的坐标转换为足球场图像。

Similiar example: Example How the boxes and Markers are drawn:类似示例:示例框和标记的绘制方式:

def draw_labels_and_boxes(img, boxes, confidences, classids, idxs, colors, labels):
    # If there are any detections
    if len(idxs) > 0:
        for i in idxs.flatten():
            # Get the bounding box coordinates
            x, y = boxes[i][0], boxes[i][1]
            w, h = boxes[i][2], boxes[i][3]

            # Draw the bounding box rectangle and label on the image
            cv.rectangle(img, (x, y), (x + w, y + h), (255, 255, 255), 2)
            cv.drawMarker (img, (int(x + w / 2), int(y + h / 2)), (x, y), 0, 20, 3)
    return img

Boxes are generated like this:盒子是这样生成的:

def generate_boxes_confidences_classids(outs, height, width, tconf):
    boxes = []
    confidences = []
    classids = []

    for out in outs:
        for detection in out:
            # print (detection)
            # a = input('GO!')

            # Get the scores, classid, and the confidence of the prediction
            scores = detection[5:]
            classid = np.argmax(scores)
            confidence = scores[classid]

            # Consider only the predictions that are above a certain confidence level
            if confidence > tconf:
                # TODO Check detection
                box = detection[0:4] * np.array([width, height, width, height])
                centerX, centerY, bwidth, bheight = box.astype('int')

                # Using the center x, y coordinates to derive the top
                # and the left corner of the bounding box
                x = int(centerX - (bwidth / 2))
                y = int(centerY - (bheight / 2))

                # Append to list
                boxes.append([x, y, int(bwidth), int(bheight)])
                confidences.append(float(confidence))
                classids.append(classid)

    return boxes, confidences, classids

Assuming a stationary camera,假设一个静止的相机,

  • Find the coordinates of the four corners of the field.求场地四个角的坐标。
  • Find the corresponding four corners in the top-view image that you want to create.在要创建的顶视图图像中找到相应的四个角。
  • Find a homography matrix using these two sets of points.使用这两组点找到一个单应矩阵。 You can use OpenCV's findHomography for this.您可以为此使用 OpenCV 的findHomography
  • Transform all the centroids using this homography matrix and that should give you your coordinates in the new image space.使用此单应矩阵变换所有质心,这应该会为您提供新图像空间中的坐标。 You can use warpPerspective for doing this.您可以使用warpPerspective来执行此操作。

Recently during COVID19 pandemic many developers have developed "social-distancing-monitoring-system".最近在 COVID19 大流行期间,许多开发人员开发了“社交距离监控系统”。 There a few of them also developed "Bird's Eye View" of the system.他们中的一些人还开发了“鸟瞰”系统。 Your problem is just similar.你的问题很相似。 As external links are not accepted here, so I am not able to post the exact link(s).由于此处不接受外部链接,因此我无法发布确切的链接。 Please check their codes in GitHub.请在 GitHub 中查看他们的代码。

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

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