Ask Your Question

How to make an array from a screen capture for avi for c++

asked 2019-11-10 08:48:37 -0500

mblackbourne gravatar image

updated 2019-11-10 16:41:47 -0500

I have used the info in this link. To build the base of the screen capture tool.

I have the imshow working fine along with imwrite. I added the codec for the video write. The understanding is: videowriter processes frames. The src is returned as the "frame". I tried using video.write(InputArray(src)); but that also didn't work. Using python numpy builds the array frame that write reads from. Would it be possible to add src to the Video Capture?

int main(int argc, char** argv)
std::cout << "Starting Script" << std::endl;
HWND hwndDesktop = GetDesktopWindow(); //FindWindowEx();
namedWindow("output", WINDOW_NORMAL);
int key = 0;
Mat src = hwnd2mat(hwndDesktop); // src =frame
VideoWriter video("out.avi", cv::VideoWriter::fourcc('X', 'V', 'I', 'D'), 10, Size(src.size()), true); // ctlHeight, ctlWidth
int count = 0;
while (key != 27)

    // InputArray img = src;
    //imwrite("outer.jpg", src);
    imshow("output", src);
    key = waitKey(60); // you can change wait time
return 0;

The above is how I am trying to make the capture into a video.

In python to create the array that is passed to the VideoWriter::Write(); looks like the following:

    img = ImageGrab.grab(bbox=(0, 0, width, height))#testSpec: bbox=(0, 0, 800, 1000)
    img_np = np.array(img)

I have tried using the InputArray() included in opencv so the write looks more like:


Which should be similar to


Either way, the above imshow() works perfectly. The Imwrite() works perfectly. but the AVI will not write anything. It creates the file but it is empty.

I also get the error of: error: (-5:Bad argument) CAP_IMAGES: can't find starting number (in the name of file): out.avi in function 'cv::icvExtractPattern

Windows10 vcpkg install 4.1.1

edit retag flag offensive close merge delete



???? VideoWriter writes Mats. It does not return objects or use parameter src or use class "frame", whatever that is.

mvuori gravatar imagemvuori ( 2019-11-10 09:38:17 -0500 )edit

Look at the link I posted. That's the "src" I am talking about. I'm new to opencv.

mblackbourne gravatar imagemblackbourne ( 2019-11-10 09:54:43 -0500 )edit

@mblackbourne can you ask a real question here, not link to another ?

can we see YOUR code there ?

also, os, opencv version, how did you install it ?

berak gravatar imageberak ( 2019-11-10 10:21:29 -0500 )edit

@berak edited question for clarity.

mblackbourne gravatar imagemblackbourne ( 2019-11-10 10:44:50 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2019-11-10 15:46:05 -0500

the problem on your code is here


src is 4 channel (BGRA) you need to convert it to BGR

you can find here the sample source i recently updated.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2019-11-10 08:48:37 -0500

Seen: 37 times

Last updated: Nov 10