1

Continuing in previous post from here OPENCV Destop Capture and this hwnd2mat function, a method to capture desktop as source in opencv is create a bitmap from the screen image with this hwnd2mat function. done. this function is already create a bitmap from the screen image.but it gives weird effect like a trailing inside the video, i just want a normal video source more likely like this one . i've already try to find what cause it, but i don't know anymore.

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include <Windows.h>
#include <iostream>

using namespace std;
using namespace cv;

Mat hwnd2mat(HWND hwnd)
{
    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, &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_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;
}

int main(int argc, char **argv)
{
    HWND hwndDesktop = GetDesktopWindow();
    namedWindow("output", CV_WINDOW_NORMAL);
    int key = 0;

    while (key != 30 )
    {
        Mat src = hwnd2mat(hwndDesktop);        
        // you can do some image processing here
        imshow("output", src);
        key = waitKey(30); // you can change wait time
    }
    ReleaseCapture();
    destroyAllWindows();
    return 0;
}
Community
  • 1
  • 1
mas bro
  • 312
  • 1
  • 12
  • the weird effect is like this : https://www.youtube.com/watch?v=RQePoFEUS3A&feature=youtu.be , can you guys tell me what happen – mas bro Apr 19 '16 at 08:00
  • 1
    so you don't want to capture the whole screen but only one window? Obviously if you capture the whole screen, you do capture the output image again, leading to that infinite-mirror-effect. Instead of `GetDesktopWindow` you should access/locate the handle of the window you want to capture. – Micka Apr 19 '16 at 10:36
  • @Micka ok, i get the idea, i should locate the handle of the window that i want to capture, but what is it instead of `GetDesktopWindow` ? – mas bro Apr 19 '16 at 13:04
  • 1
    which window do you want to capture? If you know the name, use `FindWindow` https://msdn.microsoft.com/de-de/library/windows/desktop/ms633499%28v=vs.85%29.aspx or maybe you can use `GetActiveWindow` if you can manage to make the desired window the active one. If you want to capture the whole screen instead, maybe you can hide your output window before calling `GetDesktopWindow` and display it again afterwards. – Micka Apr 19 '16 at 13:09
  • here's a way to hide your window in delphi: http://stackoverflow.com/questions/22430706/delphi-7-screenshot-without-capturing-form-windows-8-dwm-exe maybe you can adapt that. – Micka Apr 19 '16 at 13:10
  • @Micka i think i'm gonna go with the "which window i want to capture" , lets say i want to capture first tab www.youtube.com from google chrome, so how do i write it in : `HWND WINAPI FindWindow( _In_opt_ LPCTSTR lpClassName, _In_opt_ LPCTSTR lpWindowName );` – mas bro Apr 19 '16 at 13:56
  • I'm no expert on windows API, but probably you can find more information in questions/answers like this: http://stackoverflow.com/questions/19705797/find-the-window-handle-for-a-chrome-browser and general windows API tutorials on how to use FindWindow and/or how to use windows handles. – Micka Apr 19 '16 at 13:59
  • 1
    @Micka Finally! until now 2:31 AM, i understand what is all about, you are right micka!, thank you man ! you are my hero.! i get the result what i want! not only that, i understand now the logic with window handles !! :) – mas bro Apr 19 '16 at 19:32
  • that's great to hear! – Micka Apr 19 '16 at 19:36

1 Answers1

1

Using GetDesktopWindow() will make infinite-mirror-effect, instead that, using what window you want capture, and me, i'm using FindWindowEx(), and use Spy++ to find what window you want to capture, that should works. but of course it will go to the next bug :).

mas bro
  • 312
  • 1
  • 12