項目 widget-area-1 尚未註冊或是沒有一個 view.php 檔案.
項目 widget-area-1 尚未註冊或是沒有一個 view.php 檔案.
項目 search-input 尚未註冊或是沒有一個 view.php 檔案.

【OpenCV】python OpenCV 分析影像模糊程度 檢測圖片模糊 blur sample code (內含範例程式碼)

前言

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

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 – 銳化圖片,將模糊的圖