chore: 更新最新代码
This commit is contained in:
63
image/del_photo/ssim.py
Normal file
63
image/del_photo/ssim.py
Normal file
@ -0,0 +1,63 @@
|
||||
import cv2
|
||||
from skimage.metrics import structural_similarity as ssim
|
||||
import os
|
||||
|
||||
def calculate_ssim(image_path1, image_path2):
|
||||
"""
|
||||
计算两张图片的 SSIM 相似度
|
||||
"""
|
||||
# 读取图像
|
||||
img1 = cv2.imread(image_path1)
|
||||
img2 = cv2.imread(image_path2)
|
||||
|
||||
if img1 is None:
|
||||
print(f"❌ 无法读取图片1: {image_path1}")
|
||||
return None
|
||||
if img2 is None:
|
||||
print(f"❌ 无法读取图片2: {image_path2}")
|
||||
return None
|
||||
|
||||
# 转为灰度图
|
||||
gray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
|
||||
gray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
|
||||
|
||||
# 确保尺寸一致
|
||||
if gray1.shape != gray2.shape:
|
||||
print("⚠️ 图像尺寸不一致,正在调整...")
|
||||
h, w = min(gray1.shape[0], gray2.shape[0]), min(gray1.shape[1], gray2.shape[1])
|
||||
gray1 = cv2.resize(gray1, (w, h))
|
||||
gray2 = cv2.resize(gray2, (w, h))
|
||||
|
||||
# 计算 SSIM
|
||||
try:
|
||||
similarity = ssim(gray1, gray2)
|
||||
return similarity
|
||||
except Exception as e:
|
||||
print(f"❌ SSIM 计算失败: {e}")
|
||||
return None
|
||||
|
||||
|
||||
# ==================== 使用示例 ====================
|
||||
if __name__ == "__main__":
|
||||
# 替换成你本地的两张图片路径
|
||||
path1 = "/home/hx/桌面/image/image/frame_20250805_120334_585.jpg"
|
||||
path2 = "/home/hx/桌面/image/image/frame_20250805_120334_570.jpg"
|
||||
|
||||
# 检查文件是否存在
|
||||
if not os.path.exists(path1):
|
||||
print(f"文件不存在: {path1}")
|
||||
print("请修改 path1 为实际存在的图片路径")
|
||||
elif not os.path.exists(path2):
|
||||
print(f"文件不存在: {path2}")
|
||||
print("请修改 path2 为实际存在的图片路径")
|
||||
else:
|
||||
print("正在计算 SSIM...")
|
||||
sim = calculate_ssim(path1, path2)
|
||||
if sim is not None:
|
||||
print(f"✅ SSIM 相似度: {sim:.4f}")
|
||||
if sim > 0.9:
|
||||
print("🔴 太相似(>0.9),应跳过重复帧")
|
||||
elif sim > 0.7:
|
||||
print("🟡 较相似,内容变化不大")
|
||||
else:
|
||||
print("🟢 差异明显,建议保存")
|
||||
Reference in New Issue
Block a user