You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Python与OpenCV将足球场红色场地线条叠加至比赛图像的代码修改咨询

How to Overlay Pitch Red Lines onto Original Match Image with OpenCV and Python

Got it, let's fix this so you can draw those red pitch lines from pitch.png directly onto your original 36.jpg match image, just like a drawing tool would. The key here is reversing the perspective transformation you were doing before—instead of warping the match image to the pitch's top-down view, we'll warp the pitch lines to the match image's perspective, then overlay them neatly.

Here's the breakdown of the changes you need:

  • Calculate the inverse homography matrix: Your original code computes the transform from the match image to the pitch view. We need the opposite: mapping the pitch's coordinates back to the match image's perspective.
  • Handle the PNG's transparent background: Since pitch.png has no white background, we'll use its alpha channel to create a mask—this lets us only overlay the red lines, not a solid background.
  • Warp the pitch lines to the match image's perspective: Apply the inverse homography to the pitch image to align it with the match scene.
  • Overlay the warped lines onto the original image: Use the alpha mask to blend the lines smoothly without covering up the original match content.

Modified Code

import cv2
import numpy as np

if __name__ == '__main__':
    # Read source match image
    im_src = cv2.imread('c:/36.jpg')
    # Get the original match image dimensions
    src_h, src_w = im_src.shape[:2]

    # Four corners of the right penalty area in the match image (your original pts_src)
    pts_src_match = np.array([[314, 108], [693, 108], [903, 493], [311, 490]], dtype=np.float32)
    # Four corners of the right penalty area in the top-down pitch image (your original pts_dst)
    pts_dst_pitch = np.array([[480, 76], [569, 76], [569, 292], [480, 292]], dtype=np.float32)

    # Calculate homography FROM pitch TO match image (inverse of your original transform)
    h_pitch_to_match, _ = cv2.findHomography(pts_dst_pitch, pts_src_match)

    # Read pitch image with alpha channel (since it's PNG)
    im_pitch = cv2.imread('c:/pitch.png', cv2.IMREAD_UNCHANGED)
    # Split into BGR channels and alpha mask
    pitch_bgr = im_pitch[:, :, :3]
    pitch_alpha = im_pitch[:, :, 3]

    # Warp the pitch image (and its alpha mask) to the match image's perspective
    warped_pitch = cv2.warpPerspective(pitch_bgr, h_pitch_to_match, (src_w, src_h))
    warped_alpha = cv2.warpPerspective(pitch_alpha, h_pitch_to_match, (src_w, src_h))

    # Convert alpha mask to a float mask for blending (0-1 range)
    alpha_mask = warped_alpha / 255.0

    # Overlay the warped pitch lines onto the original match image
    # Use the alpha mask to only blend the red lines, keeping the original image intact elsewhere
    for c in range(3):
        im_src[:, :, c] = (1 - alpha_mask) * im_src[:, :, c] + alpha_mask * warped_pitch[:, :, c]

    # Display the result
    cv2.imshow("Match Image with Pitch Lines", im_src)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

Key Explanations:

  • Inverse Homography: By swapping pts_dst_pitch and pts_src_match in cv2.findHomography, we get a matrix that transforms the top-down pitch coordinates to match the match image's perspective.
  • Alpha Channel Handling: Reading the pitch image with cv2.IMREAD_UNCHANGED preserves the alpha channel, which tells us which parts are the red lines (opaque) and which are transparent.
  • Blending: Instead of just pasting the warped pitch image, we use the alpha mask to blend the lines with the original image—this ensures the lines sit on top without covering up the players or field details underneath.

内容的提问来源于stack exchange,提问作者payam mohammadi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 17:37:37