我的问题是:我有一个方形Hald851x512px,我想把它转换成.CUBE文件
我正在尝试反转这个简单的python算法,它可以很好地转换经典的Hald->;方形Hald:
import numpy, cv2 hald = cv2.imread("classic_hald_8.png") size = int(hald.shape[0] ** (1.0/3.0) + .5) clut = numpy.concatenate([ numpy.concatenate(hald.reshape((size,size,size**2,size**2, 3))[row], axis=1) for row in range(size) ]) cv2.imwrite("square_hald_8.png", clut)
import imageio as iio, numpy as np imagen=iio.imread("Classic_Hald_8.png") r,g,b=(imagen[:,:,0]).reshape(-1), (imagen[:,:,1]).reshape(-1), (imagen[:,:,2]).reshape(-1) np.savetxt("Classic_Hald_8.cube",X=np.column_stack((r/255,g/255,b/255)),fmt='%1.6f', header="LUT_3D_SIZE 64", comments="")
我们需要重塑价值观。一些想法或建议?谢谢
更新2:多亏格罗斯格勒的作者尤金·维多文,我才解决了这个问题。如果有人建议实施3D结构(可能使用numpy?),我们会接受。我对python很在行
from PIL import Image
im = Image.open('test.png','r')
values = im.load()
hald_side_in_pixels = im.size[0]
hald_in_pixels = im.size[0]*im.size[0]
lutSize = int(hald_in_pixels ** (1.0/3.0) + .5)
fr = [0.0]*hald_in_pixels
fg = [0.0]*hald_in_pixels
fb = [0.0]*hald_in_pixels
cubeIndex = 0
for y in range(hald_side_in_pixels):
for x in range(hald_side_in_pixels):
iR = cubeIndex % lutSize
iG = y % lutSize
iB = int(x/lutSize)+(int(y/lutSize)*int(hald_side_in_pixels/lutSize))
idx = iR * lutSize * lutSize + iG * lutSize + iB
fr[idx],fg[idx],fb[idx] = values[x,y]
cubeIndex+=1
with open("test.cube", "w") as output:
output.write("DOMAIN_MIN 0 0 0\nDOMAIN_MAX 1 1 1\nLUT_3D_SIZE " + str(lutSize) + '\n')
for iB in range(lutSize):
for iG in range(lutSize):
for iR in range(lutSize):
idx = iR * lutSize * lutSize + iG * lutSize + iB
output.write((str("%.9f" % (fr[idx]/255)) + ' ' + str("%.9f" % (fg[idx]/255)) + ' ' + str("%.9f" % (fb[idx]/255)))+ '\n')
output.close()
更新3:我按照建议,用numpy创建了一个3D阵列,现在更干净了,但比3x 1D阵列慢150毫秒,我发布了代码
from PIL import Image
import numpy as np
im = Image.open('test.png','r')
values = im.load()
hald_side_in_pixels = im.size[0]
lutSize = int((hald_side_in_pixels*hald_side_in_pixels) ** (1.0/3.0) + .5)
LUT = np.empty((lutSize,lutSize,lutSize), dtype=bytearray)
cubeIndex = 0
for y in range(hald_side_in_pixels):
for x in range(hald_side_in_pixels):
iR = cubeIndex % lutSize
iG = y % lutSize
iB = int(x/lutSize)+(int(y/lutSize)*int(hald_side_in_pixels/lutSize))
LUT[iR,iG,iB]=values[x,y]
cubeIndex+=1
with open("test1.cube", "w") as output:
output.write("DOMAIN_MIN 0 0 0\nDOMAIN_MAX 1 1 1\nLUT_3D_SIZE " + str(lutSize) + '\n')
for iB in range(lutSize):
for iG in range(lutSize):
for iR in range(lutSize):
output.write((str("%.9f" % (LUT[iR,iG,iB][0]/255)) + ' ' + str("%.9f" % (LUT[iR,iG,iB][1]/255)) + ' ' + str("%.9f" % (LUT[iR,iG,iB][2]/255)))+ '\n')
output.close()
这是一个C++代码,它解析HALD图像的每个像素,并将其值放入3DLUT中,后者保存为立方体格式的文件。代码缺少一些东西来保持紧凑。希望这能帮助您理解索引算法,以便您可以自己在python中实现它
相关问题 更多 >
编程相关推荐