我正在使用 Qt、OpenCV 和 libJpeg 压缩 YUV422 图像,但输出不正确。
如果我将 yuv 转换为 rgb 然后压缩,我会得到正确的输出,但我知道 Jpeg 在内部使用 YUV,这就是我要删除冗余的原因。
这是我的代码:
bool ipl2jpeg(IplImage *frame, unsigned char **outbuffer, long unsigned int *outlen) {
unsigned char *outdata = (uchar *) frame->imageData;
struct jpeg_compress_struct cinfo ;
struct jpeg_error_mgr jerr;
JSAMPROW row_ptr[1];
int row_stride;
*outbuffer = NULL;
*outlen = 0;
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
jpeg_mem_dest(&cinfo, outbuffer, outlen);
cinfo.image_width = frame->width;
cinfo.image_height = frame->height;
cinfo.input_components = frame->nChannels;
cinfo.in_color_space = JCS_YCbCr;
jpeg_set_defaults(&cinfo);
jpeg_set_quality (&cinfo,100 , true);
jpeg_start_compress(&cinfo, TRUE);
row_stride = frame->width *2;// frame->nChannels;
while (cinfo.next_scanline < cinfo.image_height) {
/* jpeg_write_scanlines expects an array of pointers to scanlines.
* Here the array is only one element long, but you could pass
* more than one scanline at a time if that's more convenient.
*/
row_ptr[0] = &outdata[cinfo.next_scanline * row_stride];
(void) jpeg_write_scanlines(&cinfo, row_ptr, 1);
}
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
return true;
}
怎么了 ??