您的位置:首页 > 运维架构

YUV文件转opencv的Mat格式并且显示播放

2018-03-30 10:24 501 查看
#include <stdio.h>
#include <string.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2\opencv.hpp>
#include <iostream>
//#include "SDL.h"
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavutil/imgutils.h"

};
using namespace std;
using namespace cv;
void conv_yuv420_to_mat(Mat &dst, unsigned char* pYUV420, int width, int height)
{
if (!pYUV420) {
return;
}

IplImage *yuvimage, *rgbimg, *yimg, *uimg, *vimg, *uuimg, *vvimg;

int nWidth = width;
int nHeight = height;
rgbimg = cvCreateImage(cvSize(nWidth, nHeight), IPL_DEPTH_8U, 3);
yuvimage = cvCreateImage(cvSize(nWidth, nHeight), IPL_DEPTH_8U, 3);

yimg = cvCreateImageHeader(cvSize(nWidth, nHeight), IPL_DEPTH_8U, 1);
uimg = cvCreateImageHeader(cvSize(nWidth / 2, nHeight / 2), IPL_DEPTH_8U, 1);
vimg = cvCreateImageHeader(cvSize(nWidth / 2, nHeight / 2), IPL_DEPTH_8U, 1);

uuimg = cvCreateImage(cvSize(nWidth, nHeight), IPL_DEPTH_8U, 1);
vvimg = cvCreateImage(cvSize(nWidth, nHeight), IPL_DEPTH_8U, 1);

cvSetData(yimg, pYUV420, nWidth);
cvSetData(uimg, pYUV420 + nWidth * nHeight, nWidth / 2);
cvSetData(vimg, pYUV420 + long(nWidth*nHeight*1.25), nWidth / 2);
cvResize(uimg, uuimg, CV_INTER_LINEAR);
cvResize(vimg, vvimg, CV_INTER_LINEAR);

cvMerge(yimg, uuimg, vvimg, NULL, yuvimage);
cvCvtColor(yuvimage, rgbimg, CV_YCrCb2RGB);

cvReleaseImage(&uuimg);
cvReleaseImage(&vvimg);
cvReleaseImageHeader(&yimg);
cvReleaseImageHeader(&uimg);
cvReleaseImageHeader(&vimg);

cvReleaseImage(&yuvimage);

//dst = Mat(*rgbimg,int(1));
dst = cvarrToMat(rgbimg,true);
//rgbimg->
cvReleaseImage(&rgbimg);
}

void conv_yuv400_to_mat(Mat &dst, unsigned char* pYUV400, int nWidth, int nHeight, int bit_depth)
{
IplImage *yimg;

if (!pYUV400) {
return;
}

if (bit_depth == 8) {
yimg = cvCreateImageHeader(cvSize(nWidth, nHeight), IPL_DEPTH_8U, 1);
}
else {
yimg = cvCreateImageHeader(cvSize(nWidth, nHeight), IPL_DEPTH_16U, 1);
}

cvSetData(yimg, pYUV400, nWidth);

dst = cvarrToMat(yimg, true);
cvReleaseImageHeader(&yimg);
}

/* ---------------------------------------------------------------------------
*/
void DisplayYUV420(const char *s_path, int img_w, int img_h)
{
printf("yuv file img_w: %d, img_h: %d \n", img_w, img_h);

FILE* pFileIn = fopen(s_path, "rb");
int bufLen = img_w * img_h * 3 / 2;
unsigned char* pYuvBuf = new unsigned char[bufLen];
int iCount = 0;

for (iCount = 0; iCount < 200; iCount++) {
Mat rgbImg;
fread(pYuvBuf, bufLen * sizeof(unsigned char), 1, pFileIn);

conv_yuv420_to_mat(rgbImg, pYuvBuf, img_w, img_h);

cv::imshow("img", rgbImg);
cv::waitKey(1);

printf("%d \n", iCount);
}

delete[] pYuvBuf;
fclose(pFileIn);
}

/* ---------------------------------------------------------------------------
*/
void DisplayYUV400(const char *s_path, int img_w, int img_h, int bit_depth)
{
printf("yuv file img_w: %d, img_h: %d \n", img_w, img_h);

FILE* pFileIn = fopen(s_path, "rb");
int size_of_pixel = (bit_depth <= 8) ? 1 : 2;
int bufLen = img_w * img_h * size_of_pixel;
unsigned char* pYuvBuf = new unsigned char[bufLen];
int iCount = 0;

for (iCount = 0; iCount < 200; iCount++) {
Mat rgbImg;
fread(pYuvBuf, bufLen * sizeof(unsigned char), 1, pFileIn);

conv_yuv400_to_mat(rgbImg, pYuvBuf, img_w, img_h, bit_depth);

cv::imshow("img", rgbImg);
cv::waitKey(0);

printf("%d \n", iCount);
}

delete[] pYuvBuf;
fclose(pFileIn);
}


main函数调用相应的函数并且显示即可,例如:
#include <stdio.h>
#include <string.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2\opencv.hpp>
#include <iostream>
//#include "SDL.h"
extern "C"
{
#include "libavcodec/avcodec.h"
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
#include "libavutil/imgutils.h"
#include "SDL.h";
#include "SDL_thread.h";

};
using namespace std;
using namespace cv;
#pragma warning(disable : 4996)
const int bpp = 12;
unsigned char buffer[pixel_w*pixel_h*bpp / 8];
Mat m;

void conv_yuv420_to_mat(Mat &dst, unsigned char* pYUV420, int width, int height)
{
if (!pYUV420) {
return;
}

IplImage *yuvimage, *rgbimg, *yimg, *uimg, *vimg, *uuimg, *vvimg;

int nWidth = width;
int nHeight = height;
rgbimg = cvCreateImage(cvSize(nWidth, nHeight), IPL_DEPTH_8U, 3);
yuvimage = cvCreateImage(cvSize(nWidth, nHeight), IPL_DEPTH_8U, 3);

yimg = cvCreateImageHeader(cvSize(nWidth, nHeight), IPL_DEPTH_8U, 1);
uimg = cvCreateImageHeader(cvSize(nWidth / 2, nHeight / 2), IPL_DEPTH_8U, 1);
vimg = cvCreateImageHeader(cvSize(nWidth / 2, nHeight / 2), IPL_DEPTH_8U, 1);

uuimg = cvCreateImage(cvSize(nWidth, nHeight), IPL_DEPTH_8U, 1);
vvimg = cvCreateImage(cvSize(nWidth, nHeight), IPL_DEPTH_8U, 1);

cvSetData(yimg, pYUV420, nWidth);
cvSetData(uimg, pYUV420 + nWidth * nHeight, nWidth / 2);
cvSetData(vimg, pYUV420 + long(nWidth*nHeight*1.25), nWidth / 2);
cvResize(uimg, uuimg, CV_INTER_LINEAR);
cvResize(vimg, vvimg, CV_INTER_LINEAR);

cvMerge(yimg, uuimg, vvimg, NULL, yuvimage);
cvCvtColor(yuvimage, rgbimg, CV_YCrCb2RGB);

cvReleaseImage(&uuimg);
cvReleaseImage(&vvimg);
cvReleaseImageHeader(&yimg);
cvReleaseImageHeader(&uimg);
cvReleaseImageHeader(&vimg);

cvReleaseImage(&yuvimage);

//dst = Mat(*rgbimg,int(1));
dst = cvarrToMat(rgbimg, true);
//rgbimg->
cvReleaseImage(&rgbimg);
}
int main(int argc, char *argv[])
{

FILE *fp = NULL;
fp = fopen("352x288.yuv", "rb+");
if (fp == NULL)
{
printf("Can't open yuv file!\n");
return -1;
}
namedWindow("test");
while (1) {
fread(buffer, 1, pixel_w*pixel_h*bpp / 8, fp);
conv_yuv420_to_mat(m, buffer, 352, 288);
imshow("test", m);
waitKey(40);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  YUV Mat