转换为自定义色彩空间

时间:2019-02-16 21:00:07

标签: python opencv

我想问你,如何将OpenCV(3.4.5)在python中加载的BGR图像转换为以下公式定义的色彩空间:

enter image description here

我有以下想法,买错了。

import cv2
imageSource = cv2.imread("test.jpg")
A = np.array([
              [ 0.06,  0.63 ,  0.27],
              [ 0.3 ,  0.04 , -0.35],
              [ 0.34, -0.6  ,  0.17]
             ])
vis0 = cv2.multiply(imageSource, A)

1 个答案:

答案 0 :(得分:1)

您可以执行以下操作:

import cv2
import numpy as np

# This is just a simple helper function that takes a matrix, converts it to
# the BGR colorspace (if necessary), shows it with .imshow() and waits using
# .waitKey() -- feel free to ignore it.
def show(*, img_bgr=None, img_rgb=None):
    if img_bgr is None:
        img_bgr = cv2.cvtColor(img_rgb, cv2.COLOR_RGB2BGR)
    cv2.imshow("title", img_bgr)
    cv2.waitKey()

transform = np.array([
    [ 0.06,  0.63 ,  0.27],
    [ 0.3 ,  0.04 , -0.35],
    [ 0.34, -0.6  ,  0.17]
])

img_bgr = cv2.imread("lenna.png",)
# The image will be in BGR order, we want RGB order
img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)

# This does three things:
# - Transforms the pixels according to the transform matrix
# - Rounds the pixel values to integers
# - Coverts the datatype of the matrix to 'uint8' show .imshow() works
img_trans = np.rint(img_rgb.dot(transform.T)).astype('uint8')

show(img_bgr=img_bgr)
show(img_rgb=img_trans)

哪个来自:

enter image description here

产生:

enter image description here

注意:如果您正在寻找类似的东西:

enter image description here

然后放下移调(...dot(transform.T)...-> ...dot(transform)...