#includeusing namespace std;
void readYUV()
{FILE* pfImgRaw = NULL;
if (!(pfImgRaw = fopen("E:/path/to/your/nv12.yuv", "rb")))
{printf("open input file failure!\n");
}
printf("load yuv file is OK!\n");
}
2.獲取YUV視頻幀數(shù)#includeusing namespace std;
void countYUVFrame()
{int imgWidth = 1280;
int imgHeight = 720;
int nums_frame;
FILE* pfImgRaw = NULL;
if (!(pfImgRaw = fopen("E:/path/to/your/nv12.yuv", "rb")))
{printf("open input file failure!\n");
}
printf("load yuv file is OK!\n");
fseek(pfImgRaw, 0, SEEK_END); //將文件的指針指到文件末尾
long frame_size = ftell(pfImgRaw); //該函數(shù)返回位置標識符的當前值。如果發(fā)生錯誤,則返回 -1L.
long persize = imgWidth * imgHeight * 3 / 2; //默認YUV格式為nv12,所以每幀的大小為w*h*3/2
nums_frame = (int)(frame_size / persize); //總幀數(shù)=文件總大小/每幀大小
rewind(pfImgRaw); //防止稍后還需要操作文件,重新將文件的指針指向開頭
}
3.將讀取的YUV數(shù)據(jù)用opencv顯示出來#include#include#include#include "opencv2/imgproc/imgproc_c.h"
using namespace std;
using namespace cv;
void countYUVFrame()
{int imgWidth = 1280;
int imgHeight = 720;
int nums_frame;
unsigned char* pu8ImgCur;
int l32BufLen = imgWidth * imgHeight * 1.5;
FILE* pfImgRaw = NULL;
if (!(pfImgRaw = fopen("E:/path/to/your/nv12.yuv", "rb")))
{printf("open input file failure!\n");
}
printf("load yuv file is OK!\n");
if (!(pu8ImgCur = (unsigned char*)malloc(l32BufLen)))
{printf("malloc error!\n");
}
memset(pu8ImgCur, 0, l32BufLen);
fseek(pfImgRaw, 0, SEEK_END); //文件的指針指到文件末尾
long frame_size = ftell(pfImgRaw); //該函數(shù)返回位置標識符的當前值。如果發(fā)生錯誤,則返回 -1L.
long persize = imgWidth * imgHeight * 3 / 2; //默認YUV格式為nv12,所以每幀的大小為w*h*3/2
nums_frame = (int)(frame_size / persize); //總幀數(shù)=文件總大小/每幀大小
rewind(pfImgRaw); //防止稍后還需要操作文件,重新將文件的指針指向開頭
fread(pu8ImgCur, sizeof(unsigned char), l32BufLen, pfImgRaw);
cv::Mat cv_img;
cv::Mat cv_yuv(imgHeight + imgHeight / 2, imgWidth, CV_8UC1, pu8ImgCur);//pFrame為YUV數(shù)據(jù)地址,另外這里就是用 CV_8UC1非 CV_8UC3.
cv_img = cv::Mat(imgHeight, imgWidth, CV_8UC3);
cv::cvtColor(cv_yuv, cv_img, COLOR_YUV2BGR_NV12); //這里的Mat應該是BGR順序的,但是顯示的又是RGB順序,我不太明白
imshow("result", cv_img);
waitKey(0);
fclose(pfImgRaw);
}
你是否還在尋找穩(wěn)定的海外服務器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧
成都創(chuàng)新互聯(lián)成都網(wǎng)站建設按需搭建網(wǎng)站,是成都網(wǎng)站營銷公司,為宣傳片制作提供網(wǎng)站建設服務,有成熟的網(wǎng)站定制合作流程,提供網(wǎng)站定制設計服務:原型圖制作、網(wǎng)站創(chuàng)意設計、前端HTML5制作、后臺程序開發(fā)等。成都網(wǎng)站制作熱線:028-86922220