1.僅使用 OpenCV
#include <stdio.h>
#include <iostream>
#include <time.h>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/types_c.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
int main() {
VideoCapture capturer("C:/Users/76371/Desktop/RISE.mp4");
double startTime, endTime;
if (!capturer.isOpened()) {
cout << "Open video failed!" << endl;
return false;
}
int fps = capturer.get(CV_CAP_PROP_FPS);
int width = capturer.get(CV_CAP_PROP_FRAME_WIDTH);
int height = capturer.get(CV_CAP_PROP_FRAME_HEIGHT);
cout << "FPS:" << fps << endl;
cout << "Width:" << width << endl;
cout << "Height:" << height << endl;
VideoWriter writer("C:/Users/76371/Desktop/opencv_output.mp4", CV_FOURCC('M', 'P', '4', '2'), fps, Size(width, height), false);
startTime = getTickCount();
Mat frame, gray;
while (capturer.read(frame)) {
cvtColor(frame, gray, COLOR_RGB2GRAY);
writer.write(gray);
}
endTime = getTickCount();
cout << "Duration:" << (endTime - startTime) / getTickFrequency() << "s" << endl;
capturer.release();
writer.release();
return 0;
}
2.集顯使用 OpenCL
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/types_c.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/ocl.hpp>
using namespace std;
using namespace cv;
using namespace ocl;
int main() {
VideoCapture capturer("C:/Users/76371/Desktop/RISE.mp4");
double startTime, endTime;
if (!capturer.isOpened()) {
cout << "Open video failed!" << endl;
return false;
}
int fps = capturer.get(CV_CAP_PROP_FPS);
int width = capturer.get(CV_CAP_PROP_FRAME_WIDTH);
int height = capturer.get(CV_CAP_PROP_FRAME_HEIGHT);
cout << "FPS:" << fps << endl;
cout << "Width:" << width << endl;
cout << "Height:" << height << endl;
VideoWriter writer("C:/Users/76371/Desktop/opencl_output.mp4", CV_FOURCC('M', 'P', '4', '2'), fps, Size(width, height), false);
startTime = getTickCount();
UMat frame, gray;
setUseOpenCL(true);
while (capturer.read(frame)) {
cvtColor(frame, gray, COLOR_RGB2GRAY);
writer.write(gray.getMat(ACCESS_RW));
}
endTime = getTickCount();
cout << "Duration:" << (endTime - startTime) / getTickFrequency() << "s" << endl;
capturer.release();
writer.release();
return 0;
}
3.獨(dú)顯使用 OpenCL
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <time.h>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/types_c.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/ocl.hpp>
using namespace std;
using namespace cv;
using namespace ocl;
int main() {
VideoCapture capturer("C:/Users/76371/Desktop/RISE.mp4");
double startTime, endTime;
if (!capturer.isOpened()) {
cout << "Open video failed!" << endl;
return false;
}
int fps = capturer.get(CV_CAP_PROP_FPS);
int width = capturer.get(CV_CAP_PROP_FRAME_WIDTH);
int height = capturer.get(CV_CAP_PROP_FRAME_HEIGHT);
cout << "FPS:" << fps << endl;
cout << "Width:" << width << endl;
cout << "Height:" << height << endl;
VideoWriter writer("C:/Users/76371/Desktop/opencl_output.mp4", CV_FOURCC('M', 'P', '4', '2'), fps, Size(width, height), false);
startTime = getTickCount();
UMat frame, gray;
setUseOpenCL(true);
while (capturer.read(frame)) {
cvtColor(frame, gray, COLOR_RGB2GRAY);
writer.write(gray.getMat(ACCESS_RW));
}
endTime = getTickCount();
cout << "Duration:" << (endTime - startTime) / getTickFrequency() << "s" << endl;
capturer.release();
writer.release();
return 0;
}