Note that there are some explanatory texts on larger screens.

plurals
  1. POOpenCV: assertation failed ((img.depth() == CV_8U || img.depth() == CV_32F) && img.type() == templ.type())
    text
    copied!<p>I get this error from above and don't know how to avoid it. My aim is to get a screenshot and then perform a template matching on it to see if an icon is shown on the screen at this moment. Till now it's just the position of the icon. My code:</p> <pre><code>#include "opencv2/highgui/highgui.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/core/core.hpp" #include &lt;Windows.h&gt; #include &lt;iostream&gt; #include &lt;stdio.h&gt; #include &lt;string&gt; using namespace std; using namespace cv; Mat hwnd2mat(); /// Global Variables Mat img; Mat templ; Mat result; int main() { /// Load image and template templ = imread( "Template.bmp",1); templ.convertTo(templ, CV_8U); //img = imread( "Image.jpg", 1 ); img = hwnd2mat(); /// Create the result matrix int result_cols = img.cols - templ.cols + 1; int result_rows = img.rows - templ.rows + 1; result.create( result_cols, result_rows, CV_8U); /// Do the Matching and Normalize matchTemplate( img, templ, result, CV_TM_SQDIFF ); normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() ); /// Localizing the best match with minMaxLoc double minVal; double maxVal; Point minLoc; Point maxLoc; Point matchLoc; minMaxLoc( result, &amp;minVal, &amp;maxVal, &amp;minLoc, &amp;maxLoc, Mat() ); /// show best position matchLoc = minLoc; cout&lt;&lt;matchLoc&lt;&lt;" is best position"&lt;&lt;endl; waitKey(0); return 0; } Mat hwnd2mat(){ HWND hwnd = GetDesktopWindow(); HDC hwindowDC,hwindowCompatibleDC; int height,width,srcheight,srcwidth; HBITMAP hbwindow; Mat src; BITMAPINFOHEADER bi; hwindowDC=GetDC(hwnd); hwindowCompatibleDC=CreateCompatibleDC(hwindowDC); SetStretchBltMode(hwindowCompatibleDC,COLORONCOLOR); RECT windowsize; // get the height and width of the screen GetClientRect(hwnd, &amp;windowsize); srcheight = windowsize.bottom; srcwidth = windowsize.right; height = windowsize.bottom/1; //change this to whatever size you want to resize to width = windowsize.right/1; src.create(height,width,CV_8U); // create a bitmap hbwindow = CreateCompatibleBitmap( hwindowDC, width, height); bi.biSize = sizeof(BITMAPINFOHEADER); bi.biWidth = width; bi.biHeight = -height; //this is the line that makes it draw upside down or not bi.biPlanes = 1; bi.biBitCount = 32; bi.biCompression = BI_RGB; bi.biSizeImage = 0; bi.biXPelsPerMeter = 0; bi.biYPelsPerMeter = 0; bi.biClrUsed = 0; bi.biClrImportant = 0; // use the previously created device context with the bitmap SelectObject(hwindowCompatibleDC, hbwindow); // copy from the window device context to the bitmap device context StretchBlt( hwindowCompatibleDC, 0,0, width, height, hwindowDC, 0, 0,srcwidth,srcheight, SRCCOPY); //change SRCCOPY to NOTSRCCOPY for wacky colors ! GetDIBits(hwindowCompatibleDC,hbwindow,0,height,src.data,(BITMAPINFO *)&amp;bi,DIB_RGB_COLORS); //copy from hwindowCompatibleDC to hbwindow DeleteObject(hbwindow); DeleteDC(hwindowCompatibleDC); ReleaseDC(hwnd, hwindowDC); return src; } </code></pre> <p>The function with the screenshot is not my own work, i got it from <a href="https://stackoverflow.com/questions/14148758/how-to-capture-the-desktop-in-opencv-ie-turn-a-bitmap-into-a-mat">here</a></p> <p>Any Ideas what to do?</p> <p>Thanks for help, best regards!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload