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(str(L) + '.jpg') I = np.double(I) M, N,_ = I.shape
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 SXY > T: FI = FI + SXY**2 A[L-1] = FIend_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.show()运行时出现Traceback (most recent call last): File "D:\pycharm\opencv\tenengrad.py", line 25, in if SXY > T: ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()怎么解决
- Assistant
在代码中,判断条件
if SXY > T是错误的,因为SXY不是单个值,而是一个数组。要解决这个问题,可以使用np.any(SXY > T)来判断数组中是否有任何一个元素大于T。修改后的代码如下: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(str(L) + '.jpg') I = np.double(I) M, N,_ = I.shape 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 A[L-1] = FI 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.show() - User
修改之后出现TypeError: only length-1 arrays can be converted to Python scalars
The above exception was the direct cause of the following exception:
Traceback (most recent call last): File "D:\pycharm\opencv\tenengrad.py", line 28, in A[L-1] = FI ValueError: setting an array element with a sequence.
- Assistant
在代码中,计算图像清晰度值FI时,FI被定义成了一个值,但在循环中却使用了累加的方式来更新FI。这会导致FI成为一个数组,无法赋值给A中的元素。要解决这个问题,需要将FI的定义放在循环外面,并修改累加方式。
修改后的代码如下:
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(str(L) + '.jpg') I = np.double(I) M, N,_ = I.shape GX = 0 # 图像水平方向梯度值 GY = 0 # 图像垂直方向梯度值 T = 0 # 设置的阈值 FI = 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] = FI 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.show() - 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(str(L) + '.jpg') I = np.double(I) M, N,_ = I.shape
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 A[L-1] = FIend_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.show() 代码运行出现TypeError: only length-1 arrays can be converted to Python scalars
The above exception was the direct cause of the following exception:
- Assistant
在代码中,将图像清晰度值FI存储到数组A中时出现了问题。这是因为在循环内部使用累加的方式计算FI,导致FI是一个数组,无法直接赋值给A中的元素。
要解决这个问题,可以将FI定义为一个列表,然后使用索引将值存储在A中。修改后的代码如下:
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(str(L) + '.jpg') I = np.double(I) M, N,_ = I.shape GX = 0 # 图像水平方向梯度值 GY = 0 # 图像垂直方向梯度值 FI = [] # 变量,暂时存储图像清晰度值 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.append(SXY**2) A[L-1] = np.sum(FI) 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.show()