title: gpu::morphologyEx is slower than morphologyEx in CPU?
body: <p>I am writing a c++ code for comparing the performance of morphologyEx method of opencv using the CPU and GPU versions. Here is my code:</p>
  
  <pre><code>#include &lt;opencv2/opencv.hpp&gt;
  #include &lt;opencv2/gpu/gpu.hpp&gt;
  #include &lt;sys/time.h&gt;       
  #include &lt;ctime&gt;
  using namespace cv;
  using namespace std;
  
  
  double start_timer()
  {
       double start_time = (double) getTickCount();
       return start_time;
  }
  
  double end_timer(double start_time,int num_tests)
  {
      double time = (1000 * ((double) getTickCount() - start_time)/ getTickFrequency());
      cout &lt;&lt; "Average time of " &lt;&lt; num_tests  &lt;&lt; " frames is: " &lt;&lt; time/num_tests &lt;&lt;  " ms" &lt;&lt; endl;
      return time;
  }
  
  
  int main()
  {
      Mat cpuSrc;
      cv::gpu::GpuMat src_gpu, dst_gpu;
      Mat dst;
      Mat element;
      int element_shape = MORPH_RECT;
      element = getStructuringElement(element_shape, Size(10, 10 ), Point(-1, -1) );
      cpuSrc = imread("images.jpeg",CV_LOAD_IMAGE_ANYDEPTH);
  
      if (!cpuSrc.data)
      {
          cerr &lt;&lt; "Cannot read the data" &lt;&lt; endl;
          return -1;
      }
  
  
      cout &lt;&lt; "Starting calculating time for CPU ....." &lt;&lt; endl;
      double start_time = start_timer();
      int d = 0;
      while(d&lt;100)
      {
          cv::morphologyEx(cpuSrc, dst, CV_MOP_OPEN, element,Point(-1,-1),1);
      }
  
      double total_time_cpu = end_timer(start_time,d);
  
  
  
  //--------------------------------------------------------------
      cout &lt;&lt; "Starting calculating time for GPU ....." &lt;&lt; endl;
      d = 0;
      cv::gpu::GpuMat buf1, buf2;
      gpu::Stream stream;
      double start_time_1 = start_timer();
  
      while(d&lt;100)
      {
          stream.enqueueUpload(cpuSrc, src_gpu);
          cv::gpu::morphologyEx(src_gpu,dst_gpu,CV_MOP_OPEN,element,
                     buf1,buf2,Point(-1,-1),1,stream);
          stream.enqueueDownload(dst_gpu, dst);
  
      }
      stream.waitForCompletion();
      double total_time_gpu = end_timer(start_time_1,d);
  
      cout &lt;&lt; "Gain is: " &lt;&lt; total_time_cpu / total_time_gpu &lt;&lt; endl;
      return 0;
  }
  </code></pre>
  
  <blockquote>
    <blockquote>
      <p>I am using a loop as if i am simulating a video that contains 100 frames. I am using NVIDIA Corporation GF110 [GeForce GTX 570] and Intel Corporation Xeon E5/Core i7 DMI2. Moreover, i tested the time for uploading and downloading and it is very large in the first frame but after that it can be neglected approximately for uploading it is 0.02ms per frame and downloading is 0.1ms and the main time consumption is with the morphologyEx operation. </p>
    </blockquote>
  </blockquote>
  
  <hr>
  
  <p>The time results for this simulations are as follows: </p>
  
  <blockquote>
    <p>for CPU morphology version, The 
     average time of 100 frames is:: 0.027349 ms and for the GPU version is:: 18.0128 ms</p>
    
    <blockquote>
      <p>Could you please help me to figure out what might be the reasons for such unexpected performance?!!</p>
    </blockquote>
  </blockquote>
  
  <p>Thank you so much in advance.</p>