import cv2 import time import numpy as np import matplotlib.pyplot as plt def…
- User
import cv2 import time import numpy as np import matplotlib.pyplot as plt
def focus_measure(image): dx = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3) dy = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3) gradient = np.sqrt(dx2 + dy2) score = np.sum(gradient) return score
N1 = 15 # 要处理的图片张数 A = np.zeros(N1) X = np.zeros(N1) start_time = time.time()
for L in range(1, N1+1): I = cv2.imread(str(L) + '.jpg', 0) # 读取灰度图像 I = np.double(I)
M, N = I.shape score = focus_measure(I) A[L-1] = score print(A[L - 1])end_time = time.time() time_elapsed = end_time - start_time print('Time elapsed:', time_elapsed)
C = np.max(A) D = np.min(A) E = C - D X = (A - D) / E
x1 = np.array([1,7,15,27,34,46,55,64,78,82,94,101,121,134,154]) y1 = np.array([X[0], X[1], X[2], X[3], X[4],X[5], X[6], X[7], X[8], X[9],X[10], X[11], X[12], X[13], X[14]]) p = np.polyfit(x1, y1, 2) Y = np.polyval(p, x1) plt.plot(x1, y1, 'g') plt.title('sobel') plt.show()在上述聚焦评价函数代码中,加入爬山算法搜索策略来确定正焦位置
- Assistant
import cv2 import time import numpy as np import matplotlib.pyplot as plt
def focus_measure(image): dx = cv2.Sobel(image, cv2.CV_64F, 1, 0, ksize=3) dy = cv2.Sobel(image, cv2.CV_64F, 0, 1, ksize=3) gradient = np.sqrt(dx2 + dy2) score = np.sum(gradient) return score
def search_focus(images): max_score = 0 max_index = 0
for i, image in enumerate(images): score = focus_measure(image) if score > max_score: max_score = score max_index = i return max_indexN1 = 15 # 要处理的图片张数 A = np.zeros(N1) X = np.zeros(N1) start_time = time.time()
images = [] for L in range(1, N1+1): image = cv2.imread(str(L) + '.jpg', 0) # 读取灰度图像 image = np.double(image) images.append(image)
focus_index = search_focus(images) I = images[focus_index] for L, image in enumerate(images): score = focus_measure(image) A[L] = score print(A[L])
end_time = time.time() time_elapsed = end_time - start_time print('Time elapsed:', time_elapsed)
C = np.max(A) D = np.min(A) E = C - D X = (A - D) / E
- User
import cv2 import time import numpy as np import matplotlib.pyplot as plt
def focus_measure(I): GX = 0 # 图像水平方向梯度值 GY = 0 # 图像垂直方向梯度值 FI = 0 # 变量,暂时存储图像清晰度值 T = 0 # 设置的阈值 for x in range(1, M - 1): for y in range(1, N - 1): GX = I[x - 1, y + 1] + 2 * I[x, y + 1] + I[x + 1, y + 1] - I[x - 1, y - 1] - 2 * I[x, y - 1] - I[ x + 1, y - 1] GY = I[x + 1, y - 1] + 2 * I[x + 1, y] + I[x + 1, y + 1] - I[x - 1, y - 1] - 2 * I[x - 1, y] - I[ x - 1, y + 1] SXY = np.sqrt(GX ** 2 + GY ** 2) # 某一点的梯度值 if np.any(SXY > T): FI = FI + SXY ** 2 score = np.sum(FI) return score
N1 = 15 # 要处理的图片张数 A = np.zeros(N1) X = np.zeros(N1) start_time = time.time()
for L in range(1, N1+1): I = cv2.imread(str(L) + '.jpg') I = np.double(I) M, N,_ = I.shape
score = focus_measure(I) A[L-1] = score print(A[L - 1])end_time = time.time() time_elapsed = end_time - start_time print('Time elapsed:', time_elapsed)
C = np.max(A) D = np.min(A) E = C - D X = (A - D) / E print(X)
x1 = np.array([1,7,15,27,34,46,55,64,78,82,94,101,121,134,154]) y1 = np.array([X[0], X[1], X[2], X[3], X[4],X[5], X[6], X[7], X[8], X[9],X[10], X[11], X[12], X[13], X[14]]) p = np.polyfit(x1, y1, 2) Y = np.polyval(p, x1) plt.plot(x1, y1, 'g') plt.title('tenengrad') plt.show()针对上述代码,试加入中央取窗法的聚焦窗口选择
- Assistant
import cv2 import time import numpy as np import matplotlib.pyplot as plt
def focus_measure(I): GX = 0 # 图像水平方向梯度值 GY = 0 # 图像垂直方向梯度值 FI = 0 # 变量,暂时存储图像清晰度值 T = 3000 # 设置的阈值 window_size = 10 # 窗口大小 M, N, _ = I.shape
cx = int(M/2) # 中心窗口的x坐标 cy = int(N/2) # 中心窗口的y坐标 for x in range(cx-window_size, cx+window_size): for y in range(cy-window_size, cy+window_size): GX = I[x - 1, y + 1] + 2 * I[x, y + 1] + I[x + 1, y + 1] - I[x - 1, y - 1] - 2 * I[x, y - 1] - I[ x + 1, y - 1] GY = I[x + 1, y - 1] + 2 * I[x + 1, y] + I[x + 1, y + 1] - I[x - 1, y - 1] - 2 * I[x - 1, y] - I[ x - 1, y + 1] SXY = np.sqrt(GX ** 2 + GY ** 2) # 某一点的梯度值 if SXY > T: FI = FI + SXY ** 2 score = np.sum(FI) return scoreN1 = 15 # 要处理的图片张数 A = np.zeros(N1) X = np.zeros(N1) start_time = time.time()
for L in range(1, N1+1): I = cv2.imread(str(L) + '.jpg') I = np.double(I) M, N, _ = I.shape
- User
上述代码中窗口大小的具体含义是什么
- Assistant
窗口大小指的是聚焦评价函数计算中使用的窗口的尺寸。在该代码中,中央取窗法模型中窗口大小设定为10。这意味着在计算图像清晰度时,只考虑以图像中心为中心的一个大小为10x10的正方形窗口内的像素。通过在该窗口内计算梯度值,并根据阈值判断像素清晰度,可以得到一个评价图像清晰度的得分。调整窗口大小可以在得分计算中考虑更多或更少的像素。较大的窗口可以包含更多的像素信息,但也有可能将背景或其他不相关的区域也纳入考虑;而较小的窗口则可能忽略图像的一部分信息。因此,窗口大小的具体选择取决于具体的应用场景和需求。