Long jump results display program based on OpenCV and PyQt5

Long jump results display program based on OpenCV and PyQt5

In recent years, more and more people pay attention to sports, among which long jump is a high-profile sport. In order to better display the athletes' long jump results, this article will introduce a method for realizing the long jump results display program based on OpenCV and PyQt5.

The long jump results display program in this paper mainly includes two parts: one is to calculate the jumping distance of the athlete by reading the image in the video; the other is to display the video of the athlete jumping and the result of the jumping distance on the interface. Below we will introduce the implementation methods of these two parts respectively.

1. Calculate the jump distance

1. Get the background image

The program initially needs to obtain the background image of the jumping field. In order to facilitate the calculation of the jump distance, we need to convert the background image into a grayscale image and perform Gaussian blur processing.

self.cap = cv2.VideoCapture('跳远3.mp4')
_, self.bg_frame = self.cap.read()
self.bg_frame = cv2.cvtColor(self.bg_frame, cv2.COLOR_BGR2GRAY)
self.bg_frame = cv2.GaussianBlur(self.bg_frame, (21, 21), 0)

2. Read the video frame and calculate the jump distance

When the program reads each frame of video image, it needs to convert it into a grayscale image and perform Gaussian blur processing. Then, the foreground image is obtained by background subtraction and binarization, and the largest contour is found in the foreground image. Finally, the jump distance is calculated based on the length of the major axis of the ellipse of the largest contour.

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (21, 21), 0)
# 背景减除
diff = cv2.absdiff(self.bg_frame, gray)
thresh = cv2.threshold(diff, 25, 255, cv2.THRESH_BINARY)[1]
# 查找轮廓
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# 绘制轮廓并计算跳跃距离
if len(contours) > 0:
    c = max(contours, key=cv2.contourArea)
    cv2.drawContours(frame, [c], 0, (0, 255, 0), 2)
    (x, y), radius = cv2.minEnclosingCircle(c)
    cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 0), 2)
    if len(c) > 4:
        ellipse = cv2.fitEllipse(c)
        cv2.ellipse(frame, ellipse, (0, 255, 0), 2)
        self.distance = ellipse[1][0] * 0.025

2. Interface display

The interface display of the program is mainly realized through the PyQt5 library. We add a QLabel to the interface to display the jump video, and add another QLabel to display the jump distance result. At the same time, we also need to add a timer to regularly refresh the jump video and jump distance results.

class Main(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()

        self.video = Video()
        self.initUI()

    def initUI(self):
        self.setGeometry(100, 100, 1200, 700)
        self.setWindowTitle('Jumping Distance')

        # 添加用于显示视频的QLabel
        self.video_label = QtWidgets.QLabel(self)
        self.video_label.setGeometry(QtCore.QRect(100, 50, 1000, 600))
        self.video_label.setFrameShape(QtWidgets.QFrame.Box)

        # 添加用于显示跳跃距离的QLabel
        self.result_label = QtWidgets.QLabel(self)
        self.result_label.setGeometry(QtCore.QRect(275, 30, 300, 100))
        font = QtGui.QFont()
        font.setPointSize(20)
        font.setBold(True)
        font.setWeight(75)
        self.result_label.setFont(font)
        self.result_label.setAlignment(QtCore.Qt.AlignCenter)
        self.result_label.setObjectName("result_label")

        # 添加一个定时器
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.updateFrame)
        self.timer.start(1)

Every time the timer fires, we will call the readFrame method of the Video class to get the video image and display it on the QLabel. At the same time, we also need to update the jump distance result and display it on the QLabel.

def updateFrame(self):
    frame = self.video.readFrame()
    if frame is not None:
        # 将Qt格式的图像显示在QLabel上
        self.video_label.setPixmap(QtGui.QPixmap.fromImage(frame))

        # 在self.result_label上显示跳跃距离结果
        self.result_label.setText("跳跃距离:%.2f 米" % self.video.distance)

Through the above method, we can realize the function of the long jump results display program. When using the program, we only need to place the video file and the code file in the same directory, and run the program. During the running of the program, we can see the video of the athlete jumping and the result of the jumping distance in real time, so as to better display the athlete's jumping results.

3. Effect display

insert image description here

Summarize

This article introduces a method for realizing the long jump results display program based on Python and OpenCV library. By calculating the jumping distance of the athlete in the video, and displaying the jumping video and jumping distance results in real time on the interface, the athlete's long jump results can be better displayed.

Guess you like

Origin blog.csdn.net/qq_46556714/article/details/130830793