在视频处理中改变fps有哪些问题?

时间:2015-01-03 13:27:53

标签: opencv image-processing opencl video-processing

我正在尝试使用OpenCV和OpenCL检测运动中的白色对象。我使用的相机是30 fps的笔记本电脑相机。我所观察到的是,在不同的位置,我得到了不同的fps,相机和代码是相同的。当我执行代码时,在我的房间里,我获得了30分的完整fps,而在实验室中它是22 ...请任何人都可以告诉它可能的原因是什么以及如何纠正...我用过的代码如下......提前谢谢你......

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

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;
    }

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

   int iLowH = 0;
   int iHighH = 255;

   int iLowS = 0; 
   int iHighS = 255;

   int iLowV = 0;
   int iHighV = 255;

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

   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);

   ocl::oclMat alpha;
   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;
        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;
      }

      alpha.upload(imgOriginal);
      ocl::oclMat imgHSV;

      ocl::cvtColor(alpha, imgHSV, CV_RGB2HSV); //Convert the captured frame from BGR to HSV
      ocl::oclMat channel[4];
      ocl::split(alpha,channel);
      ocl::oclMat imgThresholded[2];

      ocl::threshold(channel[0], imgThresholded[0] , iLowH , 255 , 0 );
      ocl::threshold(channel[0], imgThresholded[1] , iHighH , 255 , 1 );
      ocl::bitwise_and(imgThresholded[0],imgThresholded[1],channel[0]);

      ocl::threshold(channel[1], imgThresholded[0] , iLowS , 255 , 0 );
      ocl::threshold(channel[1], imgThresholded[1] , iHighS , 255 , 1 );
      ocl::bitwise_and(imgThresholded[0],imgThresholded[1],channel[1]);

      ocl::threshold(channel[2], imgThresholded[0] , iLowV , 255 , 0 );
      ocl::threshold(channel[2], imgThresholded[1] , iHighV , 255 , 1 );
      ocl::bitwise_and(imgThresholded[0],imgThresholded[1],channel[2]);

      ocl::bitwise_and(channel[0],channel[1],imgThresholded[0]);
      ocl::bitwise_and(imgThresholded[0],channel[2],imgThresholded[1]);

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

      //morphological closing (fill small holes in the foreground)
      ocl::dilate( imgThresholded[1], imgThresholded[1], getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) ); 
      ocl::erode(imgThresholded[1], imgThresholded[1], getStructuringElement(MORPH_ELLIPSE, Size(5, 5)) );
      int c= ocl::countNonZero(imgThresholded[1]);
      Mat result = imgThresholded[1];
      //Mat r2 = channel[0];

      imshow("Thresholded Image", result); //show the thresholded image
      //imshow("Original", r2); //show the original image
      cout<<"\t"<<c<<endl;

      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;
}

1 个答案:

答案 0 :(得分:0)

您正在图像捕获循环中进行图像处理。图像处理的时间取决于输入,因为例如侵蚀操作不会在每个图像像素上执行,而是在满足某些条件的图像上执行。因此,在不同的环境中获得不同的执行时间应该不会感到惊讶。 此外,我可以想象笔记本电脑相机驱动程序正在执行一些预处理操作,例如亮度调整,然后设置帧已准备好输出的标志。如果您对恒定帧速率感兴趣,则应禁用此类预处理,或使用SDK无法使用此类功能的其他相机。