title: OPENCV4 bad matching on video capture
body: <p>I am trying to find a custom image in a Window Captured (Desktop window, videogame window...), but the matcher fn that I wrote gives me back random locations, not matching the image in the captured window.</p>
  <p>By the way, if I match a bigger image with this smaller one, (both static images) the program find the smaller into the bigger without no problem.</p>
  <p>I just want to find the little image in the captured window. I already achieved this goal on Python easily, so I assume that's it's and issue with color conversion, channel codecs or some value conversion.</p>
  <p>Code looks like this:</p>
  <pre><code>#include &lt;windows.h&gt;
  #include &lt;opencv2/opencv.hpp&gt;
  
  using namespace cv;
  
  using namespace std;
  using namespace cv;
  
  
  Mat hwnd2mat(HWND hwnd)
  {
      HDC deviceContext = GetDC(hwnd);
      HDC memoryDeviceContext = CreateCompatibleDC(deviceContext);
  
      RECT windowRect;
      GetClientRect(hwnd, &amp;windowRect);
  
      int height = windowRect.bottom;
      int width = windowRect.right;
  
      HBITMAP bitmap = CreateCompatibleBitmap(deviceContext, width, height);
  
      SelectObject(memoryDeviceContext, bitmap);
  
      //copy data into bitmap
      BitBlt(memoryDeviceContext, 0, 0, width, height, deviceContext, 0, 0, SRCCOPY);
  
  
      //specify format by using bitmapinfoheader!
      BITMAPINFOHEADER bi;
      bi.biSize = sizeof(BITMAPINFOHEADER);
      bi.biWidth = width;
      bi.biHeight = -height;
      bi.biPlanes = 1;
      bi.biBitCount = 32;
      bi.biCompression = BI_RGB;
      bi.biSizeImage = 0; //because no compression
      bi.biXPelsPerMeter = 0; //we
      bi.biYPelsPerMeter = 0; //we
      bi.biClrUsed = 0; //we ^^
      bi.biClrImportant = 0; //still we
  
      cv::Mat mat = cv::Mat(height, width, CV_8UC4); // 8 bit unsigned ints 4 Channels -&gt; RGBA
  
      //transform data and store into mat.data
      GetDIBits(memoryDeviceContext, bitmap, 0, height, mat.data, (BITMAPINFO*)&amp;bi, DIB_RGB_COLORS);
      //clean up!
      DeleteObject(bitmap);
      DeleteDC(memoryDeviceContext); //delete not release!
      ReleaseDC(hwnd, deviceContext);
  
      return mat;
  }
  
  int find(Mat input_image, Mat filter_image, double threshold = 0.5, bool debug_mode = false) {
  
      Mat img = input_image;
      Mat templ = filter_image;
      Mat result;
      const char* image_window = &quot;Source Image&quot;;
      const char* result_window = &quot;Result window&quot;;
      const int match_method = TM_CCOEFF_NORMED;
  
      Mat img_display;
      img.copyTo( img_display );
      int result_cols =  img.cols - templ.cols + 1;
      int result_rows = img.rows - templ.rows + 1;
      result.create( result_rows, result_cols, CV_8UC4);
      
      matchTemplate(img, templ, result, match_method);
      normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() );
      
      double minVal; double maxVal; Point minLoc; Point maxLoc;
      Point matchLoc;
      minMaxLoc( result, &amp;minVal, &amp;maxVal, &amp;minLoc, &amp;maxLoc, Mat() );
      
      matchLoc = maxLoc;
      
      std::cout &lt;&lt; &quot;Match location -&gt; &quot; &lt;&lt; matchLoc &lt;&lt; endl;
      rectangle( img_display, matchLoc, Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), CV_RGB(0, 255, 0), cv::BORDER_CONSTANT);
      rectangle( result, matchLoc, Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), CV_RGB(0, 255, 0), cv::FILLED);
      
      imshow( image_window, img_display );
      //imshow( result_window, result );
      
      return 0;
  }
  
  int main(int argc, char** argv)
  {
      HWND hwndDesktop = GetDesktopWindow();
  
      int key = 0;
  
      // TEMPLATE MATCHING
      cv::Mat needle_image = cv::imread(&quot;encontrar_partida.jpg&quot;, CV_8UC4);
  
      while (key != 27)
      {
          Mat src = hwnd2mat(hwndDesktop);
          std::cout &lt;&lt; &quot;src type -&gt; &quot; &lt;&lt; src.type() &lt;&lt; endl;
          std::cout &lt;&lt; &quot;needle_image type -&gt; &quot; &lt;&lt; needle_image.type() &lt;&lt; endl;
          cv::Mat new_needle_img;
          cvtColor(needle_image, new_needle_img, IMREAD_COLOR);
          std::cout &lt;&lt; &quot;needle_image AFTER -&gt; &quot; &lt;&lt; new_needle_img.type() &lt;&lt; endl;
  
          // Find the needle image on the source image
          find(src, new_needle_img);
  
          //imshow(&quot;LoL&quot;, src);
         
          key = waitKey(60);
      }
  
      // Prevent to leak memory by don't clean up busy resources
      cv::destroyAllWindows();
  
      return 0;
  
  }
  </code></pre>