1

I'm writing a code that uses openCV, here is my Question the frame in openCV is in RGB color space, but I need it in YCbCr format, so I used I wrote this small code :

 void  PixelError::conversiontoYcbcr(cv::Mat rgbFrame){
    cv::cvtColor(rgbFrame,frameycbcr, CV_RGB2YCrCb,0);
    cv::Vec3f pixel = frameycbcr.at<cv::Vec3f>(row,col);
 } 

this was the example to get R and G adn B value but since I convert the frame I was hopping to get my values too, but I got an error in row and col and I don't know which channel of the vector pixel represents my Y or Cb or CR values. thanks in advance for your help

Engine
  • 5,360
  • 18
  • 84
  • 162

1 Answers1

2

If your input RGB image is using 8 bits per channel, the result of cvtColor will also be of the type CV_8UC3. So you have to get your pixel with as a cv::Vec3b like so:

#include <opencv2/opencv.hpp>
#include <iostream>
int main()
{
    cv::Mat rgb(4, 3, CV_8UC3, cv::Scalar(255, 127, 63));
    cv::Mat ycbcr;
    cv::cvtColor(rgb, ycbcr, CV_RGB2YCrCb, 0);
    for (int row(0); row < ycbcr.rows; ++row)
    {
        for (int col(0); col < ycbcr.cols; ++col)
        {
            cv::Vec3f pixel = ycbcr.at<cv::Vec3b>(row, col);
            std::cout << "row: " << row << "; col: " << col << "; Y: " << pixel[0] << "; Cr: " << pixel[1] << "; Cb: " << pixel[2] << std::endl;
        }
    }
}
Tobias Hermann
  • 9,936
  • 6
  • 61
  • 134
  • thanks Dobi it work, but it's not exactly what I need, I need to the value of each pixel on the frame – Engine Dec 10 '12 at 12:33
  • OK, I have edited my answer according to your comment. Now every pixel value is printed. – Tobias Hermann Dec 10 '12 at 13:26
  • thanks so much for your help I tryed to use your old version and printed the pixel values for each pixel. but this version is away more better. thanks again – Engine Dec 10 '12 at 13:46