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; }
相关文章推荐
- 在 MFC 框架中,有什么方法能直接将 OpenCV 2.0 库中 Mat 格式的图片传递到 Picture Control(图片控件)显示?
- Halcon13菜鸟 Opencv Mat 转halcon并且显示 vs2013
- MFC显示Mat数据格式(Opencv)图片,保证4字节对齐,图片缩放不错行
- ubuntu 下 利用ffplay 播放 yuv 格式的文件
- MFC显示Mat数据格式(Opencv)图片,保证4字节对齐,图片缩放不错行
- ffplay播放原始格式的音频文件(PCM)和视频文件(YUV)
- ffmpeg opencv 打开视频文件,并且播放
- opencv 读取yuv格式数据文件
- ffmpeg和opencv 播放视频文件并显示
- python︱matplotlib使用(读入、显示、写出、opencv混用、格式转换...)
- 在MFC中显示OpenCV的Mat格式图像和打开摄像头
- 在MFC图像控件上显示opencv Mat格式图片
- OpenCV图像MAT格式转换为QT中QImage并显示
- 知乎上有一个问题“在mfc框架中,有上面方法能直接将opencv2.0库中的Mat格式图片传递到Picture Control”中显示?
- ffmpeg中avframe的YUV格式数据到OpenCV中Mat的BGR格式转换
- 修改ubuntu文件路径显示格式
- 利用OpenCV读入一个彩色视频文件并以灰度格式输出
- word文档转换成swf格式文件在网页中用flash显示
- OpenCV支持的AVI文件格式
- Fedora 8 播放 mp3, 与 rmvb, rm,avi等格式文件的解决办法