It seems that your camera was not initialized properly. The following code works for me.
using namespace cv;
[...]
VideoCapture capture(0);
capture.set(CV_CAP_PROP_FRAME_WIDTH, width);
capture.set(CV_CAP_PROP_FRAME_HEIGHT, height);
I experimented with it a bit and found the following issues:
capture.set
returns 0 if capture
was not initialized.
capture.set
returns 0 if camera is busy (another process using it).
It is not guaranteed that calling VideoCapture::set
will change camera resolution to your desired resolution. For example, with my Logitech HD Pro Webcam C290, setting resolution to 640x480 and 1920x1080 works. But when I try 1024x768, VideoCapture::set
returns true, but actual resolution is set to 960x720. So, check the actual resolution after reading a frame.