Daniyal Yasin
Daniyal Yasin

Reputation: 172

OpenCV cvtColor image truncated

I am using cvtColor to convert an image from YUYV format to RGB24. The output is fine as far as color is concerned but half of the image is cut. The image is 640x480 YUYV image buffer without any headers. I am using the following code:

 FILE* fd = fopen("imgdump", "r+b");

 char buffer[640*480*2]; // Each pixel takes two bytes in YUYV

 if (fd != NULL)
 {
     fread(buffer, sizeof(char), 640*480*2, fd);
     fclose(fd);
 }

 Mat s_sImageMat = Mat(640, 480, CV_8UC2);  
 Mat s_sConvertedImageMat;

 cout << "before conversion\n";
 s_sImageMat.data = (uchar*) buffer;
 cvtColor(s_sImageMat, s_sConvertedImageMat, CV_YUV2RGB_YUYV);
 cout << "after conversion\n";

 FILE* fw = fopen("converted", "w+b");

 if (fw != NULL)
 {
     fwrite((char*)s_sConvertedImageMat.data, sizeof(char), 640*480*2, fw);
     fclose(fw);
 }

Original Converted

Original file: https://drive.google.com/file/d/0B0YG1rjiNkBUQ0ZuaWN6Y1E2LUU/view?usp=sharing

Additional info: I am using opencv 3.2

Upvotes: 0

Views: 411

Answers (1)

Hamza Shahid
Hamza Shahid

Reputation: 26

The issue seems to be in the following line :

fwrite((char*)s_sConvertedImageMat.data, sizeof(char), 640*480*2, fw);

For RGB24, it should be be :

fwrite((char*)s_sConvertedImageMat.data, sizeof(char), 640*480*3, fw);

Each pixel is 3 bytes in RGB24

Upvotes: 1

Related Questions