First time here? Check out the FAQ!

Ask Your Question
4

How can I get frames from my webcam ?

asked Jun 1 '12

AlexanderShishkov gravatar image

updated Jul 11 '15

How can I do that in C++ and Python?

Preview: (hide)

3 answers

Sort by » oldest newest most voted
6

answered Jun 1 '12

AlexanderShishkov gravatar image

updated Jan 21 '17

C++:

#include "opencv2/opencv.hpp"
using namespace cv;
int main(int argc, char** argv)
{
    VideoCapture cap;
    // open the default camera, use something different from 0 otherwise;
    // Check VideoCapture documentation.
    if(!cap.open(0))
        return 0;
    for(;;)
    {
          Mat frame;
          cap >> frame;
          if( frame.empty() ) break; // end of video stream
          imshow("this is you, smile! :)", frame);
          if( waitKey(10) == 27 ) break; // stop capturing by pressing ESC 
    }
    // the camera will be closed automatically upon exit
    // cap.close();
    return 0;
}
Preview: (hide)

Comments

I tried this and frame is always empty.

diegoaguilar gravatar imagediegoaguilar (Sep 5 '13)edit

This works for me. I am using openCV 3.1.0 with VS2015.

sofia123 gravatar imagesofia123 (Jan 7 '16)edit

can you explain cap >> frame

marzooq6463 gravatar imagemarzooq6463 (Jun 18 '17)edit

This code works for me too. I am using OpenCV 3.2.0 with visual-studio-2017

cap >> frame means the captured image will be sent to the frame which will open in a new window

blink gravatar imageblink (Jul 19 '17)edit
-1

answered Mar 26 '16

openCV gravatar image

Can I get a code for the same in C language?

Preview: (hide)

Comments

Something like this:

#include "cv.h"
#include "highgui.h"
int main(int argc, char** argv)
{
CvCapture* capture=0;
IplImage* frame=0;
capture = cvCaptureFromAVI("C:\\video.avi"); 
if( !capture )
    throw "Error when reading steam_avi";

cvNamedWindow( "w", 1);

for( ; ; )
{
    frame = cvQueryFrame( capture );
    if(!frame)
        break;
    cvShowImage("w", frame);
}
cvWaitKey(0); 
cvDestroyWindow("w");
cvReleaseImage(&frame);
}
AlexanderShishkov gravatar imageAlexanderShishkov (Mar 28 '16)edit

Thank you so much !!! I will try this code.

openCV gravatar imageopenCV (Mar 28 '16)edit

I have a small doubt. Will this code work for OMAP L138 C6748 DSP processor?

openCV gravatar imageopenCV (Mar 29 '16)edit

Which version of openCV should I use for the C code mentioned above? I'm using Code Composer Studio v5.5 (CCS v5.5) as the IDE. So, what are the supporting files to be included before compiling the code. When I typed the above code in CCS, I got this error : could not open source file cv.h

openCV gravatar imageopenCV (Mar 29 '16)edit

Also, I want to access an external webcam connected to OMAP L138 C6748 DSP Processor. Not the video file stored in my PC.

openCV gravatar imageopenCV (Mar 29 '16)edit

If you want to get access to video camera, just use index of camera (0-based) instead of filename. Unfortunately I know nothing about CCS, you can try to use new headers:

#include "opencv2/opencv.hpp"
AlexanderShishkov gravatar imageAlexanderShishkov (Mar 30 '16)edit
-1

answered Jul 23 '17

ron gravatar image

thanks worked for me ...i tried so hard and for hours. Problem was at line : if( waitKey(33) >= 0 ) break; // OpenCV book quotes it like this one

but your line is life saver

"if( waitKey(10) == 27 ) break; // stop capturing by pressing ESC " worked like charm

Regards ron

OpenCV 3.2 VStudio 2017 win10

Preview: (hide)

Question Tools

Stats

Asked: Jun 1 '12

Seen: 92,892 times

Last updated: Jan 21 '17