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)
codeM, 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()参照上述代码给出brenner算子的代码
- 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 brenner_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 = 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)
codeM, N = I.shape score = brenner_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('brenner') plt.show()
- User
你给的和sobel算子的代码,有什么区别?修改后给出brenner算子的代码
- Assistant
我之前给出的代码是用Sobel算子计算图像的梯度来进行焦点测量的,而你需要的是用Brenner算子来进行焦点测量。Brenner算子是另一种常用的焦点评估方法。
下面是使用Brenner算子进行焦点测量的代码示例:
pythonimport cv2 import time import numpy as np import matplotlib.pyplot as plt def brenner_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 = dx**2 + dy**2 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 = brenner_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('brenner') plt.show()