Ask Your Question
0

Camera is not opening and getting frames

asked 2014-05-01 03:45:29 -0600

Vivek Goel gravatar image

updated 2014-05-01 04:27:07 -0600

FLY gravatar image
#include "stdafx.h"
#include <stdio.h>
#include <highgui.h>
#include <cv.h>
#include <cxcore.h>

void main(){ //Sobel edge detection for a video

    CvCapture* capture = cvCreateCameraCapture(0);  //The video is loaded into a pointer of type CvCapture
    IplImage* frame; IplImage*gscale_res; IplImage* edgeframe; //Declaration of structure variables to store frames
    char *win = "video"; char *winEdge = "edges"; //Declaration of character pointers to store window names
    cvNamedWindow(win, CV_WINDOW_AUTOSIZE); //Window is created for original image
    cvNamedWindow(winEdge, CV_WINDOW_AUTOSIZE); //Window is created for resultant image

    while (1)
    {
        frame = cvQueryFrame(capture); //Image under consideration is captured as a shot of the video
        if (!frame)
        {
            printf("cam not found");
            break;
        }
        gscale_res = cvCreateImage(cvSize(frame->width, frame->height), IPL_DEPTH_8U, 1);//Creation of the image variable to store both the grayscale intermediate and final result
        edgeframe = cvCreateImage(cvSize(frame->width, frame->height), IPL_DEPTH_16S, 1); //The 16-bit signed image for the result of the Sobel Edge detection
        cvCvtColor(frame, gscale_res, CV_BGR2GRAY); //Gray-scale intermediate created
        cvSobel(gscale_res, edgeframe, 1, 0, 3);// Sobel edge detection function is applied on the grayscale image and the result is put in the other variable
        cvConvertScaleAbs(edgeframe, gscale_res, 1, 0); //The 16-bit signed image is converted to an 8-bit unsigned version so it can be displayed
        cvShowImage(win, frame); //Original image is shown
        cvShowImage(winEdge, gscale_res); //Resultant image with edges detected, is shown
        char c = cvWaitKey(33); //This is to account for the frame speed of the loaded video
        cvReleaseImage(&gscale_res);
        cvReleaseImage(&edgeframe);
        if (c == 27) //If the character of code 27 or the ESCAPE character is entered, the loop will break
            break;
    }

    cvReleaseCapture(&capture); //Video is released from memory
    cvReleaseImage(&frame);  //Images released from memory
    cvDestroyWindow(win);
    cvDestroyWindow(winEdge); //Windows closed
}

i have tried different arguments, cvCreateCameraCapture,cvCaptureFromCAM but still not working

edit retag flag offensive close merge delete

Comments

1

oaah stevenputtemans is tag too now :)

FLY gravatar imageFLY ( 2014-05-01 04:00:08 -0600 )edit

@what error your getting and what happens when you run the program ?

FLY gravatar imageFLY ( 2014-05-01 04:58:03 -0600 )edit

it is just exiting with code 0. the console opens and close in 1 second in many programs.

Vivek Goel gravatar imageVivek Goel ( 2014-05-03 12:55:53 -0600 )edit

use WaitKey function , waitkey(0);

FLY gravatar imageFLY ( 2014-05-03 15:11:01 -0600 )edit

2 answers

Sort by ยป oldest newest most voted
1

answered 2014-05-01 04:53:56 -0600

FLY gravatar image

updated 2014-05-01 04:56:52 -0600

Give a try to c++ interface and put a check on it

 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;
    }

and for C interface , the complete method if playing video is

int main()
{
    CvCapture* capture = cvCreateFileCapture("sample.avi");

    IplImage* frame = NULL;

    if(!capture)
    {
        printf("Video Not Opened\n");
        return -1;
    }

    int width = (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_WIDTH);
    int height = (int)cvGetCaptureProperty(capture,CV_CAP_PROP_FRAME_HEIGHT);
    double fps = cvGetCaptureProperty(capture, CV_CAP_PROP_FPS);
    int frame_count = (int)cvGetCaptureProperty(capture,  CV_CAP_PROP_FRAME_COUNT);

    printf("Video Size = %d x %d\n",width,height);
    printf("FPS = %f\nTotal Frames = %d\n",fps,frame_count);

    while(1)
    {
        frame = cvQueryFrame(capture);

        if(!frame)
        {
            printf("Capture Finished\n");
            break;
        }

        cvShowImage("video",frame);
        cvWaitKey(10);
    }

    cvReleaseCapture(&capture);
    return 0;
}

You should take these tutorials for video and for Sobel

edit flag offensive delete link more
-1

answered 2014-05-01 19:53:07 -0600

yash101 gravatar image

updated 2014-05-01 19:54:01 -0600

As said before, try to use the newer C++ interface. With the new interface, grabbing the image from the camera is easier than anything you could think of. Here's some sample code:

#include <opencv2/highgui/highgui.hpp>
int main()
{
    CvCapture *cam1 = cv::cvCaptureFromCAM(0);
    while(true)
    {
        if(cam1)
        {
            cv::Mat img = cv::cvQueryFrame(cam1);
            //put your code here. img contains the picture from the camera.
        }
    }
}

I have never encountered any problem like this. The only time I have a camera failure is when my code is so cluttered that one small thing spoils everything. Try switching to Mat and garbage collection, coding and much more will be simpler!

Good luck! :)

edit flag offensive delete link more

Comments

1

you recommend the c++ api, but use the c-one ?

berak gravatar imageberak ( 2014-05-02 03:16:42 -0600 )edit

Question Tools

Stats

Asked: 2014-05-01 03:45:29 -0600

Seen: 887 times

Last updated: May 01 '14