0

下面我提供了我正在尝试开发的这个程序的所有代码。这需要作为输入的是一个 N x 3 文件;我将在下面提供我使用的示例(它只是一个 5x3 示例)。每个样本代表图像中像素的一个坐标,该坐标已使用多维缩放缩放到某个 XYZ 坐标。该程序的目的是从 XYZ 坐标到 LaB 颜色...然后被翻译成sRGB。下面的代码(第二部分)显示了从 XYZ 到 LaB 的转换,而上半部分(标记为 Fast XYZ - RGB)是我发现从 XYZ 到 RGB 的捷径,去掉了 LaB 步骤。问题在于 Fast 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 x 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

-> 问题出在这一步,我试图将矩阵取为 (1/2.4) 的幂,但矩阵的某些值是负数 - 我如何让 python 给我一个值??!

#te = pow(RGBLin, (1./2.4))

XYZ -> 实验室

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######
4

1 回答 1

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])

在 scipy.org 上有一些讨论。

于 2012-04-21T19:12:08.343 回答