Click here to Skip to main content
15,567,221 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everybody, I'm trying to open webcam, catch a still image and save it into a specific directory.
the webcam opens and the image is saved, but I always get a black image.
I'm using c++ (cv.h and highgui.h)
Note that the webcam is working well using another programs, and also I've tried using another webcam, but got the same problem.
anyone can help please?!

What I have tried:

C++
#include "cv.h"
#include <highgui.h>
#include "stdio.h"
#include <iostream> 
#include <time.h>


using  namespace  std;
void sleep(clock_t delayms);


int main(int argc, char* argv[])
 {

	int logincounter = 3;

	CvCapture* cptMyCam;
	cptMyCam = cvCreateCameraCapture(-1);//get the default cam

	//initializing
	IplImage* frame = cvCreateImage(cvSize(640, 480), IPL_DEPTH_8U, 1);

	if (cptMyCam == NULL)
	{
		exit(0);
	}

	cvNamedWindow("WebCam", 0);

	int counter = 1;
	while (-1)
	{
		if (cvGrabFrame(cptMyCam) == 0)//grabe a frame from the cam
		{
			cout << "Could not Capture";
			break;
		}
		counter--;

		frame = cvRetrieveFrame(cptMyCam);

		cvShowImage("WebCam", frame);//display the grabbed fram	
	

		while (logincounter>0)
		{

			if (counter == 0)
			{
				counter = 4;
				sleep(4000);
				cvSaveImage("C:\\FromCam\\image.jpeg", frame);//saving the captured image
				cvDestroyWindow("WebCam");
           }
}
}
}
Posted
Updated 29-Jan-21 23:39pm
v2

Maybe you can use this: Yet another Web Camera control[^]
 
Share this answer
 
Black picture is a bit unclear, my guess is that the device isnt ready yet so wait. Another issue maybe that your program hasnt the right to write that file.

Check the image buffer of incoming picture to find out whether you get data. Make a loop for some seconds.
 
Share this answer
 
Comments
Abbas Harb 30-Jan-21 5:16am    
I tried previously to wait before
frame = cvRetrieveFrame(cptMyCam);
but didn't work. where I have to wait?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900