Search code examples
c++opencvyuv

cv::cvtColor gets wrong size image


I am using the following code to convert a RGB format image to a YUVI420 format image, but the result i420Mat is wrong in color and size.

cv::cvtColor(rgbMat, i420Mat, CV_RGB2YUV_I420);

Source rgbMat(RGB Format) : cols: 480, rows: 640;

Corresponding CIImage:

enter image description here

Destination i420Mat(YUVI420 Format): cols: 480, rows: 960;

Corresponding CIImage:

enter image description here

Ideally, they should keep the same size, right?


Solution

  • No, it shouldn't keep the same size, since YUV_I420 is planar. And please note that input image was 3-channel, but output image is 1-channel.

    Image has bigger size, because top part of image (640*480) represents Y' plane (luma), and bottom part represent U and V components correspondingly. So all color planes are represented on one single-channel image, so it has a bigger size. See image from wikipedia:

    enter image description here

    Y' plane is quite the same as grayscale image.

    Why do we see four images in the bottom?

    Top two images corresponds to U plane. Left image is for even rows, right is for odd rows. The same is for V plane in the bottom.

    For additional information please read nice article in wikipedia, and fourcc.org.