Ask Your Question
0

C++ - Y'CbCr422 to RGB - convert from raw data file

asked 2017-09-11 09:51:41 -0500

simozz gravatar image

updated 2017-09-11 10:40:49 -0500

Hello,

I am trying to process YCrCb data from file and convert it to RGB.
For this task I am using the following code:

#define DEFAULT_FRAME_Y 288
#define DEFAULT_FRAME_X 544

Mat ycbcrFrame = Mat::zeros(DEFAULT_FRAME_Y, DEFAULT_FRAME_X, CV_8UC3);
Mat rgbFrame = Mat::zeros(DEFAULT_FRAME_Y, DEFAULT_FRAME_X, CV_8UC3);

const char rawFileName[] = "data.raw";
// data.raw is 544 * 288 * 2 = 313344 bytes long
uint32_t rawSize = 2 * DEFAULT_FRAME_Y * DEFAULT_FRAME_X;

FILE *file = fopen(rawFileName, "r");
if (file == NULL)
{
    cout << "Error opening " << rawFileName << endl;
    return 1;
}

fread(ycbcr.data, sizeof(char), rawSize, file);
fclose(file);
cvtColor(ycbcr, rgbFrame, CV_YCrCb2BGR);
imwrite("imageRGB.png", rgbFrame);

The good RGB image taken from the camera is the following:

image description

while the generated image is:

image description

The data.raw file has been generated from the following gst-launch pipeline execution:

gst-launch-1.0 -e v4l2src device=/dev/webcam ! videoconvert ! video/x-raw,width=544,height=288,framerate=10/1 ! multifilesink location=data.raw

while the v4l2-ctl --list-formats --device=/dev/webcam output is the following:

ioctl: VIDIOC_ENUM_FMT
    Index       : 0
    Type        : Video Capture
    Pixel Format: 'YUYV'
    Name        : YUYV 4:2:2

    Index       : 1
    Type        : Video Capture
    Pixel Format: 'MJPG' (compressed)
    Name        : Motion-JPEG

What's wrong with my code and how can I solve this problem ?

EDIT: changing the code as follows: #define DEFAULT_FRAME_Y 288 #define DEFAULT_FRAME_X 544

Mat ycbcrFrame = Mat::zeros(DEFAULT_FRAME_Y, DEFAULT_FRAME_X, CV_8UC2);
Mat rgbFrame = Mat::zeros(DEFAULT_FRAME_Y, DEFAULT_FRAME_X, CV_8UC3);

const char rawFileName[] = "data.raw";
// data.raw is 544 * 288 * 2 = 313344 bytes long
uint32_t rawSize = 2 * DEFAULT_FRAME_Y * DEFAULT_FRAME_X;

FILE *file = fopen(rawFileName, "r");
if (file == NULL)
{
    cout << "Error opening " << rawFileName << endl;
    return 1;
}

fread(ycbcr.data, sizeof(char), rawSize, file);
fclose(file);
cvtColor(ycbcr, rgbFrame, CV_YUV2BGR_UYVY);
imwrite("imageRGB.png", rgbFrame);

produces this image:

image description

which is still wrong.

edit retag flag offensive close merge delete

Comments

Isn't rawSize should be 3 * DEFAULT_FRAME_Y * DEFAULT_FRAME_X; ?

bvbdort gravatar imagebvbdort ( 2017-09-11 10:48:03 -0500 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-09-11 10:49:24 -0500

simozz gravatar image

Solved. After the last edited code, I must use CV_YUV2BGR_YUY2 value in cvtColor.

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-09-11 09:51:41 -0500

Seen: 2,983 times

Last updated: Sep 11 '17