OpenCV cvtColor图像被截断

时间:2017-03-20 02:25:32

标签: c++ opencv image-processing mat image-conversion

我使用cvtColor将图像从YUYV格式转换为RGB24。就颜色而言,输出很好但是图像的一半被切割。图像是640x480 YUYV图像缓冲区,没有任何标题。我使用以下代码:

 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

原始档案:https://drive.google.com/file/d/0B0YG1rjiNkBUQ0ZuaWN6Y1E2LUU/view?usp=sharing

其他信息:我使用的是opencv 3.2

1 个答案:

答案 0 :(得分:1)

问题似乎在以下几行:

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

对于RGB24,它应该是:

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

每个像素在RGB24中是3个字节