➣ Reading Time: 8 minutes

前言

這支程式可以幫助我們分析一張影像的模糊程度。

sample code

import cv2
import glob
import os
import shutil

folder_path = glob.glob("./input_folder/*.png")
output_folder = "./output_folder"

BIG_BLUR_THESHOLD = 300
MID_BLUR_THESHOLD = 200
SMALL_BLUR_THESHOLD = 100

score_dist = {}


def copyimgfile(text, img_path, fm, output_f):
    print(f"[{text}] img = {img_path}, blur_score = {fm}")
    src = img_path
    dst = output_f + img_path.split("/")[-1]
    print(f"[copyfile] to {dst}")
    shutil.copyfile(src, dst)

def variance_of_laplacian(image):
    '''
    1.calculate laplacian of the images 
    2.calculate variance
    '''
    return cv2.Laplacian(image, cv2.CV_64F).var()


if __name__ == '__main__':
    total_num = len(folder_path)
    for img_path in folder_path:
        # read image
        image = cv2.imread(img_path)

        # convert to grayscale images
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

        # 1.calculate laplacian of the images 
        # 2.calculate variance
        fm = int(variance_of_laplacian(gray))

        score_rank = int(fm/100)

        if(score_dist.get(score_rank)):
            score_dist[score_rank] += 1
        else:
            score_dist[score_rank] = 1

        # rename files with score (if needed)
        # src = img_path
        # basename = os.path.basename(img_path)
        # new_basename = f"s{fm}-{basename}"
        # new_basename = basename.replace(".png", f"-s{fm}.png")

        # dst = src.replace(basename, new_basename)
        # print(src)
        # print(dst)
        # os.rename(src, dst)

        # process result
        if fm > BIG_BLUR_THESHOLD:
            text = "Very Very Very Not Blurry"
            copyimgfile(text, img_path, fm, output_folder+f"/{BIG_BLUR_THESHOLD}/")
        elif fm > MID_BLUR_THESHOLD:
            text = "Very Very Not Blurry"
            copyimgfile(text, img_path, fm, output_folder+f"/{MID_BLUR_THESHOLD}/")
        elif fm > SMALL_BLUR_THESHOLD:
            text = "Very Not Blurry"
            copyimgfile(text, img_path, fm, output_folder+f"/{SMALL_BLUR_THESHOLD}/") 
        else:
            text = "Not Blurry"

    k = sorted(score_dist.keys())
    for key in k:
        print(f"{key}: {score_dist[key]}\t{score_dist[key]/total}%")

Reference

https://blog.csdn.net/WZZ18191171661/article/details/96602043

⭐Python OpenCV 相關文章整理⭐:
⭐基礎知識篇⭐:
1.【OpenCV】1 – 安裝 python OpenCV install 電腦中圖片的基本概念總整理 (附錄:OpenCV 快速測試用程式碼)
2.【OpenCV】2 – OpenCV 圖片的讀取、顯示、存檔 (load, show, save),附贈簡易理解「相對路徑」與「絕對路徑」
3.【OpenCV】OpenCV 利用 python OpenCV 查詢 image 大小, img shape sample code (內附範例程式碼)
⭐基本圖像處理篇⭐:
1.【OpenCV】OpenCV 利用 python OpenCV 縮放圖片 image 大小, cv2 resize (內附程式碼)
2.【OpenCV】3 – python OpenCV 的剪裁、旋轉、縮放 (crop, rotate, resize)
3.【OpenCV】4 – 運用 OpenCV 調整光線 (modify brightness, intensity)
4.【OpenCV】5 – 運用 OpenCV 調整亮度、飽和度(透過轉移至 HLS 顏色空間) modify lightness, saturation
5.【OpenCV】6 – 運用 OpenCV 調整色調(冷色系/暖色系)、色溫(白平衡)modify color temperature, white balance
6.【OpenCV】7 – 運用 OpenCV 為圖片增加一些顆粒感 (增加高斯噪點) add gaussian noise
7.【OpenCV】8 – 運用 OpenCV 改變圖片的對比度 modify contrast (內含:網路上常見錯誤調整對比度方式的分析)
8.【OpenCV】9 – 運用 OpenCV 降低圖片的高光 reduce highlights
9.【OpenCV】11 – OpenCV 建立新空白圖、畫點、畫圓 create new pictures, draw points and draw circle
10.【OpenCV】12 – 運用 OpenCV 畫線、畫矩形、畫橢圓 draw lines, draw rectangle, draw ellipse
11.【OpenCV】13 – 運用 OpenCV 在圖片上寫文字、查色碼、顯示色碼顏色 write text, get and show RGB Color Code
⭐進階圖像處理篇⭐:
1.【OpenCV】14 – 運用 OpenCV 顯示圖片直方圖、分離與合併RGB通道 show histogram, split, merge RGB channel
2.【OpenCV】15 – OpenCV 當然也有像 ps 圖層的功能! 什麼?圖片也能加減法?! Add, Subtract, AddWeighted
3.【OpenCV】16 – 運用 OpenCV 幫助我們找圖片的輪廓(高斯模糊、Canny) cv2.GaussianBlur, cv2.Canny
4.【OpenCV】17 – 運用 OpenCV 的終極圖層處理大全, 想P圖該怎麼P (bitwise_or, and, xor, not, addWeighted)
5.【OpenCV】18 – 運用 OpenCV 做圖片二值化,產生黑白的圖片吧!cv2.threshold 各種選擇參數大全
6.【OpenCV】19 – OpenCV 的圖片自適應二值化,產生更好效果的黑白圖片!cv2.adaptiveThreshold
7.【OpenCV】20 – OpenCV 的各種 Threshold 方法整理,Otsu’s Threshold 大津二值化,自動計算最佳閥值,做出最好的黑白效果圖!
8.【OpenCV】26 – 銳化圖片,將模糊的圖片變得更清晰吧! sharpen images
⭐冷門應用篇⭐:
1.【OpenCV】python OpenCV 分析影像模糊程度 檢測圖片模糊 blur sample code (內含範例程式碼)
2.【OpenCV】21 – OpenCV 也有 photoshop 的魔術棒工具?! 漫水填充法 cv2.floodFill (Magic Wand Tool)
3.【OpenCV】27 – 顏色映射 – OpenCV 內建的自動配色?! auto recolor images, cv2.applyColorMap
⭐影片處理篇⭐:
1.【OpenCV】OpenCV 利用 python OpenCV 將圖片製作成一部影片 (內附程式碼) make video from images
2.【OpenCV】OpenCV 利用 python OpenCV 將一部影片拆成一張張圖片 sample code (內附程式碼) get images from video
3.【OpenCV】用 OpenCV 開啟 rtsp 即時影像串流 sample code (內含範例程式碼) – python OpenCV rtsp
4.【OpenCV】python pyinotify video player 利用 OpenCV + inotify 製作即時影片播放器 sample code (內含範例程式碼)
⭐綜合應用篇⭐:
1.【OpenCV】10 – 日系濾鏡總集篇,運用 OpenCV 製作屬於自己的濾鏡吧 (內含可於網頁上直接完成濾鏡的 colab )
2.【OpenCV】22 – 綜合運用1,用 OpenCV 來P圖囉! 來運用各種之前學習的各種東西吧! merge two images
3.【OpenCV】23 – 綜合運用2,(資料標註) 用 OpenCV 來製作一個標記點小工具吧! 滑鼠與視窗控制 label points
4.【OpenCV】24 – 綜合運用3,(應用app) 用 OpenCV 來製作一個照片文件掃描機吧! photo scanner 透視投影
5.【OpenCV】25 – 綜合運用4,用 OpenCV 來把圖片p到各種奇怪的地方吧! 透視投影 cv2.warpPerspective, merge two images
6.【OpenCV】28 – Final Project v1,於是沒錢買ps的我,開發了自己的photoshop,我的天啊 My photoshop made by OpenCV
7.【OpenCV】29 – Final Project v2,於是沒錢買ps的我,開發了自己的photoshop,我的天啊 My photoshop made by OpenCV
8.【OpenCV】30 – Final Project v3,於是沒錢買ps的我,開發了自己的photoshop,我的天啊 My photoshop made by OpenCV
9.【OpenCV】用 python OpenCV 計算 iou 的方法 (內附範例程式碼) sample code
⭐【喜歡我的文章嗎? 歡迎幫我按讚~ 讓基金會請創作者喝一杯咖啡!
如果喜歡我的文章,請幫我在下方【按五下Like】 (Google, Facebook 免註冊),會由 「LikeCoin」 贊助作者鼓勵繼續創作,讀者們「只需幫忙按讚,完全不用出錢」哦!

likecoin-steps