在opencv中如何将sRGB转换成线性sRGB来计算颜色校正矩阵?(CCM)

2024-06-26 02:53:03 发布

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

我已经参考了下面的stackoverflow thread来计算颜色校正矩阵。在

在上面提到的线程中,我想从sRGB颜色空间转换成线性sRGB空间,我尝试使用pwkit colorspace mapper代码进行转换。在

但是,我不确定结果的线性sRGB值,因为函数要求sRGB在[0-1]范围内,sRGB值除以255.0是正确的方法吗?如何验证函数返回的线性sRGB值是否正确?在

import os
import numpy as np
import cv2 
import matplotlib.pyplot as plt

def srgb_to_linsrgb (srgb):
    """Convert sRGB values to physically linear ones. The transformation is
       uniform in RGB, so *srgb* can be of any shape.

       *srgb* values should range between 0 and 1, inclusively.

    """
    gamma = ((srgb + 0.055) / 1.055)**2.4
    scale = srgb / 12.92
    return np.where (srgb > 0.04045, gamma, scale)


if __name__ == "__main__":

    colorChecker = cv2.imread('C:/Users/Ai/Documents/Urine Sample Analysis/Assets/colorchecker_1.jpg')
    cc = cv2.cvtColor(colorChecker,cv2.COLOR_BGR2RGB)
    plt.imshow(cc)

    #Convert srgb to linear rgb
    cc = cc / 255.0
    cc1 = srgb_to_linsrgb(cc)
    print("Conversion from sRGB to linear RGB:\n")
    print(cc1[1,1,:])

换算结果为:[0.30946892 0.234555058 0.19806932]

输入的sRGB应该在0-1之间,如何将sRGB通道的值从[0-255]缩放到[0-1],简单的除以255.0会得到正确的线性sRGB值吗?如何验证转换是否产生了正确的线性sRGB值?在


Tags: to函数import颜色asnp空间plt
1条回答
网友
1楼 · 发布于 2024-06-26 02:53:03

从术语的角度来看,假设您的图像是有效的sRGB编码的,即遵循iec61966-2-1:1999,您将通过应用sRGB电光传递函数(EOTF),一种颜色分量传递函数(CCTF)来解码sRGB编码的非线性光值,因此:

I want to convert from the sRGB color space to linear RGB space

是不正确的,因为您仍然有效地使用sRGB编码的颜色,唯一的区别是在使用解码CCTF之前,它们是非线性编码的。一个加性RGB颜色空间由三个组成部分组成:原色、白点和CCTFs。通过使用CCTF,你根本没有改变初选或白点,也就是说,色域是相同的,因此你应该说的更多的是沿着这些思路:

I want to convert from the sRGB color space to linear sRGB color space

现在要验证您的方法是否正确,可以与引用实现(如Colour)进行比较,该实现的代码在您引用的SO线程中使用:使用sRGB inverse EOTF进行非线性编码时为118:18%gray:

>>> int(round(colour.models.eotf_inverse_sRGB(0.18) * 255))
118

从那里你可以检查你的方法是否真的转换回18%,它确实(忽略舍入)!在

^{pr2}$

相关问题 更多 >