如何使用opencv将单个RBG颜色转换为YCbCr颜色空间?

b4wnujal  于 2023-05-01  发布在  其他
关注(0)|答案(1)|浏览(244)

因此,我尝试使用cv2将值为(232,190,172)的单个RGB颜色转换为YCbCr颜色空间。cvtColor()但是我得到一个错误:

error                                     Traceback (most recent call last)
Cell In[10], line 25
     22 typicalSkinColorRGB = np.array([232, 190, 172])
     24 # convert to YCbCr color space
---> 25 typicalSkinColorYCbCr = cv2.cvtColor(typicalSkinColorRGB, cv2.COLOR_RGB2YCR_CB)
     26 print(typicalSkinColorYCbCr)

error: OpenCV(4.7.0) d:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\color.simd_helpers.hpp:92: error: (-2:Unspecified error) in function '__cdecl cv::impl::`anonymous-namespace'::CvtHelper<struct cv::impl::`anonymous namespace'::Set<3,4,-1>,struct cv::impl::A0x820c46fd::Set<3,-1,-1>,struct cv::impl::A0x820c46fd::Set<0,2,5>,2>::CvtHelper(const class cv::_InputArray &,const class cv::_OutputArray &,int)'
> Invalid number of channels in input image:
>     'VScn::contains(scn)'
> where
>     'scn' is 1

这是一个简单的代码,我正在尝试运行:

import numpy as np
import cv2

# from researching on the internet, the RGB color (232, 190, 172) is a typical natural skin
typicalSkinColorRGB = np.array([232, 190, 172])

# convert to YCbCr color space
typicalSkinColorYCbCr = cv2.cvtColor(typicalSkinColorRGB, cv2.COLOR_RGB2YCR_CB)
print(typicalSkinColorYCbCr)
e0bqpujr

e0bqpujr1#

我认为问题与输入数组的形状有关。CV 2需要3个颜色通道,但您的阵列只有一个通道。通常,图像将具有形状[width, height, n_channels]。如果你把它改为np.array([[[232, 190, 172]]]),它应该可以工作-因为现在它将是一个单像素图像,可以这么说。

相关问题