21 lines
No EOL
599 B
Python
21 lines
No EOL
599 B
Python
import cv2
|
|
import numpy as np
|
|
|
|
def calculate_focus_score(frame):
|
|
"""
|
|
Calculate focus score using Laplacian variance.
|
|
"""
|
|
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
|
laplacian = cv2.Laplacian(gray, cv2.CV_64F)
|
|
score = laplacian.var()
|
|
return score
|
|
|
|
def calculate_focus_score_sobel(frame):
|
|
"""
|
|
Focus score using Sobel gradients.
|
|
"""
|
|
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
|
sobel_x = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)
|
|
sobel_y = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3)
|
|
score = np.mean(sobel_x**2 + sobel_y**2)
|
|
return score |