#include <iostream>
#include <highgui.h>
#include <cv.h>
#include <fstream>
#include <sstream>
using namespace cv;
IplImage* YUV420_To_IplImage(unsigned char* pYUV420, int width, int height);
void YUV420_To_BGR24(unsigned char *puc_y, unsigned char *puc_u, unsigned char *puc_v, unsigned char *puc_rgb, int width_y, int height_y);
void BGR_To_YUV(unsigned char *puc_y, unsigned char *puc_u, unsigned char *puc_v, unsigned char *puc_rgb, int width_y, int height_y);
int main(int argc,char *argv[])
{
int iFrameCount = atoi(argv[1]) ;//字符串转换成长整型数的一种函数,检测到非数字(包括结束符 \0) 字符时停止转换,返回整型数
int iPicWidth = atoi(argv[2]) ;
int iPicHeight = atoi(argv[3]) ;
FILE *fp=fopen(argv[4],"rb");
FILE *fp1 = fopen(" down.yuv","wb");
FILE *fp2 = fopen(" up.yuv","wb");
unsigned char *buff = new unsigned char[iPicWidth*iPicHeight*3/2];
for (int i=0; i<iFrameCount; i++)
{
fseek(fp,i*(iPicWidth*iPicHeight*3/2),SEEK_SET);
fread(buff,1,iPicWidth*iPicHeight*3/2,fp);
IplImage *rgbimg = YUV420_To_IplImage(buff, iPicWidth, iPicHeight);//将buf转为rgb,存入rgbimg
IplImage * dst1 = cvCreateImage(cvSize( rgbimg->width/2, rgbimg->height/2 ),rgbimg->depth, rgbimg->nChannels);
cvPyrDown( rgbimg, dst1,CV_GAUSSIAN_5x5); //下采样
//将下采样后的图像输出为yuv序列
unsigned char *tmpY = new unsigned char[iPicWidth/2*iPicHeight/2];
unsigned char *tmpCb = new unsigned char[iPicWidth*iPicHeight/4/4];
unsigned char *tmpCr = new unsigned char[iPicWidth*iPicHeight/4/4];
BGR_To_YUV(tmpY,tmpCb,tmpCr,(unsigned char *)(dst1->imageData),iPicWidth/2,iPicHeight/2);//将rgb转为yuv输出
fwrite(tmpY,1,iPicWidth*iPicHeight/4,fp1);
fwrite(tmpCb,1,iPicWidth*iPicHeight/16,fp1);
fwrite(tmpCr,1,iPicWidth*iPicHeight/16,fp1);
delete[] tmpY;
delete[] tmpCb;
delete[] tmpCr;
IplImage * dst2 = cvCreateImage(cvSize( dst1->width*2, dst1->height*2 ),dst1->depth, dst1->nChannels);
cvResize(dst1,dst2,CV_INTER_LINEAR); //将dst1进行插值
unsigned char *tmpY1 = new unsigned char[iPicWidth*iPicHeight];
unsigned char *tmpCb1 = new unsigned char[iPicWidth*iPicHeight/4];
unsigned char *tmpCr1 = new unsigned char[iPicWidth*iPicHeight/4];
BGR_To_YUV(tmpY1,tmpCb1,tmpCr1,(unsigned char *)(dst2->imageData),iPicWidth,iPicHeight);//将rgb转为yuv输出
fwrite(tmpY1,1,iPicWidth*iPicHeight,fp2);
fwrite(tmpCb1,1,iPicWidth*iPicHeight/4,fp2);
fwrite(tmpCr1,1,iPicWidth*iPicHeight/4,fp2);
delete[] tmpY1;
delete[] tmpCb1;
delete[] tmpCr1;
// cvNamedWindow( "yuv" ,1);
// cvShowImage( "yuv", rgbimg);
// cvWaitKey(10);
cvReleaseImage(&rgbimg);
}
// cvDestroyAllWindows();
fclose(fp);
fclose(fp1);
fclose(fp2);
}
IplImage* YUV420_To_IplImage(unsigned char* pYUV420, int width, int height)
{
//初始化变量
int baseSize = width*height;
int imgSize = baseSize*3;
unsigned char* pRGB24 = new unsigned char[imgSize];
memset(pRGB24, 0, imgSize);
/* 变量声明 */
unsigned char* yData = pYUV420; //y分量地址
unsigned char* uData = pYUV420 + baseSize; //u分量地址
unsigned char* vData = uData + (baseSize>>2); //v分量地址
//将YUV转为RGB
YUV420_To_BGR24(yData, uData, vData, pRGB24, width, height);
IplImage *image = cvCreateImage(cvSize(width, height), 8,3);
memcpy(image->imageData, pRGB24, imgSize);
delete [] pRGB24;
return image;
}
void YUV420_To_BGR24(unsigned char *puc_y, unsigned char *puc_u, unsigned char *puc_v, unsigned char *puc_rgb, int width_y, int height_y)
{
//初始化变量
int baseSize = width_y * height_y;
int rgbSize = baseSize * 3;
unsigned char* rgbData = new unsigned char[rgbSize];
memset(rgbData, 0, rgbSize);
/* 变量声明 */
int temp = 0;
unsigned char* rData = rgbData; //r分量地址
unsigned char* gData = rgbData + baseSize; //g分量地址
unsigned char* bData = gData + baseSize; //b分量地址
int uvIndex =0, yIndex =0;
//YUV->RGB 的转换矩阵
//double Yuv2Rgb[3][3] = {1, 0, 1.4022,
// 1, -0.3456, -0.7145,
// 1, 1.771, 0};
for(int y=0; y < height_y; y++)
{
for(int x=0; x < width_y; x++)
{
uvIndex = (y>>1) * (width_y>>1) + (x>>1);
yIndex = y * width_y + x;
/* r分量 */
temp = (int)(puc_y[yIndex] + (puc_v[uvIndex] - 128) * 1.4022);
rData[yIndex] = temp<0 ? 0 : (temp > 255 ? 255 : temp);
/* g分量 */
temp = (int)(puc_y[yIndex] + (puc_u[uvIndex] - 128) * (-0.3456) +
(puc_v[uvIndex] - 128) * (-0.7145));
gData[yIndex] = temp < 0 ? 0 : (temp > 255 ? 255 : temp);
/* b分量 */
temp = (int)(puc_y[yIndex] + (puc_u[uvIndex] - 128) * 1.771);
bData[yIndex] = temp < 0 ? 0 : (temp > 255 ? 255 : temp);
}
}
//将R,G,B三个分量赋给img_data
int widthStep = width_y*3;
for (int y = 0; y < height_y; y++)
{
for (int x = 0; x < width_y; x++)//yuv按存完全部Y分量,再存全部U,最后存全部V;rgb按每个像素BGR存储
{
puc_rgb[y * widthStep + x * 3 + 2] = rData[y * width_y + x]; //R
puc_rgb[y * widthStep + x * 3 + 1] = gData[y * width_y + x]; //G
puc_rgb[y * widthStep + x * 3 + 0] = bData[y * width_y + x]; //B
}
}
delete [] rgbData;
}
void BGR_To_YUV(unsigned char *puc_y, unsigned char *puc_u, unsigned char *puc_v, unsigned char *puc_rgb, int width_y, int height_y)
{
int baseSize = width_y * height_y;
int rgbSize = baseSize * 3;
/* 变量声明 */
int tempY = 0,tempU = 0,tempV = 0;
int index =0, yIndex =0,uvIndex=0;
//Y'= 0.299*R' + 0.587*G' + 0.114*B' , U'= -0.147*R' - 0.289*G' + 0.436*B' = 0.492*(B'- Y') , V'= 0.615*R' - 0.515*G' - 0.100*B' = 0.877*(R'- Y')
for(int y=0; y < height_y; y++)
{
for(int x=0; x < width_y; x++)
{
yIndex = y * width_y + x;
index = 3*yIndex;
//Y
tempY = (int)(puc_rgb[index+2]*0.299 + puc_rgb[index+1]*0.587+ puc_rgb[index]*0.11);
puc_y[yIndex] = tempY<0 ? 0 : (tempY > 255 ? 255 : tempY);
if ((x%2==0)&&(y%2==0))
{
uvIndex = (y>>1) * (width_y>>1) + (x>>1);
//U
tempU = (int)(puc_rgb[index+2]*(-0.147) + puc_rgb[index+1]*(-0.289)+ puc_rgb[index]*0.436+128);
puc_u[uvIndex] = tempU<0 ? 0 : (tempU > 255 ? 255 : tempU);
//V
tempV = (int)(puc_rgb[index+2]*(0.615) + puc_rgb[index+1]*(-0.5159)+ puc_rgb[index]*(-0.100)+128);
puc_v[uvIndex] = tempV<0 ? 0 : (tempV > 255 ? 255 : tempV);
}
}
}
}
yuv文件下采用和插值
需积分: 16 149 浏览量
2017-04-06
12:18:19
上传
评论
收藏 15.15MB ZIP 举报
Eyrane
- 粉丝: 107
- 资源: 15
最新资源
- 前端开发-什么是前端开发-关于前端开发的一些相关介绍
- Sora AI-关于文生视频的使用场景说明
- suno AI文生视频的相关教程和介绍使用
- 什么是后端开发-关于后端开发的一些小介绍分享
- Jurassic Pack Vol. II Dinosaurs 侏罗纪包卷恐龙二号Unity游戏模型资源unitypackage
- Jurassic Pack Vol. III Dinosaurs 侏罗纪包卷恐龙三号Unity游戏模型资源unitypackag
- Ultimate Seating Controller 终极座椅控制器Unity游戏开发插件资源unitypackage
- 什么是人工智能-关于人工智能的相关介绍说明
- Figma Converter for Unity适用Unity的Figma转换器Unity游戏开发插件unitypackage
- Creepy Animatronic Anims 令人毛骨悚然的电子动画Unity游戏动画插件资源unitypackage
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈