这是我的代码:
import cv2
import numpy as np
import os
def read(sn,tn):
#read the source and target file
source = cv2.imread("./source/" + sn +".bmp")
target = cv2.imread("./target/" + tn +".bmp")
#then transfer it's channel to LAB
source = cv2.cvtColor(source, cv2.COLOR_BGR2LAB)
target = cv2.cvtColor(target, cv2.COLOR_BGR2LAB)
return source,target
def readresult(rn):
result = cv2.imread("./ct-result/" + rn + ".bmp")
result = cv2.cvtColor(result, cv2.COLOR_BGR2LAB)
return result
def mean_std(x):
x_mean, x_std = cv2.meanStdDev(x)
x_mean = np.hstack(np.around(x_mean, 2))
x_std = np.hstack(np.around(x_std, 2))
return x_mean, x_std
def color():
try:
os.mkdir("./ct-result")
except OSError as e:
print("Directory exists")
for n in range(1,7):
source,target=read('s'+str(n),'t'+str(n))
smean,sstd=mean_std(source)
tmean,tstd=mean_std(target)
for i in range(0, source.shape[0]):
for j in range(0, source.shape[1]):
for k in range(0, source.shape[2]):
source[i, j, k] = round( ((source[i, j, k] - smean[k]) * (tstd[k] / sstd[k])) + tmean[k] )
s = cv2.cvtColor(source, cv2.COLOR_LAB2BGR)
print('ct-result/tr' + str(n) + '.bmp'+" is done")
cv2.imwrite('ct-result/tr' + str(n) + '.bmp', s)
def recolor():
try:
os.mkdir("./recov‐source")
except OSError as e:
print("Directory exists")
for n in range(1,7):
source,target=read('s'+str(n),'t'+str(n))
result = readresult('tr'+str(n))
print(target)
smean,sstd=mean_std(result)
tmean,tstd=mean_std(target)
for i in range(0, source.shape[0]):
for j in range(0, source.shape[1]):
for k in range(0, source.shape[2]):
result[i, j, k] = round( ((result[i, j, k] - tmean[k]) * ( sstd[k]/tstd[k] )) + smean[k] )
r = cv2.cvtColor(result, cv2.COLOR_LAB2BGR)
print('recov‐source/rs' + str(n) + '.bmp'+" is done")
cv2.imwrite('recov‐source/tr' + str(n) + '.bmp', r)
#color()
recolor()
我正在尝试在两个图像之间进行颜色转移。当我尝试将源图像转换为结果图像时,它运行得很好。但是,我正在尝试撤消该步骤,以便可以比较结果图像和恢复源图像。
有关功能的说明:
read()
和readresult()
函数的作用相同,只是读取的文件不同。 mean_std()
函数根据图像计算均值和标准差。 color()
和recolor()
基本上做相同的事情,唯一的区别是计算公式。但是,这是我的问题。我可以使用cv2.imwrite()
将图像写入目录,但是不能在recolor()
函数中执行相同的操作。
答案 0 :(得分:0)
我可能是错的。但是,在recolor()
函数中,您将再次迭代源。您不是应该在color()
函数中迭代result
吗?
来自
recolor()
到
for i in range(0, source.shape[0]):
for j in range(0, source.shape[1]):
for k in range(0, source.shape[2]):
result[i, j, k] = round( ((result[i, j, k] - tmean[k]) * ( sstd[k]/tstd[k] )) + smean[k] )