Ask Your Question
0

Problem with IplImage pointer

asked 2013-12-23 02:57:37 -0600

ampersd gravatar image

updated 2013-12-23 03:08:10 -0600

berak gravatar image

Hi everybody! I start learning OpenCV. Now using Windows 8.1 + VS2013 + opencv246. I have a problem with pointer "frame":

  CvCapture *capture = cvCreateCameraCapture(0);    
  if (capture == NULL)          
    return 0;
  IplImage *frame = NULL; 
  IplImage *sframe = NULL;

  CvSize size = cvSize(         
     cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH) / 2,  
  cvGetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT) / 2       
  );

  frame = cvQueryFrame(capture); //after this line "frame" remains NULL!    
  sframe = cvCreateImage(size, frame->depth, frame->nChannels); //here we've got exception

Visual Studio throws exception:

Unhandled exception at 0x0029564C in opencv_threewindows.exe: 0xC0000005: 
Access violation reading address 0x00000008.

Why "frame" doesn't initialize? If we will delete these lines, and continue program execution:

cvNamedWindow("camera", CV_WINDOW_AUTOSIZE);
while (1)
    {
        frame = cvQueryFrame(capture);
                cvShowImage("camera", frame);
        }

All works fine... I can't understand why this occurs. Appreciate any help.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2013-12-23 03:12:10 -0600

berak gravatar image

updated 2013-12-23 04:31:41 -0600

since you're just starting with opencv, try to avoid the old c-api, support for that is fading away quickly. try to use proper c++ for this, avoid IplImages and cv*Functions in general:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"

using namespace cv;

int main()
{
    VideoCapture cap(0);
    if ( ! cap.isOpened() )
         return;
    Size size( cap.get(CV_CAP_PROP_FRAME_WIDTH) / 2, 
               cap.get(CV_CAP_PROP_FRAME_HEIGHT) / 2 );
    Mat frame;
    while ( 1 )
    {
        if ( cap.read(frame) )
        {
            // processing here
            imshow("lalala",frame);
        }
        int k = waitKey(10);
        if ( k==27 ) // 'esc' pressed
            break;
    }
    return 0;
}
edit flag offensive delete link more

Comments

Thanks for your help. May be you are right about not bothering with C-Api. By the way, I try to run your example. Program execution's time is about 0.5 sec. (LED of camera turn on). How can I make infinite cycle? Next code throw exception: while(1) { cap.read(frame); //using "cap >> frame;" cause the same effect ... }

ampersd gravatar imageampersd ( 2013-12-23 04:21:28 -0600 )edit

so, the 1st frame from your cam is always invalid ? pretty strange, but easy to handle. see edit above

berak gravatar imageberak ( 2013-12-23 04:27:18 -0600 )edit

Thanks! Now all are ok. I didn't think about cam frames before, but may be it's the reason.

ampersd gravatar imageampersd ( 2013-12-23 04:41:17 -0600 )edit

Question Tools

Stats

Asked: 2013-12-23 02:57:37 -0600

Seen: 1,037 times

Last updated: Dec 23 '13