网络编程
位置:首页>> 网络编程>> Python编程>> YUV转为jpg图像的实现

YUV转为jpg图像的实现

作者:叶晚zd  发布时间:2021-07-26 09:18:29 

标签:YUV,jpg图像

调用opencv库,将yuv图像转为jpg图像。

代码如下:


# define _CRT_SECURE_NO_WARNINGS
#include <string>
#include <iostream>
#include <fstream>

#include <cv.h>
#include <highgui.h>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

using namespace cv;
using namespace std;
int main()
{
 int iWidth;
 int iHeight;
 int iFrameNum;
 int iImageSize;

iWidth = 640;
 iHeight = 480;
 char *inputFileName = "640x480_YUV400.yuv";

FILE *fpIn;
 if (fopen_s(&fpIn, inputFileName, "rb"))
 {
   cout << "File Open Failed!\n";
   system("pause");
   exit(1);
 }

iImageSize = iWidth * iHeight;

unsigned char *InData = (unsigned char*)malloc(iImageSize * sizeof(unsigned char));
 unsigned char *uvData = (unsigned char*)malloc(iImageSize / 2 * sizeof(unsigned char));//uv
 memset(uvData, 128, iImageSize / 2);

Mat frameYUV(iHeight * 3 / 2, iWidth, CV_8UC1);
 Mat frameBGR;
 Mat frameRGB;
 Mat frameYUV420;

char outName[128];
 iFrameNum = 0;
 while (1)
 {
   size_t size = fread(InData, sizeof(unsigned char), iImageSize, fpIn);
   if (size == 0)
   {
     cout << "Read Frame Fail!\n";
     system("pause");
     break;
   }
   memcpy(frameYUV.data, InData, iImageSize);
   memcpy(frameYUV.data + iImageSize, uvData, iImageSize / 2);

cvtColor(frameYUV, frameBGR, CV_YUV2BGR_I420);
   cvtColor(frameBGR, frameRGB, CV_BGR2RGB);

imshow("video", frameRGB);
   waitKey(1);

cout << iFrameNum++ << " Frame Processed\n";

sprintf(outName, "outFile/%d.jpg", iFrameNum);
   imwrite(outName, frameRGB);

}

free(InData);
 free(uvData);
 fclose(fpIn);

return 0;
}

来源:https://blog.csdn.net/u013925378/article/details/82153377

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com