• python3 opencv 获取图片模糊度


    #! /usr/bin/env python
    # -*- coding: utf-8 -*-#
    
    # -------------------------------------------------------------------------------
    # Name:         图片模糊度计算
    # Author:       yunhgu
    # Date:         2021/8/20 11:01
    # Description: 
    # -------------------------------------------------------------------------------
    import shutil
    from traceback import format_exc
    import cv2
    from pathlib import Path
    from PIL import Image
    
    import numpy as np
    
    
    def ImageToMatrix(file):
        # 读取图片
        im = Image.open(file)
        # 显示图片
        # im.show()
        width, height = im.size
        im = im.convert("L")
        data = im.getdata()
        data = np.matrix(data, dtype='float') / 255.0
    
        new_data = np.reshape(data, (height, width))
        return new_data
    
    
    def Brenner(img):
        x, y = img.shape
        D = 0
        for i in range(x - 2):
            for j in range(y - 2):
                D += (img[i + 2, j] - img[i, j]) ** 2
        return D
    
    
    def variance_of_laplacian(image):
        return cv2.Laplacian(image, cv2.CV_64F).var()
    
    
    def main(input_path, output_path):
        for file in Path(input_path).rglob("*.jpg"):
            # 拉普拉斯算子
            image = cv2.imdecode(np.fromfile(file, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
            gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
            fm = variance_of_laplacian(gray)
            # Brenner 检测
            frame = ImageToMatrix(file)
            score = Brenner(frame)
            print(fm, score)
    
            # result = Path(output_path).joinpath("no_clear")
            # if fm >= 90:
            #     result = Path(output_path).joinpath("90")
            # elif fm >= 80:
            #     result = Path(output_path).joinpath("80")
            # elif fm >= 70:
            #     result = Path(output_path).joinpath("70")
            # elif fm >= 60:
            #     result = Path(output_path).joinpath("60")
            # elif fm >= 50:
            #     result = Path(output_path).joinpath("50")
            # elif fm >= 40:
            #     result = Path(output_path).joinpath("40")
            # elif fm >= 30:
            #     result = Path(output_path).joinpath("30")
            # elif fm >= 20:
            #     result = Path(output_path).joinpath("20")
            # elif fm >= 10:
            #     result = Path(output_path).joinpath("10")
            #
            # result.mkdir(parents=True, exist_ok=True)
            # shutil.copy(file, result)
    
    
    if __name__ == '__main__':
        print("Start...")
        # input_Folder = input("请输入源文件夹:")
        # output_Folder = input("请输入结果文件夹:")
        input_Folder = r"F:pythonProject图片模糊度计算color"
        output_Folder = r"F:pythonProject图片模糊度计算
    esult"
        try:
            main(input_Folder, output_Folder)
            print("finished")
        except Exception as err:
            print(f"程序运行失败!!!请联系数据处理中心:{err}")
            print(format_exc())
        input("按任意键盘退出!!!")
    
    不论你在什么时候开始,重要的是开始之后就不要停止。 不论你在什么时候结束,重要的是结束之后就不要悔恨。
  • 相关阅读:
    揭示同步块索引(下):总结
    关于.NET技术体系的思维导图
    嵌入式Linux中摄像头使用简要整理
    Tslib和Qt 4.8.4与在开发板上的移植
    图像处理经典图片Lena背后的故事
    Linux 下编译安装OpenCV
    Linux 下编译、安装、配置 QT
    Qt Creator的配置和开发初步测试
    OpenCV的第一个小程序:读取图像并显示
    转:智能手机Flash/DRAM选择、配置与价格大全
  • 原文地址:https://www.cnblogs.com/yunhgu/p/15165856.html
Copyright © 2020-2023  润新知