我正在使用Visual Studio 2017和OpenCV 3.1。没有任何图像处理,帧速率非常好,但是一旦我进行图像处理,只要转换为灰度图像并应用高斯模糊,帧速率就会急剧下降。 Python中的相同代码几乎以实时帧速率运行(不应该C ++比Python快)。这是我的代码。
#include "opencv2\core\core.hpp"
#include "opencv2\imgproc\imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
Mat processed_frame;
Mat imageProcessing(Mat frame_proc) {
cvtColor(frame_proc, processed_frame, COLOR_BGR2GRAY);
medianBlur(processed_frame, processed_frame, 5);
GaussianBlur(processed_frame, processed_frame, Size(21, 21), 0);
threshold(processed_frame, processed_frame, 127, 255, 0);
cv::Mat structuringElement5x5 = cv::getStructuringElement(cv::MORPH_RECT, cv::Size(5, 5));
for (unsigned int i = 0; i < 5; i++) {
cv::dilate(processed_frame, processed_frame, structuringElement5x5);
}
for (unsigned int j = 1; j < 2; j++) {
erode(processed_frame, processed_frame, structuringElement5x5);
}
return (processed_frame);
}
int main() {
Mat frame;
Mat img3;
VideoCapture cap("/path/to/Video/File");
if (!cap.isOpened()) {
cout << "Cannot open file" << endl;
return -1;
}
bool bSuccess = cap.read(frame);
while (bSuccess) {
cap >> frame;
frame = imageProcessing(frame);*IF I COMMENT THIS LINE, FRAME RATE IS VERY GOOD
imshow("VideoCapture", frame);
cap.set(CV_CAP_PROP_FPS, 25);
if (waitKey(1) == 27) {
break;
}
}
return 0;
}