【问题标题】:converting RGB to YCbCr in OpenCV在 OpenCV 中将 RGB 转换为 YCbCr
【发布时间】:2015-04-17 16:32:16
【问题描述】:

我目前正在使用 OpenCV 函数 -cvtColor 将图像从 RGB 转换为 YCrCb 格式。我想使用类似于

的方程式自行执行转换
 //equations for RGB to YUV conversion
 Y' = 0.299 R + 0.587 G + 0.114  B 
 U = -0.147 R - 0.289 G + 0.436  B
 V = 0.615  R - 0.515 G - 0.100  B.      

我无法理解 OpenCV 图像矩阵运算。我想从图像Mat 访问 RGB 像素值,以便我可以自己执行转换。如何从图像中获取 R、G、B 值,然后如何应用转换?我当前的代码如下。

int main (int argc, char *argv[])
{   
    // Load in image 
    cv::Mat src = cv ::imread("C:\\openv2410\\frames\\frame_0.png",1);

    // Create a vector for the channels and split the original image into B G R colour channels.
    // Keep in mind that OpenCV uses BGR and not RGB images
    vector<cv::Mat> spl;
    split(src,spl);

    // Create an zero pixel image for filling purposes - will become clear later
    // Also create container images for B G R channels as colour images
    cv::Mat empty_image =  cv::Mat::zeros(src.rows, src.cols, CV_8UC1);
    cv::Mat empty_channel =  cv::Mat::zeros(src.rows, src.cols, CV_8UC1);

    cv::Mat result_blue(src.rows, src.cols, CV_8UC3); // notice the 3 channels here!
    cv::Mat result_green(src.rows, src.cols, CV_8UC3); // notice the 3 channels here!
    cv::Mat result_red(src.rows, src.cols, CV_8UC3); // notice the 3 channels here!

    // Create blue channel
    cv::Mat in1[] = { spl[0], empty_image, empty_image };
    int from_to1[] = { 0,0, 1,1, 2,2 };
    mixChannels( in1, 3, &result_blue, 1, from_to1, 3 );

    // Create green channel
    cv::Mat in2[] = { empty_channel, spl[1], empty_image };
    int from_to2[] = { 0,0, 1,1, 2,2 };
    mixChannels( in2, 3, &result_green, 1, from_to2, 3 ); 

    // Create red channel
    cv::Mat in3[] = { empty_channel, empty_channel, spl[2]};
    int from_to3[] = { 0,0, 1,1, 2,2 };
    mixChannels( in3, 3, &result_red, 1, from_to3, 3 );    

    imshow("blue channel",result_blue);
    imshow("green channel",result_green);
    imshow("red channel",result_red);  

    cv::waitKey(0);
    return 0;    
}    

【问题讨论】:

标签: opencv


【解决方案1】:

从 BGR 转换为 YCrCb 的示例代码。来源(1)。

//sample input and output
float data[3][1] = { 98,76,88 };
Mat input( 1, 1, CV_32FC3, data) ;
Mat output( 1, 1, CV_32FC3 );

//iterate over all pixels
for(int i = 0; i < input.rows; i++) {
    for(int j = 0; j < input.cols; j++) {
        //get bgr pixel 
        Vec3f bgrPixel = input.at<Vec3f>(i, j);

        float B = bgrPixel[0];
        float G = bgrPixel[1];
        float R = bgrPixel[2];          

        //actual conversion from BGR to YCrCb
        float delta = 0.5f; 
        float Y  = 0.299f * R + 0.587f * G + 0.114f * B;
        float Cb = (B - Y) * 0.564f + delta;
        float Cr = (R - Y) * 0.713f + delta;

        //store into result image
        Vec3f yCrCbPixel( Y, Cr, Cb );
        output.at<Vec3f>(i, j) = yCrCbPixel;
    }
}

【讨论】:

  • 感谢您的帮助。但是当我尝试替换虚拟数据时 float data[3][1] = { 98,76,88 };使用 Mat 输入 = imread(argv[1], 0);它没有显示任何结果我添加了 std::cout
  • @user3805574 imread(argv[1], 0) 为您提供灰度图像,而不是 RGB 图像。我认为这个问题是关于将 RGB 转换为 YCrCb。
  • 是的问题是关于将 RGB 转换为灰度但是,当我尝试替换 Mat 输入(1、1、CV_32FC3、数据)时; with mat img = imread;
  • 抱歉以上评论。将我的评论改写为试图替换 Mat input(1, 1, CV_32FC3, data) ;与垫 img = imread;我没有得到任何价值。如果我想知道的话,你能告诉我如何打印 Y 值吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-25
  • 1970-01-01
  • 1970-01-01
  • 2018-06-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多