溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么將YUV轉(zhuǎn)換為jpg圖像

發(fā)布時(shí)間:2021-04-17 16:08:15 來源:億速云 閱讀:829 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細(xì)講解有關(guān)怎么將YUV轉(zhuǎn)換為jpg圖像,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個(gè)參考,希望大家閱讀完這篇文章后對(duì)相關(guān)知識(shí)有一定的了解。

代碼如下:

# 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;
}

關(guān)于怎么將YUV轉(zhuǎn)換為jpg圖像就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI