一、YUV420 I420介绍
一种颜色编码方法,在YUV色彩空间中,Y表示亮度信号,U、V表示色度信号;
其YUV排列如下,4个Y分量(2x2)对应一个U和V, Y存放完,接着存放U,U存放完,最后是V分量
二、代码实现(opencv2.4.8)
#include <opencv/highgui.h> #include <opencv/cv.h> #include <opencv2/imgproc/imgproc_c.h> using namespace cv; int main(int argv, char **argc) { IplImage *pstImage = NULL; IplImage *pstYUVImage = NULL; FILE *fp = NULL; pstImage = cvLoadImage("D:\test.jpg", CV_LOAD_IMAGE_COLOR); fp = fopen("D://out.yuv", "wb"); pstYUVImage = cvCreateImage(cvSize(pstImage->width, pstImage->height), IPL_DEPTH_8U, 3); cvCvtColor(pstImage, pstYUVImage, CV_BGR2YUV); for(int i = 0; i < pstImage->width * pstImage->height; i++) { //提取Y分量 fwrite(&pstYUVImage->imageData[i*3], 1 , 1, fp); //提取U分量 //fwrite(&pstYUVImage->imageData[i*3+2], 1 , 1, fp); //提取V分量 //fwrite(&pstYUVImage->imageData[i*3+1], 1 , 1, fp); } for(int i = 0; i < pstImage->height; i = i+2) { for(int j = 0; j < pstImage->width; j= j+2) { //提取U分量 fwrite(&pstYUVImage->imageData[3*(i*pstImage->width + j)+2], 1 , 1, fp); } } for(int i = 0; i < pstImage->height; i = i+2) { for(int j = 0; j < pstImage->width; j = j+2) { //提取V分量 fwrite(&pstYUVImage->imageData[3*(i*pstImage->width + j)+1], 1 , 1, fp); } } cvShowImage("Win", pstImage); cvWaitKey(0); cvReleaseImage(&pstImage); cvReleaseImage(&pstYUVImage); fclose(fp); return 0; }
三、查看YUV图像
去本地查看,用7yuv工具查看,格式选择YUV420 I420格式,如下图: