【发布时间】:2020-09-06 09:00:40
【问题描述】:
我的问题是:我有一个 Square Hald 8 512x512px,我想将其转换为 .CUBE 文件
- 将 Square Hald 转换为 Classic Hald,然后轻松转换为 CUBE(参见第二个算法) 图片:Square Hald 8 - Classic Hald 8
我正在尝试反转这个简单的 python 算法,它可以很好地转换 Classic Hald -> Square 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="")
我们需要重塑价值观。一些想法或建议?谢谢
UPDATE2:感谢 GrossGrade 的作者 Eugene Vdovin,我已经解决了。如果有人建议实施 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()
UPDATE3:我已经按照建议,我用 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()
【问题讨论】:
标签: python android numpy opencv cube