YUV转为jpg图像的实现

yipeiwu_com6年前Python基础

调用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;
}

以上这篇YUV转为jpg图像的实现就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python探索之自定义实现线程池

为什么需要线程池呢?         设想一下,如果我们使用有任务就开启一个子线程处理,处理完成后,销毁子线程或等...

浅谈python 中类属性共享的问题

感觉这种理解有问题,举个例子来说。 class Dog(object): name = 'dog' def init(self): self.age...

Python学习笔记之pandas索引列、过滤、分组、求和功能示例

本文实例讲述了Python学习笔记之pandas索引列、过滤、分组、求和功能。分享给大家供大家参考,具体如下: 解析html内容,保存为csv文件 /post/162401.htm 前面...

Python运行的17个时新手常见错误小结

1)忘记在 if , elif , else , for , while , class ,def 声明末尾添加 :(导致 “SyntaxError :invalid syntax”)...

python使用matplotlib绘制折线图教程

python使用matplotlib绘制折线图教程

matplotlib简介 matplotlib 是python最著名的绘图库,它提供了一整套和matlab相似的命令API,十分适合交互式地行制图。而且也可以方便地将它作为绘图控件,嵌入...