webcam

i was wondering how to get access to the web cam and display it in a window

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <Windows.h>

using namespace std;

int main()
{
	int return_value;
	
	return_value = MessageBoxA(NULL, "Would yo like to start webcan feed", "WebCam",
		MB_ICONEXCLAMATION | MB_YESNO);
	
	if (return_value == 6)//yes
	{
		cout << "yes" << endl;//test for which button pressed
		//open a window with webcam feed
	}
	else if (return_value == 7)//no
	{
		cout << "no" << endl;//test for which button pressed
	}
	return 0;
}
I have OpenCV set up but i still cant find out how to access and display the camera.
I have never used opencv before, but I did find some code that opens a camera.
http://opencv-srf.blogspot.com/2011/09/capturing-images-videos.html
Here is the copy paste that the website explains.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include "opencv2/highgui/highgui.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char* argv[])
{
    VideoCapture cap(0); // open the video camera no. 0

    if (!cap.isOpened())  // if not success, exit program
    {
        cout << "Cannot open the video cam" << endl;
        return -1;
    }

   double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
   double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video

    cout << "Frame size : " << dWidth << " x " << dHeight << endl;

    namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"

    while (1)
    {
        Mat frame;

        bool bSuccess = cap.read(frame); // read a new frame from video

         if (!bSuccess) //if not success, break loop
        {
             cout << "Cannot read a frame from video stream" << endl;
             break;
        }

        imshow("MyVideo", frame); //show the frame in "MyVideo" window

        if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
       {
            cout << "esc key is pressed by user" << endl;
            break; 
       }
    }
    return 0;

}
Last edited on
Topic archived. No new replies allowed.