/**
* Opencv中一般是读取 demo.avi 格式视频,下面程序是直接读取 demo.yuv 然后使用 Mat 保存
* 再使用 cv::cvtColor() 将每帧的 yuv 数据转化为 rgb 然后显示
*/
- #include <opencv/cv.h>
- #include <fstream>
- #include <iostream>
- #include <opencv2/core/core.hpp>
- #include <opencv2/highgui/highgui.hpp>
- #include <opencv2/imgproc/imgproc.hpp>
- using namespace std;
- using namespace cv;
- const int width = 1280;
- const int height = 720;
- const int framesize = width * height * 3 / 2; //一副图所含的像素个数
- typedef struct planet
- {
- char name[framesize];
- double population;
- double g;
- } PLANET;
- int main()
- {
- /
- // 计算视频的帧数,怎样替换成c语言形式的?
- PLANET pl;
- ifstream fin;
- fin.open("demo.yuv", ios_base::in|ios_base::binary);
- if(fin.fail())
- {
- cout << "the file is error" << endl;
- return -1;
- }
- fin.seekg(0, ios::end); //设置文件指针到文件流的尾部
- streampos ps = fin.tellg(); //指出当前的文件指针
- unsigned long NumberPixe = ps;
- cout << "file size: " << ps << endl; //输出指针的位置
- unsigned FrameCount = ps / framesize; //帧大小
- cout << "frameNuber: " << FrameCount; //输出帧数
- fin.close();
- /
- FILE* fileIn = fopen("demo.yuv", "rb+");
- unsigned char* pYuvBuf = new unsigned char[framesize]; //一帧数据大小
- //存储到图像
- namedWindow("yuv", 1);
- for(int i = 0; i < FrameCount; ++i)
- {
- fread(pYuvBuf, framesize*sizeof(unsigned char), 1, fileIn);
- cv::Mat yuvImg;
- yuvImg.create(height*3/2, width, CV_8UC1);
- memcpy(yuvImg.data, pYuvBuf, framesize*sizeof(unsigned char));
- cv::Mat rgbImg;
- cv::cvtColor(yuvImg, rgbImg, CV_YUV2BGR_I420);
- cv::imshow("yuv", yuvImg); //只显示y分量
- cv::imshow("rgbImg", rgbImg);
- printf("第 %d 帧\n", i);
- int c = waitKey(30);
- if((char)c == 27)
- {
- break;
- }
- }
- fclose(fileIn);
- cvDestroyWindow("yuv");
- cin.get();
- cin.get();
- return 0;
- }