OpenCV读取YUV420p文件并转换为Mat类型
来源:互联网 发布:俄罗斯港口知乎 编辑:程序博客网 时间:2024/05/20 18:51
参考文章:
http://blog.csdn.net/gxiaob/article/details/9718071
这里直接封装成了 Mat类型,增加了YUV400格式的转换:
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, true); 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 = Mat(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);}
0 0
- OpenCV读取YUV420p文件并转换为Mat类型
- opencv读取txt文件,并赋值为Mat矩阵
- Java实现GDAL读取TIF并转换为OpenCV(Mat格式)
- Opencv读取mat和cvMat元素&&Mat与IplImage和CvMat类型之间的相互转换
- OpenCV 读取文件并转换灰度图像
- opencv 数组转换为mat
- 在Python读取mat文件,并保存为pickle格式
- Mat转换为IplImag*类型
- YUV420sp转换为YUV420p
- 读取文件内容并转换为对象
- opencv Mat类型读取像素点
- 【OpenCV】cv::Mat与IplImage类型转换
- OpenCV中Mat,IplImage,CVMat类型转换
- opencv中Mat类型转换 ConvertTo
- 将MATLAB中的mat数据转换为OpenCV能够读取的xml数据
- OpenCV图像MAT格式转换为QT中QImage并显示
- c读取mat文件,并调用m生成的dll中的函数,函数返回值保存为mat文件
- YUV420P转换为RGB32格式
- js 获取地址栏参数
- 解决国内NPM安装依赖速度慢问题
- Oracle 分析函数 Last_value和First_Value
- Struts2相关的jar包
- ios集成环信遇到的相关问题整理
- OpenCV读取YUV420p文件并转换为Mat类型
- NYOJ题目5
- day79_babasport第五天
- 自定义View-太极旋转
- Player Settings导出设置
- 十五、this 的值详解
- JS生成26个大小写英文字母
- ROS初学者资料
- tpshop好用吗