手持相机高速连拍过程,当快门速度不够高时不可避免出现模糊帧,通过肉眼去从多张连拍图像中找到最清晰的帧是比较费事的,可通过代码自动去计算最清晰的图像,省去挑选图像的麻烦事,同时也可以将模糊图像剔除掉,提高后期PS堆栈的效果
import os
from PIL import Image
import numpy as np
def CLIP(value, down, top):
if value <= down:
return down
if value >= top:
return top
return value
def check_roi_region(roi_region, w, h):
safe_roi_region = [0, w-2, 0, h-2]
if (roi_region[0] == 0 and roi_region[1] == 0 and roi_region[2] == 0 and roi_region[3] == 0):
return safe_roi_region
if (roi_region[1] < roi_region[0]) or (roi_region[3] < roi_region[2]):
print("check your roi_region!")
return safe_roi_region
roi_region[0] = CLIP(roi_region[0], 0, w-2)
roi_region[1] = CLIP(roi_region[1], 0, w - 2)
roi_region[2] = CLIP(roi_region[2], 0, h - 2)
roi_region[3] = CLIP(roi_region[3], 0, h - 2)
return roi_region
def cal_resolution(img_gray, roi_region):
resolution = 0
w, h = img_gray.shape
start_x, end_x, start_y, end_y = check_roi_region(roi_region, w, h)
dx = np.sum(np.abs(np.gradient(img_gray[start_x:end_x, start_y:end_y], axis=1))) # 水平方向梯度
dy = np.sum(np.gradient(img_gray[start_x:end_x, start_y:end_y], axis=0)) # 垂直方向梯度
resolution = dx + dy
return resolution
def main():
imgs_dir = r'E:\MyCode\python_code\data\case3'
roi_region = [0, 0, 0, 0] #可设置计算清晰度的矩形区域,如只计算人脸区域,默认全图计算
imgs_names = os.listdir(imgs_dir)
imgs_names = [file for file in imgs_names if file.lower().endswith('.jpg')]
print("图像数量:", len(imgs_names))
count = 1
img_resolution_list = []
for img in imgs_names:
img_path = os.path.join(imgs_dir, img)
img_src = Image.open(img_path)
img_gray = np.array(img_src.convert('L'))
img_resolution = cal_resolution(img_gray, roi_region)
img_resolution_list.append(img_resolution)
print(count, img_path, img_resolution)
count += 1
imgs_names_and_resolution_list = list(zip(imgs_names, img_resolution_list))
sorted_resolution_list = sorted(imgs_names_and_resolution_list, key=lambda x: x[1], reverse=True)
print("=========以下按清晰度从高到低排序=========")
for idx, img in enumerate(sorted_resolution_list):
print(idx + 1, img)
if __name__ == "__main__":
main()
=========以下按清晰度从高到低排序=========
1 ('DSC_3623.JPG', 33861616.5)
2 ('DSC_3605.JPG', 32694941.5)
3 ('DSC_3593.JPG', 32577916.5)
4 ('DSC_3601.JPG', 31280211.0)
5 ('DSC_3592.JPG', 30576425.5)
6 ('DSC_3599.JPG', 30550129.5)
7 ('DSC_3607.JPG', 30258823.5)
8 ('DSC_3574.JPG', 29138570.0)
9 ('DSC_3624.JPG', 29127739.5)
10 ('DSC_3578.JPG', 29041243.5)
11 ('DSC_3619.JPG', 28843627.0)
12 ('DSC_3590.JPG', 28563339.0)
13 ('DSC_3618.JPG', 28483760.5)
14 ('DSC_3606.JPG', 28448455.0)
15 ('DSC_3573.JPG', 27737165.0)
...............................
50 ('DSC_3582.JPG', 19952055.5)
51 ('DSC_3616.JPG', 18890835.5)
52 ('DSC_3609.JPG', 18709554.5)
53 ('DSC_3632.JPG', 16350545.5)
54 ('DSC_3631.JPG', 15735590.0)
55 ('DSC_3584.JPG', 14930016.5)
56 ('DSC_3621.JPG', 14896296.5)
57 ('DSC_3586.JPG', 14681760.5)
58 ('DSC_3610.JPG', 14539433.5)
59 ('DSC_3612.JPG', 14300990.5)
60 ('DSC_3611.JPG', 14108023.5)
61 ('DSC_3585.JPG', 13996736.5)
连拍61张图像,从左到右分别为排序第61(最模糊)、30(中间)、1(最清晰)的图像,对比结果显而易见