Afeefa123's picture
Update utils.py
5ce5d0d verified
raw
history blame contribute delete
821 Bytes
import cv2
import numpy as np
from skimage.metrics import structural_similarity as ssim
import math
def calculate_psnr(original, enhanced):
mse = np.mean((original - enhanced) ** 2)
if mse == 0:
return 100
max_pixel = 255.0
return 20 * math.log10(max_pixel / math.sqrt(mse))
def calculate_ssim(original, enhanced):
grayA = cv2.cvtColor(original, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(enhanced, cv2.COLOR_BGR2GRAY)
score, _ = ssim(grayA, grayB, full=True)
return score
def detect_blur(image):
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
return cv2.Laplacian(gray, cv2.CV_64F).var()
def enhance_image(image, alpha=1.5, beta=30):
enhanced = cv2.convertScaleAbs(image, alpha=alpha, beta=beta)
denoised = cv2.GaussianBlur(enhanced, (5,5),0)
return denoised