Psnr对比图片画质

3.1 典型评价尺度 PSNR范围(dB) 图像质量评价 >40 极好(肉眼不可辨差异) 30-40 良好(可接受质量) 20-30 较差(明显失真) <20 严重失真(不可接受) 1import numpy as np 2import cv2 3import matplotlib.pyplot as plt 4 5 6"""计算两幅RGB图像的PSNR值 7参数: 8 img1: 原始图像(numpy数组) 9 img2: 待评估图像(numpy数组) 10返回: 11 PSNR值(dB) 12""" 13def rgb_psnr(img1, img2): 14 # 确保输入是numpy数组 15 img1 = np.array(img1, dtype=np.float64) 16 img2 = np.array(img2, dtype=np.float64) 17 # 计算各通道MSE 18 mse_r = np.mean((img1[:, :, 0] - img2[:, :, 0]) ** 2) 19 mse_g = np.mean((img1[:, :, 1] - img2[:, :, 1]) ** 2) 20 mse_b = np.mean((img1[:, :, 2] - img2[:, :, 2]) ** 2) 21 # 计算平均MSE 22 mse = (mse_r + mse_g + mse_b) / 3 23 # 处理完全相同的图像 24 if mse == 0: 25 return float('inf') 26 return 20 * np.log10(255 / np.sqrt(mse)) 27 28 29def psnr(img1, img2): 30 img1 = img1.astype(np.float64) 31 img2 = img2.astype(np.float64) 32 33 mse = np.mean((img1 - img2) ** 2) 34 if mse == 0: 35 return float('inf') 36 37 if img1.max() > 1: 38 max_pixel = 255.0 39 else: 40 max_pixel = 1.0 41 42 return 20 * np.log10(max_pixel / np.sqrt(mse)) 43 44 45if __name__ == "__main__": 46 # 1. 读取原始图像 47 original = cv2.imread('../../mario.png') # BGR格式 48 original = cv2.cvtColor(original, cv2.COLOR_BGR2RGB) # 转换为RGB 49 50 # 2. 创建测试图像(添加高斯噪声) 51 noisy = original + np.random.normal(0, 25, original.shape) 52 noisy = np.clip(noisy, 0, 255).astype(np.uint8) 53 54 # 3. 计算PSNR 55 # psnr_value = rgb_psnr(original, noisy) 56 psnr_value = psnr(original, noisy) 57 print(f"PSNR between original and noisy image: {psnr_value:.2f} dB") 58 59 # 4. 可视化比较 60 plt.figure(figsize=(12, 6)) 61 62 plt.subplot(1, 2, 1) 63 plt.imshow(original) 64 plt.title('Original Image') 65 plt.axis('off') 66 67 plt.subplot(1, 2, 2) 68 plt.imshow(noisy) 69 plt.title(f'Noisy Image (PSNR={psnr_value:.2f}dB)') 70 plt.axis('off') 71 72 plt.tight_layout() 73 plt.show()

October 2, 2025 · 1 分钟阅读 · 技术文档 -- 次访问

SSIM对比图片相似度

对比图片相似度, 相似度越接近1 表示相似度越高, ...

October 2, 2025 · 2 分钟阅读 · 技术文档 -- 次访问

Python日期处理工具类深度解析

核心功能实现 1. 基础时间获取 1@staticmethod 2def get_current_date() -> date: 3 """获取当前日期对象(date类型)[1][3] 4 Example: 2024-02-15 5 """ 6 return date.today() 7 8@staticmethod 9def get_current_datetime() -> datetime: 10 """获取当前日期时间对象(datetime类型)[2][3] 11 Example: 2024-02-15 14:30:45.123456 12 """ 13 return datetime.now() 2. 日期格式化与解析 1@staticmethod 2def format_date(dt: date, fmt: str = "%Y-%m-%d") -> str: 3 """日期格式化输出[1][2] 4 Example: 2024-02-15 → "15/02/2024" 5 """ 6 return dt.strftime(fmt) 7 8@staticmethod 9def parse_date_str(date_str: str, fmt: str = "%Y-%m-%d") -> date: 10 """字符串转日期对象[2][3] 11 Example: "20240215" → 2024-02-15 12 """ 13 return datetime.strptime(date_str, fmt).date() 3. 日期计算功能 1@staticmethod 2def calculate_days_diff(start: date, end: date) -> int: 3 """计算两个日期之间的天数差[2] 4 Example: 2024-02-10与2024-02-15 → 5天 5 """ 6 return (end - start).days 7 8@staticmethod 9def add_days(base_date: date, days: int) -> date: 10 """日期加减计算[1][3] 11 Example: 2024-02-15 + 3天 → 2024-02-18 12 """ 13 return base_date + timedelta(days=days) 4. 日历相关功能 1@staticmethod 2def get_month_calendar(year: int, month: int) -> str: 3 """生成月份日历[4][5] 4 Example: 2024年2月 → 返回格式化日历表格 5 """ 6 return calendar.month(year, month) 7 8@staticmethod 9def is_month_end(dt: date) -> bool: 10 """判断是否月末[4][5] 11 Example: 2024-02-15 → False 12 """ 13 return dt.day == calendar.monthrange(dt.year, dt.month)[1] 5. 高级判断功能 1@staticmethod 2def is_future_date(target: date) -> bool: 3 """判断是否为未来日期[1] 4 Example: 2025-01-01 → True 5 """ 6 return target > date.today() 7 8@staticmethod 9def get_workdays(year: int, month: int) -> List[date]: 10 """获取当月工作日列表[4][5] 11 Example: 2024年2月 → 过滤周末的日期列表 12 """ 13 return [date(year, month, day) for day in range(1, calendar.monthrange(year, month)[1]+1) 14 if date(year, month, day).weekday() < 5] 6. 时间维度计算 1@staticmethod 2def get_week_number(dt: date) -> int: 3 """获取ISO周数[2][3] 4 Example: 2024-02-15 → 周数7 5 """ 6 return dt.isocalendar()[1] 7 8@staticmethod 9def get_age(birth_date: date) -> int: 10 """精确年龄计算[2][3] 11 Example: 2000-03-15 → 24岁(当前日期2024-02-15) 12 """ 13 today = date.today() 14 return today.year - birth_date.year - ((today.month, today.day) < (birth_date.month, birth_date.day)) 使用示例 1print("当前日期:", DateUtils.get_current_date()) # [1][3] 2print("月末判断:", DateUtils.is_month_end(date(2024,2,28))) # [4][5] 3print("工作日列表:", DateUtils.get_workdays(2024, 2)) # [4][5] 功能对比表 功能 核心方法 相关模块 应用场景 日期格式化 strftime datetime 日志记录 工作日计算 weekday + calendar calendar 考勤统计 闰年判断 monthrange calendar 日期校验 时区转换 astimezone dateutil 跨国系统 最佳实践建议 处理跨时区场景时建议统一使用UTC时间[1][3] 日期比较前确保时区一致性[1][4] 使用calendar.monthrange()处理月末日期更可靠[4][5] 批量处理日期时建议先进行时区转换[3][4] 该工具类已涵盖日常开发中常见的日期处理需求,通过合理组合使用这些方法,可以显著提升开发效率。 ...

February 15, 2025 · 3 分钟阅读 · 技术文档 -- 次访问