0

I create a MAT from a given hWnd

cv::Mat hwnd2mat(HWND hwnd) {

HDC hwindowDC, hwindowCompatibleDC;

int height, width, srcheight, srcwidth;
HBITMAP hbwindow;
cv::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, &windowsize);

srcheight = windowsize.bottom;
srcwidth = windowsize.right;
height = windowsize.bottom;  
width = windowsize.right;

src.create(height, width, CV_8UC4);

// create a bitmap
hbwindow = CreateCompatibleBitmap(hwindowDC, width, height);
bi.biSize = sizeof(BITMAPINFOHEADER);    //http://msdn.microsoft.com/en-us/library/windows/window/dd183402%28v=vs.85%29.aspx
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 *)&bi, DIB_RGB_COLORS);  //copy from hwindowCompatibleDC to hbwindow

                                                                                                   // avoid memory leak
DeleteObject(hbwindow); DeleteDC(hwindowCompatibleDC); ReleaseDC(hwnd, hwindowDC);

return src;
}

And I try to load the images to run the template matching algorithm. However, when I try to imgdecode I get img = NULL. If I save the file to a png and read it afterwards, it's working.

I even tried to use the different flags to read the image.

cv::Mat resMat = hwnd2mat(blueStackshWnd);
cv::imwrite("foo.png", resMat);
img = cv::imdecode(resMat, cv::IMREAD_GRAYSCALE);
//img = cv::imread("fuck.png", cv::IMREAD_COLOR);
templ = cv::imread("images\\giant.png", cv::IMREAD_GRAYSCALE);

Here is a screenshot while debugging

EDIT:

When assigning the ret value of hwnd2mat to img I get the following error

error: (-215) (depth == 0 || depth == 5) && type == _templ.type() && _img.dims() <= 2 in function cv... ...} ...}   const cv::Exception &
0x45
  • 779
  • 3
  • 7
  • 26
  • @zindarod This is just stuff I tried, assign the return of `hwnd2mat` to `img` wouldn't work either. I get an exception. I will edit my question – 0x45 Jul 09 '18 at 19:21
  • @zindarod a gray screen... but why is it working if I save it?! – 0x45 Jul 09 '18 at 19:26
  • I assume u meant `COLOR_BGR2RGBA`, still all grey (empty image) `COLOR_RGB2BGRA` same result – 0x45 Jul 09 '18 at 19:34
  • @zindarod it won't compile. `COLOR_ARGB2BGRA` doesn't exist – 0x45 Jul 09 '18 at 19:36
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/174676/discussion-between-zindarod-and-0x45). – zindarod Jul 09 '18 at 19:38

0 Answers0