我正在尝试为每个图像在天顶角的特定范围内计算平均温度。
我正在使用for循环来执行上述任务。在循环内部,我正在计算每个像素距中心的距离,然后根据该距离应用条件。
图像有很多不想要的反射,我正在使用df_con数据框删除它们。
该循环需要1分30秒来处理一张图像上的所有操作(仅花24分钟就花费了38分钟)。有没有一种方法可以提高代码速度。
### Reading all the images inside the Images folder
X_data = []
files = glob.glob ("Images/*.jpg"). # Total 17,000 images of(480*640*3)
files.sort(key=os.path.getmtime, reverse=True)
X_data = [cv.imread(img) for img in files]
image_data = np.array(X_data)
T_hot = np.array([])
for r in tqdm(range(image_data[:,0,0,0].size)):
##Converting RGB image to grey scale image
grey = cv.cvtColor(image_data[r],cv.COLOR_BGR2GRAY)
Z = grey.reshape(-1,1)
Tem = np.array([])
Tmax = 25
Tmin = -10
Zmax = 255
Zmin = 0
c = -10
m = (Tmax - Tmin) / (Zmax - Zmin)
zenith = np.array([])
theta = np.around(np.arange(0,90,90/200),6)
for i in range(0,480):
for j in range(0,640):
# Calculating distance of each pixel from the center.
r = np.around(np.sqrt((332 - j)**2 + (235 - i)**2))
# Assigning zxenith angle to each pxl.
# Calculating Temperature of indexed pxl.
if r < 200:
k = theta[theta == np.around((r*90/200),6)]
zenith = np.append(zenith,k)
T = (m*grey[i,j]) + c
Tem = np.append(Tem,T)
else:
k = 120
zenith = np.append(zenith,k)
T = 255
Tem = np.append(Tem,T)
# creating panda dataframe
df = pd.DataFrame({'Pxl':Z[:,0],'Tem':Tem[:],'zenith':zenith[:]})
# Fetching the Image mask data points
df_con = pd.read_excel('contour.xlsx')
dataset_final = pd.merge(df,df_con, how='outer', on=None, \
left_index=True, right_index=True, sort=True)
dataset_final = dataset_final[dataset_final['pxl_new'] < 255]
df_0 = pd.DataFrame(0, index=range(Z.shape[0]), columns={'Null'})
df_image = pd.merge(dataset_final,df_0, how='outer', on=None, \
left_index=True, right_index=True,\
sort=True)
df_image = df_image[['Pxl','Tem','zenith']].fillna(255)
df_target = dataset_final[(dataset_final['zenith'] >= 65) & \
(dataset_final['zenith'] <= 85)]
mean = np.mean(df_target[['Tem']].values)
T_hot = np.append(T_hot, mean)
因此,经过三天的奋斗,我设法部分解决了问题。我在代码中注意到三件事:
X_data = []
files = glob.glob ("Images/*.jpg")
files.sort(key=os.path.getmtime, reverse=True)
for img in tqdm(files):
# Reading images in greyscale
X = cv.imread(img,0);
X_data.append(X)
我正在循环内使用熊猫数据帧执行索引操作。尽管熊猫数据框使我们的生活变得轻松,但它需要花费大量的计算时间。因此,我决定使用numpy array
而不是panda DataFrame
。
我为下面的代码块实现了多处理。
for i in range(0,480):
for j in range(0,640):
r = np.around(np.sqrt((332 - j)**2 + (235 - i)**2))
if r < 200:
k = theta[theta == np.around((r*90/200),6)]
zenith = np.append(zenith,k)
T = (m*grey[i,j]) + c
Tem = np.append(Tem,T)
else:
k = 120
zenith = np.append(zenith,k)
T = 255
Tem = np.append(Tem,T)
现在新的代码块是这样的:
def temperature(count):
zenith = np.array([])
Tem = np.array([])
theta = np.around(np.arange(0, 90, 90 / 200), 6)
for j in range(0, 640):
r = np.around(np.sqrt((332 - j) ** 2 + (235 - count) ** 2))
if r < 200:
k = theta[theta == np.around((r * 90 / 200), 6)]
zenith = np.append(zenith,k)
T = (m * grey[count, j]) + c
Tem = np.append(Tem,T)
else:
k = 120
zenith = np.append(zenith,k)
T = 20
Tem = np.append(Tem,T)
result = np.vstack((zenith, Tem)).T
return np.array(result)
if __name__ == '__main__':
pool = Pool(cpu_count())
result = pool.map(temperature, range(0,480))
res = np.array(result)
Tem = res[:,:,1].reshape(-1,1)
zenith = res[:,:,0].reshape(-1,1)
通过实施上述更改,我设法减少了1min30sec to 2sec
中单个图像的处理时间。我相信有更好的方法可以进一步优化此效果。请随时提供您的解决方案。对于像我这样的新生来说,这将是一个很大的帮助。