通过深入学习Python和Keras,如何在执行照明估计后显示经过颜色校正的图像?
我正在通过深入学习Python和Keras来做一个有监督的图像颜色校正方法。我完成了数据的训练,并预测了测试数据的值。现在,我想展示数据集中的一些经过颜色校正的图像,这样我可以直观地将它们与原始图像进行比较。我陷入了一个循环,不知道如何解决这个问题。有人能帮我写代码或一些提示吗 我已经比较了预测光照和地面真实光照的数量,但我想用预测光照绘制它们的外观图片。我正在使用3倍交叉验证,这使得验证更加困难。 我有1000多张图片,但为了简单起见,我只有12张。我将对其中8个进行训练,并对其中4个进行测试通过深入学习Python和Keras,如何在执行照明估计后显示经过颜色校正的图像?,python,tensorflow,image-processing,keras,deep-learning,Python,Tensorflow,Image Processing,Keras,Deep Learning,我正在通过深入学习Python和Keras来做一个有监督的图像颜色校正方法。我完成了数据的训练,并预测了测试数据的值。现在,我想展示数据集中的一些经过颜色校正的图像,这样我可以直观地将它们与原始图像进行比较。我陷入了一个循环,不知道如何解决这个问题。有人能帮我写代码或一些提示吗 我已经比较了预测光照和地面真实光照的数量,但我想用预测光照绘制它们的外观图片。我正在使用3倍交叉验证,这使得验证更加困难。 我有1000多张图片,但为了简单起见,我只有12张。我将对其中8个进行训练,并对其中4个进行测试
#this is the part where the training and testing is happening, images are loaded
#in xs variable and ground truth illumination is loaded in ys variable
for i in range (3):
print('\nFold ',i)
X_train = xs [folds[i]==0, :]
X_test = xs [folds[i]==1, :]
Y_train = ys [folds[i]==0, :]
Y_test = np.zeros((4,3), dtype=np.uint8)
model = None
model = create_model()
history = model.fit(X_train, Y_train, epochs=10, batch_size=8)
Y_test = model.predict(X_test, batch_size=4)
print("Predicted values for fold %d:" % i, Y_test)
for y in Y_test[:]:
predicted.append(y)
这部分代码功能完美,我不知道的是,在使用预测照明进行颜色校正后,如何绘制或保存这12幅图像中的每一幅。
编辑:我提取了每张照片的预测值。如何将其应用于图像?如果我理解正确,您希望使用模型预测的光源对颜色投射的图像进行白平衡。您的预测包括3个值(比如[alpha、beta、ceta]),它们是将应用于彩色浇铸图像的每个通道(蓝色、绿色、红色)的校正增益 但是,在应用校正增益之前,需要对图像执行gamma线性化(更多信息请参阅) 以下是一些帮助您的示例代码:
import cv2
import numpy as np
def gamma_decode(B_gamma, G_gamma, R_gamma):
B_gamma = B_gamma/255
G_gamma = G_gamma/255
R_gamma = R_gamma/255
gamma = 1/2.2
B_gamma_decode = 255*(B_gamma**(1/gamma))
G_gamma_decode = 255*(G_gamma**(1/gamma))
R_gamma_decode = 255*(R_gamma**(1/gamma))
return (B_gamma_decode, G_gamma_decode, R_gamma_decode)
def gamma_encode(B_channel, G_channel, R_channel):
B_channel = B_channel/255
G_channel = G_channel/255
R_channel = R_channel/255
gamma = 1/2.2
if np.all(B_channel <= 0):
B_gamma_cor = (B_channel**(gamma + 0j))
B_gamma_cor = 255*(abs(B_gamma_cor))
else:
B_gamma_cor = 255*(B_channel**gamma)
if np.all(G_channel <= 0):
G_gamma_cor = (G_channel**(gamma + 0j))
G_gamma_cor = 255*(abs(G_gamma_cor))
else:
G_gamma_cor = 255*(G_channel**gamma)
if np.all(R_channel <= 0):
R_gamma_cor = (R_channel**(gamma + 0j))
R_gamma_cor = 255*(abs(R_gamma_cor))
else:
R_gamma_cor = 255*(R_channel**gamma)
return (B_gamma_cor, G_gamma_cor, R_gamma_cor)
def white_balance(img, pred_illum)
B_channel, G_channel, R_channel = cv2.split(img)
alpha, beta, ceta = pred_illum
#Gamma_decoding
B_channel, G_channel, R_channel = gamma_decode(B_channel, G_channel, R_channel)
#Correction
B_cor = (alpha*B_channel)
G_cor = (beta*G_channel)
R_cor = (ceta*R_channel)
#Gamma encoding
B_cor, G_cor, R_cor = gamma_encode(B_cor, G_cor, R_cor)
#Convert to uint8 to display
B_cor = B_cor.astype(np.uint8)
G_cor = G_cor.astype(np.uint8)
R_cor = R_cor.astype(np.uint8)
img_white_balanced = cv2.merge((B_cor, G_cor, R_cor))
return img_white_balanced
导入cv2
将numpy作为np导入
def gamma_解码(B_gamma、G_gamma、R_gamma):
B_gamma=B_gamma/255
G_伽马=G_伽马/255
R_gamma=R_gamma/255
伽马=1/2.2
B_gamma_decode=255*(B_gamma**(1/gamma))
G_伽马解码=255*(G_伽马**(1/伽马))
R_伽马解码=255*(R_伽马**(1/伽马))
返回(B_gamma_解码、G_gamma_解码、R_gamma_解码)
def伽马编码(B_通道、G_通道、R_通道):
B_通道=B_通道/255
G_通道=G_通道/255
R_通道=R_通道/255
伽马=1/2.2
如果np.all(B_频道)感谢您的输入!是的,您答对了,我想用预测照明对图片进行白平衡。您能澄清一下在应用预测照明后如何查看图像吗?您是否尝试过我上面的代码?您正在寻找的是img_白平衡。您可以运行cv2.imwrite('image\u white\u balanced.png',img\u white\u balanced)为此purpose@Pham,我尝试了你的代码,我也尝试了一些编辑,但它不能正确显示图片。它非常嘈杂,只有鲜艳的颜色,所以我试图找出问题所在。你知道吗?你能给我一些你正在处理的彩色图片吗?