import cv2 import time import numpy as np import matplotlib.pyplot as plt N1 = 15 #…
- User
import cv2 import time import numpy as np import matplotlib.pyplot as plt
N1 = 15 # 要处理的图片张数 A = np.zeros(N1) X = np.zeros(N1) start_time = time.time()
for L in range(1, N1+1): I = cv2.imread(f'photograph{str(L)} .jpg',0) I = np.double(I) M, N = I.shape
codeGX = 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 A[L-1] = np.sum(FI)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''' X=A 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()运行时出现错误[ WARN:0@0.412] global loadsave.cpp:248 cv::findDecoder imread_('photograph\1 .jpg'): can't open/read file: check file path/integrity Traceback (most recent call last): File "D:\pycharm\opencv\focusing evaluation function\tenengrad.py", line 14, in M, N = I.shape ValueError: not enough values to unpack (expected 2, got 0)
- Assistant
The error message indicates that the program is unable to open/read the image file specified in the
cv2.imreadfunction. This could be due to an incorrect file path or missing image file. Please make sure that the image files exist in the specified directory and that the file names are correct.You should also check the format of the image file and make sure it is supported by OpenCV.