gpt4 book ai didi

python - 转换为自定义色彩空间

转载 作者:太空宇宙 更新时间:2023-11-03 22:40:06 24 4
gpt4 key购买 nike

请问如何将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)

最佳答案

你可以这样做:

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

关于python - 转换为自定义色彩空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54727608/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com