總結一下視頻的讀取和寫入,opencv1+和opencv2+以後的版本都總結一下。
先總結一下opencv1+的用法,現在使用的opencv都是2.0以後了,不過1.0版本是使用指針,用起來確實感覺很爽,簡單實用。有人不喜歡1.0+的釋放控件,我覺得這是個好習慣。
視頻讀入:
int main(int argc,char* argv[] )
{
//argv[1] input video file
//argv[2] name of new output file
argv[1] = ".../12.mp4";
CvCapture* capture = NULL;
capture = cvCreateFileCapture(argv[1]);
if (!capture)
return - 1;
IplImage *bgr_frame = cvQueryFrame(capture);
//幀率,保證播放的正常速度
double fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
//大小
CvSize size = cvSize(
(int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH),
(int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT));
while ((bgr_frame = cvQueryFrame(capture)) != NULL)//(bgr_frame = cvQueryFrame(capture))!=NULL
{
cvNamedWindow("capture", 1);
cvShowImage("capture", bgr_frame);
cvWaitKey(fps);
}
cvReleaseCapture(&capture);
cvReleaseImage(&bgr_frame);
return 0;
}
int main(int argc, char* argv[])
{
argv[1] = ".../12.mp4";
VideoCapture capture(argv[1]);
if (!capture.isOpened())
{
cout << "fail to open" << endl;
return -1;
}
//視頻寬高
Size videoResolution = Size((int)capture.get(CV_CAP_PROP_FRAME_WIDTH),
(int)capture.get(CV_CAP_PROP_FRAME_HEIGHT));
//獲取視頻圖像幀率
double fps = capture.get(CV_CAP_PROP_FPS);
//視頻總幀
double totalFrame = capture.get(CV_CAP_PROP_FRAME_COUNT);
cout << "總幀數:" << totalFrame << endl;
cout << "幀率:" << fps << endl;
cout << "視頻寬、高:" << videoResolution.width << videoResolution.height << endl;
Mat frameImg;
while (true)
{
capture >> frameImg;
imshow("frame", frameImg);
waitKey(fps);
}
return 0;
}
CvVideoWriter* writer = NULL;
writer = cvCreateVideoWriter(argv[2], CV_FOURCC('M', 'J', 'P', 'G'), fps, size,0);
while(1)
{
cvWriteFrame(writer, bgr_frame);
}
//釋放
cvReleaseVideoWriter(&writer);
IplImage* gray_frame = cvCreateImage(size, IPL_DEPTH_8U, 1);
cvCvtColor(bgr_frame, gray_frame, CV_BGR2GRAY);
//opencv 2.0+的用法
cvtColor(bgr_frame,gray_frame,CV_BGR2GRAY);
int main(int argc,char* argv[] )
{
//argv[1] input video file
//argv[2] name of new output file
argv[1] = ".../12.mp4";
argv[2] = ".../12out.avi";
CvCapture* capture = NULL;
capture = cvCreateFileCapture(argv[1]);
if (!capture)
return - 1;
IplImage *bgr_frame = cvQueryFrame(capture);
IplImage *gray_frame;
//幀率,保證播放的正常速度
double fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
//大小
CvSize size = cvSize(
(int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH),
(int)cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT));
gray_frame = cvCreateImage(size, IPL_DEPTH_8U, 1);
CvVideoWriter* writer = NULL;
writer = cvCreateVideoWriter(argv[2], CV_FOURCC('M', 'J', 'P', 'G'), fps, size, 0);
while ((bgr_frame = cvQueryFrame(capture)) != NULL)//(bgr_frame = cvQueryFrame(capture))!=NULL
{
cvCvtColor(bgr_frame, gray_frame, CV_BGR2GRAY);
cvNamedWindow("capture", 1);
cvShowImage("capture", bgr_frame);
cvShowImage("gray_grame", gray_frame);
cvWriteFrame(writer, gray_frame);
cvWaitKey(fps);
}
cvReleaseCapture(&capture);
cvReleaseImage(&bgr_frame);
cvReleaseVideoWriter(&writer);
return 0;
}
string outputVideoPath = "...\\input8Write.avi";
VideoWriter outputVideo;
Size videoResolution = Size((int)video.get(CV_CAP_PROP_FRAME_WIDTH), (int)video.get(CV_CAP_PROP_FRAME_HEIGHT));
outputVideo.open(outputVideoPath, -1, 25, videoResolution, true);
while(!frame.empty())
{
outputVideo << image;
}
int main(int argc, char* argv[])
{
argv[1] = ".../12.mp4";
VideoCapture capture(argv[1]);
if (!capture.isOpened())
{
cout << "fail to open" << endl;
return -1;
}
//視頻寬高
Size videoResolution = Size((int)capture.get(CV_CAP_PROP_FRAME_WIDTH),
(int)capture.get(CV_CAP_PROP_FRAME_HEIGHT));
//獲取視頻圖像幀率
double fps = capture.get(CV_CAP_PROP_FPS);
//視頻總幀
double totalFrame = capture.get(CV_CAP_PROP_FRAME_COUNT);
cout << "總幀數:" << totalFrame << endl;
cout << "幀率:" << fps << endl;
cout << "視頻寬、高:" << videoResolution.width <<" "<< videoResolution.height << endl;
string outputVideoPath = "E:/數學建模/12out2.avi";
VideoWriter outputVideo;
outputVideo.open(outputVideoPath, CV_FOURCC('M', 'J', 'P', 'G'), fps, videoResolution, true);
Mat frameImg,grayImage;
while (true)
{
capture >> frameImg;
if (!frameImg.empty())
{
cvtColor(frameImg, grayImage, CV_BGR2GRAY);
imshow("frame", frameImg);
imshow("grayImage", grayImage);
outputVideo << grayImage;
waitKey(fps);
}
else
{
break;
}
}
return 0;
}