协助将矩阵转化为康斯坦斯的幂

2024-09-19 23:34:56 发布

您现在位置:Python中文网/ 问答频道 /正文

下面我提供了我的所有代码,这个程序我正试图开发。这在输入中是一个nx3文件;我将在下面提供im使用的示例(它只是一个5x3示例)。每个样本代表图像中像素的坐标,该坐标已使用多维缩放到某个XYZ坐标缩放比例这个程序的目的是从XYZ坐标到实验室颜色。。。然后可以转换成sRGB。下面的代码(第二部分)显示了从XYZ到LaB的转换,上面的部分(标记为Fast XYZ-RGB)是我发现的从XYZ到RGB的快捷方式,去掉了LaB步骤。问题出在快速步骤rgxyz。在

我要做的是使sRGBmat=(1+val)*RGBLin^(1/2.4)-val

我一直遇到的问题是RGBLin有时可能是负数。。。也就是说我必须用Cmath或者别的什么。我尝试使用Cmath,但它给了我错误的值- 在MatLab中,它给了我一个合适的数字(好吧,一个实的+虚的部分),我仍然可以使用它。在

文件XYZ测试.txt包含具有以下值的5x3矩阵:

.2345   .9817   .7612
.5465   .7897   .3514
.7796   .6765   .5645
.1221   .6376   .8790
.5432   .5853   .4652

输出应(再进行几次计算)得到一个nx3矩阵,其中每一行代表第1行像素1-N处的RGB值(对于前N个值),然后第2行代表下一个N+1个值-

任何帮助将不胜感激!在

^{2}$

快速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######

Tags: infornprange矩阵rgbitemprint
1条回答
网友
1楼 · 发布于 2024-09-19 23:34:56

告诉纽比你的数字很复杂。在

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的讨论。在

相关问题 更多 >