2016-12-06 285 views
0

我試圖將RGB圖像轉換爲YUV。 我使用openCV加載圖像。FFmpeg中RGB到YUV轉換的錯誤

我打電話的功能如下:

//I know IplImage is outdated 
IplImage* im = cvLoadImage("1.jpg", 1); 
//.... 
bgr2yuv(im->imageData, dst, im->width, im->height); 

功能轉換彩色圖像YUV下圖中給出。 我正在使用ffmpeg來做到這一點。

void bgr2yuv(unsigned char *src, unsigned char *dest, int w, int h) 
{ 
    AVFrame *yuvIm = avcodec_alloc_frame(); 
    AVFrame *rgbIm = avcodec_alloc_frame(); 
    avpicture_fill(rgbIm, src, PIX_FMT_BGR24, w, h); 
    avpicture_fill(yuvIm, dest, PIX_FMT_YUV420P, w, h); 
    av_register_all(); 

    struct SwsContext * imgCtx = sws_getCachedContext(imgCtx, 
             w, h,(::PixelFormat)PIX_FMT_BGR24, 
             w, h,(::PixelFormat)PIX_FMT_YUV420P, 
             SWS_BICUBIC, NULL, NULL, NULL); 

    sws_scale(imgCtx, rgbIm->data, rgbIm->linesize,0, h, yuvIm->data, yuvIm->linesize); 
    av_free(yuvIm); 
    av_free(rgbIm); 
} 

轉換後出現錯誤輸出。 我想這是由於填充發生在IplImage。 (我的輸入圖像寬度不是4的倍數)。

即使在我沒有得到正確的輸出之後,我更新了lineize變量。 當我使用寬度爲4的倍數的圖像時,它的工作正常。

任何人都可以告訴代碼中存在什麼問題。

回答

1

檢查IplImage::alignIplImage::widthStep並使用這些設置AVFrame::linesize。對於RGB幀,例如,你可以設置:

frame->linesize[0] = img->widthStep; 

dst陣列的佈局可以是任何你想要的,這取決於如何你之後使用它。

0

我們需要做如下:

rgbIm->linesize[0] = im->widthStep; 

但是我覺得從sws_scale()輸出數據不填充,使其多的4 所以,當你複製此數據(DEST)再次IplImage的這在顯示,存儲等將 創建問題..

因此,我們需要設置widthStep=width如下:

IplImage* yuvImage = cvCreateImageHeader(cvGetSize(im), 8, 1); 
yuvImage->widthStep = yuvImage->width; 
yuvImage->imageData = dest;