微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

通过python alghoritm将Square Hald clut转换为Classic Hald clut

如何解决通过python alghoritm将Square Hald clut转换为Classic Hald clut

我的问题是:我有一个 Square Hald 方形512x512px,我要将其转换为.CUBE文件

我正在尝试扭转这种简单的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,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),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="")

我们需要重塑值。一些想法或建议?谢谢

解决方法

这里是解析 HALD 图像的每个像素并将其值放入 3DLUT 中的 C++ 代码,然后将其保存到 CUBE 格式的文件中。代码缺少一些东西来保持一切紧凑。希望这能帮助您理解索引算法,以便您可以自己在 python 中实现它。

std::string formatstring(const char *Format,...)
{
    if(Format == NULL || Format[0] == '\0') return "";
    static std::string Res;
    va_list args;
    va_start(args,Format);
    int len = _vscprintf(Format,args);
    Res.resize(len);
    vsprintf((char*)Res.data(),Format,args);
    va_end(args);
    return Res;
}

void convert_hald_to_cube()
{
    CLUT LUT; // you must implement a class to store 3DLUT data of any size

    // Here you getting your HALD image data into a 2D-array hald_colors
    // ...

    // Getting 3DLUT values from a HALD 2D-array
    int hald_side_in_pixels = 512;
    int lutSize = (int)(powf((float)hald_side_in_pixels*hald_side_in_pixels,1.f/3.f)+0.5f); // +0.5f is for rounding a positive value
    int iR,iG,iB;
    int cubeIndex = 0;
    for(int y=0; y<hald_side_in_pixels; ++y) 
    {
        for(int x=0; x<hald_side_in_pixels; ++x)
        {
            iR = cubeIndex % lutSize;
            iG = y % lutSize;
            iB = (x/lutSize)+((y/lutSize)*(hald_side_in_pixels/lutSize));
                        
            // Here you copy the hald_colors[x][y] color value to the 3DLUT voxel value at indexes {iR,iB}
                        
            cubeIndex++;
        }
    }

    // Putting 3DLUT values to a CUBE file
    FILE *f = fopen("OutputCubeFile.cube","w+");
    fputs("TITLE my cube file\n",f);                        
    fputs("DOMAIN_MIN 0 0 0\n",f);
    fputs("DOMAIN_MAX 1 1 1\n",f);
    std::string s = "LUT_3D_SIZE " + std::to_string((_ULonglong)lutSize) + "\n";
    fputs(s.c_str(),f);
    for(int iB=0; iB<lutSize; ++iB) 
    {
        for(int iG=0; iG<lutSize; ++iG) 
        {
            for(int iR=0; iR<lutSize; ++iR) 
            {
                float fr,fg,fb;
                
                // Here you copy the 3DLUT voxel value at indexes {iR,iB} to the values fr,fb
                
                std::string outputvalues = formatstring("%.9f %.9f %.9f\n",fr,fb); 
                fputs(outputvalues.c_str(),f);
            }
        }
    }
    fclose(f);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。