Ask Your Question

What is the best way to open and input a camera image?

asked 2013-04-03 15:16:29 -0600

updated 2013-04-23 02:14:27 -0600

I am using opencv2.2 with windows vista. All I see is a grey image when calling the camera input. Can someone suggest me the best camera input method?

include <opencv2\core\core.hpp>
include <opencv2\highgui\highgui.hpp>
include <opencv2/opencv.hpp>
include <opencv2/objdetect/objdetect.hpp>
include <opencv2/imgproc/imgproc.hpp>
include <iostream>
include <stdio.h>
include <windows.h>

using namespace cv; using namespace std; bool fg=1; bool cg=1;

int threshold_value = 70; int threshold_type = 0;; int const max_value = 255; int const max_type = 4; int const max_BINARY_value = 255; char* window_name = "Threshold Demo";

int main() { if(1){

//VideoCapture capture("2013.wmv"); 
//"2013.wmv" open the default camera (0)
//  VideoCapture  set(CV_CAP_PROP_CONVERT_RGB);

VideoCapture capture(0); VideoCapture set(CV_CAP_PROP_CONVERT_RGB);


if(!capture.isOpened()) // check if we succeeded
edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2013-04-23 02:19:29 -0600

Basically what you are doing wrong looking at the code above is opening your capture but not reading any frame from it. Try the following code in stead of yours.

Mat frame;
VideoCapture capture(0);
while(true){ //infinite loop
   cap >> frame; //retrieve a single frame
   imshow("window", frame);
   int key = waitKey(0); //wait until key is pressed and give window time to call paint functionality
   if (key == 30){
       break; //if pressedkey is ESC, then exit the loop
edit flag offensive delete link more

answered 2013-04-04 02:32:41 -0600

Your answer :

The VideoCapture functionality does all you want, by reading directly frames from your webcam :)

edit flag offensive delete link more


Tried that already. Camera opens and lights up, but returns 0 size and fps.

Bob Root gravatar imageBob Root ( 2013-04-04 12:23:34 -0600 )edit

If camera opens, then connection is created. If you grab a frame, then what does the grab function returns?

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-04 15:26:49 -0600 )edit

It returns 1. Also tried capture>>frame, capture.grab & retrieve(frame). All return 1 but still no imshow('name',frame) other than grey. Also retuns zero for width and color!!! Thanks for your help. Maybe I should do straight c++?

Bob Root gravatar imageBob Root ( 2013-04-04 17:24:01 -0600 )edit

Probably you are mixing the C and C++ style interface? Place your code inside original message and I will have a look at it. I am guessing you are screwing up the pointers in C :)

StevenPuttemans gravatar imageStevenPuttemans ( 2013-04-05 00:52:19 -0600 )edit

Not using straight c that I know of. Will trim code down and send today. Thanks again.

Bob Root gravatar imageBob Root ( 2013-04-05 13:31:18 -0600 )edit

Question Tools


Asked: 2013-04-03 15:16:29 -0600

Seen: 1,617 times

Last updated: Apr 23 '13