I want to use OpenCV to process my desktop as if it were a video stream.
I am familiar with OpenCV.
I am not familiar with the Windows API.
I realize there are other ways to capture the screen, but for the purposes of my question, I need it to be done using OpenCV.
Here is my (super naive) code:
HWND hDesktopWnd;
HDC hDesktopDC;
hDesktopWnd=GetDesktopWindow();
hDesktopDC=GetDC(hDesktopWnd);
// get the height and width of the screen
int height = GetSystemMetrics(SM_CYVIRTUALSCREEN);
int width = GetSystemMetrics(SM_CXVIRTUALSCREEN);
// create a bitmap
HBITMAP hbDesktop = CreateCompatibleBitmap( hDesktopDC, width, height);
Mat src(height,width,CV_8UC4);
src.data = (uchar*)hbDesktop;
imshow("output",src); //fails :(
There are similar questions on StackOverflow, but they are either for the old-style OpenCV, or for Android operating system.
I'm on windows 7 64x
Opencv 2.4.3
Thanks anyone who can answer this question.
A better way to do it is do it while allocating memory to the pixels only once. so the only copy done here is the one that made by BitBlt
Note that no error handling done here to make it simple to understand but you have to do error handling in your code!
Hope this helps
After MUCH trial and error, I managed to write a function to do it. here it is for anyone else who might want it: