|
|
import cv2 |
|
|
import numpy as np |
|
|
|
|
|
video_path = "/mnt/prev_nas/qhy/MagicMotion/assets/mask_trajectory/mammoth_rhino.mp4" |
|
|
|
|
|
cap = cv2.VideoCapture(video_path) |
|
|
ret, frame = cap.read() |
|
|
cap.release() |
|
|
if not ret: |
|
|
raise RuntimeError("无法读取第一帧") |
|
|
|
|
|
|
|
|
pink_bgr = np.array([77, 80, 119], dtype=np.float32) |
|
|
red_bgr = np.array([33, 37, 117], dtype=np.float32) |
|
|
|
|
|
|
|
|
T_pink = 30.0 |
|
|
T_red = 30.0 |
|
|
|
|
|
|
|
|
img = frame.astype(np.float32) |
|
|
pixels = img.reshape(-1, 3) |
|
|
|
|
|
|
|
|
dist_to_pink = np.linalg.norm(pixels - pink_bgr, axis=1) |
|
|
dist_to_red = np.linalg.norm(pixels - red_bgr, axis=1) |
|
|
|
|
|
pink_mask = (dist_to_pink < T_pink).reshape(frame.shape[:2]) |
|
|
red_mask = (dist_to_red < T_red ).reshape(frame.shape[:2]) |
|
|
|
|
|
|
|
|
both = pink_mask & red_mask |
|
|
if both.any(): |
|
|
closer_to_pink = dist_to_pink[both.reshape(-1)] <= dist_to_red[both.reshape(-1)] |
|
|
|
|
|
pink_idx = np.where(both) |
|
|
|
|
|
i = 0 |
|
|
for y, x in zip(*pink_idx): |
|
|
if closer_to_pink[i]: |
|
|
red_mask[y, x] = False |
|
|
else: |
|
|
pink_mask[y, x] = False |
|
|
i += 1 |
|
|
|
|
|
|
|
|
vis = np.zeros_like(frame) |
|
|
vis[pink_mask] = (255, 0, 255) |
|
|
vis[red_mask] = (0, 0, 255) |
|
|
|
|
|
cv2.imwrite("first_frame_bgr_distance.png", vis) |
|
|
print("已保存: first_frame_bgr_distance.png") |
|
|
|