opencv - 使用 OpenCL 和 GPU 不会提高我的相机的 fps 性能

标签 opencv image-processing opencl gpu

我使用 OpenCV 和 Visual Studio 2012 编译了这个简单的颜色跟踪图像处理程序。 首先,我使用 CPU 编译它。 程序:

#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <time.h>

using namespace cv;
using namespace std;

 int main( int argc, char** argv )
 {
    time_t t= time(0);
    VideoCapture cap(0); //capture the video from web cam

    if ( !cap.isOpened() )  // if not success, exit program
    {
         cout << "Cannot open the web cam" << endl;
         return -1;
    }
     double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
   double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video

    cout << "Frame size : " << dWidth << " x " << dHeight << endl;

    namedWindow("Control", CV_WINDOW_AUTOSIZE); //create a window called "Control"

  int iLowH = 0;
 int iHighH = 179;

  int iLowS = 0; 
 int iHighS = 255;

  int iLowV = 0;
 int iHighV = 255;

  //Create track bars in "Control" window
 cvCreateTrackbar("LowH", "Control", &iLowH, 179); //Hue (0 - 179)
 cvCreateTrackbar("HighH", "Control", &iHighH, 179);

  cvCreateTrackbar("LowS", "Control", &iLowS, 255); //Saturation (0 - 255)
 cvCreateTrackbar("HighS", "Control", &iHighS, 255);

  cvCreateTrackbar("LowV", "Control", &iLowV, 255); //Value (0 - 255)
 cvCreateTrackbar("HighV", "Control", &iHighV, 255);

    int fps=0;
    int cur=0;
    while (true)
    {
    fps++;
        t=time(0);
        struct tm *tmp = gmtime(&t);

        int h= (t/360) %24;
        int m= (t/60) %60;
        int s = t%60;
        if(cur !=s)
        {
            cout<<fps<<endl;
            fps=0;
            cur=s;
        }
        Mat imgOriginal;

        bool bSuccess = cap.read(imgOriginal); // read a new frame from video

         if (!bSuccess) //if not success, break loop
        {
             cout << "Cannot read a frame from video stream" << endl;
             break;
        }

    Mat imgHSV;

   cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV

  Mat imgThresholded;

   inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded); //Threshold the image

  //morphological opening (remove small objects from the foreground)
  erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
  dilate( imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) ); 

   //morphological closing (fill small holes in the foreground)
  dilate( imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) ); 
  erode(imgThresholded, imgThresholded, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );

   imshow("Thresholded Image", imgThresholded); //show the thresholded image
  imshow("Original", imgOriginal); //show the original image

        if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
       {
            cout << "esc key is pressed by user" << endl;
            break; 
       }
    }

   return 0;

}

我的相机的帧率为 16。 然后我使用 OpenCL(GPU 支持)编译了这个程序。 程序:

#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv2/ocl/ocl.hpp>
#include <time.h>

using namespace cv;
using namespace std;

 int main( int argc, char** argv )
 {
    time_t t= time(0);
    VideoCapture cap(0); //capture the video from web cam

    if ( !cap.isOpened() )  // if not success, exit program
    {
         cout << "Cannot open the web cam" << endl;
         return -1;
    }
     double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
   double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video

    cout << "Frame size : " << dWidth << " x " << dHeight << endl;

    namedWindow("Control", CV_WINDOW_AUTOSIZE); //create a window called "Control"

  int iLowH = 0;
 int iHighH = 179;

  int iLowS = 0; 
 int iHighS = 255;

  int iLowV = 0;
 int iHighV = 255;

  //Create track bars in "Control" window
 cvCreateTrackbar("LowH", "Control", &iLowH, 179); //Hue (0 - 179)
 cvCreateTrackbar("HighH", "Control", &iHighH, 179);

  cvCreateTrackbar("LowS", "Control", &iLowS, 255); //Saturation (0 - 255)
 cvCreateTrackbar("HighS", "Control", &iHighS, 255);

  cvCreateTrackbar("LowV", "Control", &iLowV, 255); //Value (0 - 255)
 cvCreateTrackbar("HighV", "Control", &iHighV, 255);

    int fps=0;
    int cur=0;
    while (true)
    {
    fps++;
        t=time(0);
        struct tm *tmp = gmtime(&t);

        int h= (t/360) %24;
        int m= (t/60) %60;
        int s = t%60;
        if(cur !=s)
        {
            cout<<fps<<endl;
            fps=0;
            cur=s;
        }
        Mat imgOriginal;

        bool bSuccess = cap.read(imgOriginal); // read a new frame from video

         if (!bSuccess) //if not success, break loop
        {
             cout << "Cannot read a frame from video stream" << endl;
             break;
        }

    Mat imgHSV;

   cvtColor(imgOriginal, imgHSV, COLOR_BGR2HSV); //Convert the captured frame from BGR to HSV

  Mat imgThresholded;

   inRange(imgHSV, Scalar(iLowH, iLowS, iLowV), Scalar(iHighH, iHighS, iHighV), imgThresholded); //Threshold the image

  //morphological opening (remove small objects from the foreground)
    ocl::oclMat alpha(imgThresholded);
    ocl::erode(alpha,alpha, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
    ocl::dilate( alpha, alpha, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) ); 

   //morphological closing (fill small holes in the foreground)
    ocl::dilate( alpha, alpha, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) ); 
    ocl::erode(alpha, alpha, getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
    imgThresholded = Mat(alpha);
   imshow("Thresholded Image", imgThresholded); //show the thresholded image
  imshow("Original", imgOriginal); //show the original image

        if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
       {
            cout << "esc key is pressed by user" << endl;
            break; 
       }
    }

   return 0;

}

但现在我得到了 fps = 10 。请有人能告诉为什么会这样。 我在某处读到 GPU 支持提高了 fps 性能。我使用的显卡是 AMD RAEDON。

最佳答案

GPU 专为大吞吐量而设计,但将数据从 CPU 内存移动到 GPU 内存需要花费大量时间。你不应该认为 GPU 总是在增加 fps。这完全取决于 GPU 的功率收集情况。

在您的情况下,您似乎对每一帧都做了很少的工作。所以我的猜测是您的系统大部分时间都在使用将帧移至 GPU 并将结果移回。

关于opencv - 使用 OpenCL 和 GPU 不会提高我的相机的 fps 性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25703789/

相关文章:

image-processing - 图像交叉检测

performance - 改进感知器神经网络的 OpenCL 内核

opencl - 如何使自定义指标中的 OnCalculate() 函数在 MQL5/OpenCL 中使用 GPU?

opencv - 断言失败(src.type()== dst.type())在opencv

algorithm - 如何在绝对灰度图像中而不是在二进制图像中检测直线?

opencv - OpenCV resize()导致新图像中的剪切

python - 查找修改后的图像 - 图像取证

matlab - 我是否在 MATLAB imfilter 中发现了单精度输入的错误?

opencl - 如何发布事件?

python - Python 3.8.5 cv2 -215:断言失败