转换为自定义颜色sp

2024-04-24 22:14:42 发布

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

我想问您,如何将python中OpenCV(3.4.5)加载的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)

Tags: test图像import定义颜色np空间array
1条回答
网友
1楼 · 发布于 2024-04-24 22:14:42

你可以这样做:

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)...

相关问题 更多 >