下面我提供了我正在尝试开发的这个程序的所有代码。输入中的内容是N x 3文件;我将提供下面使用的样本(它只是一个5x3样本)。每个样本代表图像中像素的坐标,使用多维缩放比例缩放到某个XYZ坐标。该程序的目的是从XYZ坐标到LaB颜色......这可以然后被翻译成sRGB。下面的代码(第二部分)显示了从XYZ到LaB的转换,上部(标记为Fast XYZ - RGB)是我发现从XYZ切换到LaB步骤的快捷方式。问题在于快速XYZ - RGB步骤。
我想做的是使sRGBmat =(1 + val)* RGBLin ^(1 / 2.4) - val
我一直遇到的问题是RGBLin有时可能是负数...这意味着我必须使用Cmath或其他东西。我尝试使用Cmath,但它给了我不正确的值 - 在MatLab中,它给了我一个正确的数字,(以及一个真实的+想象的部分),我仍然可以使用它。
文件xyztest.txt包含一个5x3矩阵,其中包含以下值:
.2345 .9817 .7612
.5465 .7897 .3514
.7796 .6765 .5645
.1221 .6376 .8790
.5432 .5853 .4652
输出应该(进行一些计算)得到一个N×3矩阵,其中每一行代表第1行像素1-n处的RGB值(前n个值),然后是第2行下一个n + 1值 -
任何帮助将不胜感激!
import numpy as np
d=open('xyztest.txt', 'r')
import cmath
a=[]
count = 0
b = []
AoverAn = []
XoX = []
YoY = []
ZoZ = []
aova=[]
c = 0
while 1:
line = d.readline()
a.append(line.split())
count = count + 1
if not line:
break
#print a #contains all of the line elements in a list
t=[]
XYZM = []
illuminant = [94.9423, 100.0000, 108.7201]
##or is it [ .9424, 1.000, .8249] which is in matlab-
#print count
for i in range(count-1):
b = a[i:(i+1)]
#print "this is", b
c = b[0]
x = c[0]
y = c[1]
z = c[2]
XoverXn = round(float(x) /illuminant [0], 10)
YoverYn = round(float(y) / illuminant [1], 10)
ZoverZn = round(float(z) / illuminant [2], 10)
XoX.append(XoverXn)
YoY.append(YoverYn)
ZoZ.append(ZoverZn)
x.replace('\'', '')
mmaker = (float("".join(x)), float("".join(y)), float("".join(z)))
XYZM.append(mmaker)
L = []
a = []
b = []
fXoX = []
fYoY = []
fZoZ = []
Lab = []
##print "YOUR XYZ MAT", XYZM
##Get an XYZ matrix so i can use fast XYZ to RGB
快速XYZ> RGB
##A is the thing we want to multiply
A= np.matrix('3.2410, -1.5374, -0.4986 ;-.9692, 1.8760, 0.0416 ; .0556, -.2040, 1.0570')
##we get [R,G,B]' = A * [X,Y,Z]'
##Must be in the range 0-1
RGBLin=[]
##XYZM = float(XYZM)
print "XYZ"
print XYZM
xyzt = np.transpose(np.matrix(XYZM))
RGBLin = np.transpose(A * xyzt)
val = 0.555
temp = (RGBLin <= 0.00304)
#print temp
print "RGB"
##print RGBLin
## Do power multiplcation because numpy doesnt want to work for non square mat
for i in range(len(RGBLin)):
for j in range(1):
rgbline = RGBLin[i].tolist()
for item in rgbline:
for i in range(3):
print item[i]
item[i] = 1.055 + item[i+1]**(1/2.4)
print item[i]
print item
#print rgbline
#te[i][j] = pow(RGBLin[i][j] , (1./2.4))
#print te
- &GT;问题在于这个步骤,我试图将矩阵转换为(1 / 2.4)的幂,但是矩阵的某些值是负的 - 我如何让python给我一个值??!
#te = pow(RGBLin, (1./2.4))
XYZ - &gt; LAB
for i in range(len(XoX)):
#print YoY[i]
xyz = []
test = float(pow(YoY[i],(1./3)))
#print test
if (YoY[i] > 0.008856):
L.append((116 * (YoY[i] **(1./3))) - 16)
#L1 = (116 * (YoY[i] **(1./3))) - 16
else:
L.append(903.3* YoY[i])
#L1 = 903.3* YoY[i]
##
if (XoX[i] > 0.008856):
fXoX.append(pow(XoX[i], (1./3)))
#A1 = pow(XoX[i], (1./3))
else:
fXoX.append((7.787 * XoX[i])+(16/116))
#A1 = (7.787 * XoX[i])+(16/116)
##
if (YoY[i] > 0.008856):
fYoY.append(pow(YoY[i], (1./3)))
#B1 = pow(YoY[i], (1./3))
else:
fYoY.append((7.787 * YoY[i])+(16/116))
#B1 = (7.787 * YoY[i])+(16/116)
##
if (ZoZ[i] > 0.008856):
fZoZ.append(pow(ZoZ[i], (1./3)))
#Z1 = pow(ZoZ[i], (1./3))
else:
fZoZ.append((7.787 * ZoZ[i])+(16/116))
#Z1 = (7.787 * ZoZ[i])+(16/116)
##
a.append(500*(fXoX[i]-fYoY[i]))
b.append(500*(fYoY[i]-fZoZ[i]))
xyz.append((L[i], a[i], b[i]))
##print xyz
######### NOW we must go from Lab to RGB, where XYZ is the LaB co-ordinates######
答案 0 :(得分:1)
告诉numpy你的数字很复杂。
In [1]: import numpy as np
In [2]: r = np.array([-5, 2, 8, -1])
In [3]: r ** (1/2.4)
/usr/local/share/python3/ipython3:1: RuntimeWarning: invalid value encountered in power
#!/usr/local/Cellar/python3/3.2.2/bin/python3.2
Out[3]: array([ nan, 1.33483985, 2.37841423, nan])
In [4]: c = r.astype(complex)
In [5]: c ** (1/2.4)
Out[5]:
array([ 0.50609696+1.88877958j, 1.33483985+0.j ,
2.37841423+0.j , 0.25881905+0.96592583j])
对此on scipy.org进行了一些讨论。