增加部署代码

This commit is contained in:
琉璃月光
2025-09-05 14:29:33 +08:00
parent ad52ab9125
commit 471c718d42
951 changed files with 14072 additions and 264 deletions

39
LK/GMM_IMAGE.py Normal file
View File

@ -0,0 +1,39 @@
import cv2
import numpy as np
import glob
import os
# 图片文件夹路径
img_dir = r"/home/hx/yolo/LK/frames"
img_files = sorted(glob.glob(os.path.join(img_dir, "*.jpg")))
print("找到图片数量:", len(img_files))
if len(img_files) == 0:
raise FileNotFoundError("❌ 没有找到图片,请检查路径和后缀!")
alpha = 0.01 # 学习率
mean_bg = None
for idx, img_path in enumerate(img_files):
frame = cv2.imread(img_path)
if frame is None:
print(f"⚠️ 跳过无法读取的图片: {img_path}")
continue
frame = frame.astype(np.float32)
if mean_bg is None:
mean_bg = frame.copy()
else:
mean_bg = (1 - alpha) * mean_bg + alpha * frame
# 每张都打印
print(f"处理进度: {idx+1}/{len(img_files)}")
# 最终背景图
bg_image = mean_bg.astype(np.uint8)
# 保存到 frames 文件夹下
save_path = os.path.join(img_dir, "background.jpg")
cv2.imwrite(save_path, bg_image)
print(f"✅ 背景图已保存: {save_path}")

30
LK/GMM_video.py Normal file
View File

@ -0,0 +1,30 @@
import cv2
import numpy as np
# 创建背景建模器 (MOG2)
bg_subtractor = cv2.createBackgroundSubtractorMOG2(history=500, varThreshold=16, detectShadows=True)
cap = cv2.VideoCapture("your_video.mp4") # 或者用图片序列
while True:
ret, frame = cap.read()
if not ret:
break
# 应用背景建模器
fg_mask = bg_subtractor.apply(frame)
# 获取背景图像
bg_image = bg_subtractor.getBackgroundImage()
# 显示
cv2.imshow("Frame", frame)
cv2.imshow("Foreground Mask", fg_mask)
if bg_image is not None:
cv2.imshow("Background Model", bg_image)
if cv2.waitKey(30) & 0xFF == 27:
break
cap.release()
cv2.destroyAllWindows()

0
LK/LK.py Normal file
View File

BIN
LK/b.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 632 KiB

BIN
LK/b1.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 576 KiB

BIN
LK/background.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 992 KiB

63
LK/divid_diff.py Normal file
View File

@ -0,0 +1,63 @@
import cv2
import numpy as np
def simple_background_subtraction(background_path, foreground_path, output_path, threshold=30):
"""
使用前后两张图做差分,分离前景
"""
bg = cv2.imread(background_path)
fg_img = cv2.imread(foreground_path)
if bg is None or fg_img is None:
raise FileNotFoundError("图片没找到!检查路径")
# 确保两图大小一致
if bg.shape != fg_img.shape:
print("⚠️ 图片大小不一致,正在调整...")
fg_img = cv2.resize(fg_img, (bg.shape[1], bg.shape[0]))
# 转灰度 + 差分
bg_gray = cv2.cvtColor(bg, cv2.COLOR_BGR2GRAY)
fg_gray = cv2.cvtColor(fg_img, cv2.COLOR_BGR2GRAY)
diff = cv2.absdiff(fg_gray, bg_gray)
# 二值化
_, mask = cv2.threshold(diff, threshold, 255, cv2.THRESH_BINARY)
# 形态学去噪
kernel = np.ones((5,5), np.uint8)
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
# 提取前景:背景变白
result_white = fg_img.copy()
result_white[mask == 0] = [255, 255, 255] # 背景白
# 提取前景背景透明PNG
b, g, r = cv2.split(fg_img)
alpha = np.where(mask > 0, 255, 0).astype(np.uint8)
result_transparent = cv2.merge([b, g, r, alpha])
# 保存
cv2.imwrite(output_path + "_white.jpg", result_white)
cv2.imwrite(output_path + "_transparent.png", result_transparent)
print(f"✅ 保存成功:\n {output_path}_white.jpg\n {output_path}_transparent.png")
# 显示(可选)
cv2.imshow('Original', fg_img)
cv2.imshow('Diff', diff)
cv2.imshow('Mask', mask)
cv2.imshow('Foreground', result_white)
cv2.waitKey(0)
cv2.destroyAllWindows()
# === 使用 ===
if __name__ == '__main__':
simple_background_subtraction(
background_path='f.jpg',
foreground_path='b.jpg',
output_path='output/foreground',
threshold=30
)

BIN
LK/f.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 616 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.1 MiB

BIN
LK/foreground_white_bg.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 654 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 519 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 540 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 559 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 571 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 577 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 601 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 563 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 587 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 604 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 595 KiB