undeclared indentifier opencv cvCaptureFromCAM and cvQueryFrame

asked 2015-02-15 14:48:52 -0500

kodasbatt gravatar image

updated 2015-02-16 02:31:32 -0500

berak gravatar image

I'm pretty new to openCv and C++ in general.

I cannot undestand why I get a undeclared identifier on the cvCaptureFromCAM and from cvQueryFrame

Do you get this type of error when the function is not included into a library? If so, aren't these two functions included into those i include?

My code is

#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace cv;

 /** Function Headers */
 void detectAndDisplay( Mat frame );

  /** Global variables */
  String face_cascade_name = "haarcascade_frontalface_alt.xml";
  String eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
  CascadeClassifier face_cascade;
  CascadeClassifier eyes_cascade;
  string window_name = "Capture - Face detection";
  RNG rng(12345);

  /** @function main */
  int main( int argc, const char** argv )
    CvCapture* capture;
    Mat frame;

    //-- 1. Load the cascades
    if( !face_cascade.load( face_cascade_name ) ){ printf("--(!)Error      loading\n"); return -1; };    //carica nome face_cascade &      eyes_cascade da file xml in CascadeClassifier
    if( !eyes_cascade.load( eyes_cascade_name ) ){ printf("--(!)Error      loading\n"); return -1; };

    //-- 2. Read the video stream
    capture = cvCaptureFromCAM(-1); //legge da camera
    if( capture )
      while( true )
    frame = cvQueryFrame( capture ); //assegna frame

    //-- 3. Apply the classifier to the frame
        if( !frame.empty() )
        { detectAndDisplay( frame ); }
        { printf(" --(!) No captured frame -- Break!"); break; }

        int c = waitKey(10);
        if( (char)c == 'c' ) { break; }
    return 0;

 /** @function detectAndDisplay */
 void detectAndDisplay( Mat frame ) //Mat is the struct that divides      image in 24x24
     //able to store a single instance of  primitive data type is Vec.      Multiple instances Vec can be stored in std::vector, Mat
   std::vector<Rect> faces; //creates rectangular conteiner of info of      faces
   Mat frame_gray; //creates mat container of std vec

   cvtColor( frame, frame_gray, CV_BGR2GRAY ); //void           cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0 ) Converts an image from one color space to another
  equalizeHist( frame_gray, frame_gray );   
 face_cascade.detectMultiScale( frame_gray, faces, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30) ); 
 for( size_t i = 0; i < faces.size(); i++ )
    Point center( faces[i].x + faces[i].width*0.5, faces[i].y + faces[i].height*0.5 ); //define center for each investigated area
   ellipse( frame, center, Size( faces[i].width*0.5, faces[i].height*0.5), 0, 0, 360, Scalar( 255, 0, 255 ), 4, 8, 0 );

   Mat faceROI = frame_gray( faces[i] );
   std::vector<Rect> eyes;

   //-- In each face, detect eyes
   eyes_cascade.detectMultiScale( faceROI, eyes, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30) );

   for( size_t j = 0; j < eyes.size(); j++ )
       Point center( faces[i].x + eyes[j].x + eyes[j].width*0.5, faces[i].y + eyes[j].y + eyes[j].height*0.5 );
   int radius = cvRound( (eyes[j].width + eyes[j].height)*0.25 );
   circle( frame, center, radius, Scalar( 255, 0, 0 ), 4, 8, 0 );
  //-- Show what you got
   imshow( window_name, frame );

please someone help

edit retag flag offensive close merge delete



those identifiers are remainders of opencv's long gone c-api, and should just be replaced (this is probably a bug in some tuturial)

instead, use:

//CvCapture* capture;
VideoCapture capture;

//capture = cvCaptureFromCAM(-1); //legge da camera

//if( capture )
if (capture.isOpened())

//frame = cvQueryFrame( capture ); //assegna frame
berak gravatar imageberak ( 2015-02-16 01:45:26 -0500 )edit

thank you so much for your reply. you're right, it's not givin me that error no more.

although... it is now saying that CV_BGR2GRAY , CV_HAAR_SCALE_IMAGE and CV_HAAR_SCALE_IMAGE use undeclared identifiers. I know these aren't declared but aren't they contained in the included libraries? also could you explain you very correct previous solution please?

kodasbatt gravatar imagekodasbatt ( 2015-02-16 10:31:17 -0500 )edit

any chance, that you're using opencv3.0 here ?

in this case, the constants were changed, see here and here

(that would also explain, why the compiler did not find the c-api functions. 3.0 has a much clearer watershed between the old and the new functionality)

berak gravatar imageberak ( 2015-02-16 10:54:52 -0500 )edit

hello berak,you were correct! i managed to compile it finally. THANK YOU the struggle though is not over as i get the following messages in the terminal Compilation OK. Creating machine code... Code generating 'detectAndDisplay()' Linking... Linking OK. Finished compiling (0.21+0.22=0.42 sec) and linking (0.59 sec). 11:21:19 Build Finished (took 1s.822ms) <terminanted> --(!)Error loading

have you got any clue of what this mught be? thank you!

kodasbatt gravatar imagekodasbatt ( 2015-02-20 05:23:37 -0500 )edit

now have a sharp look at your code, and see, where this error is generated. (hint-it's there literary)

berak gravatar imageberak ( 2015-02-20 05:29:08 -0500 )edit

lol.. PLEASE tell me because i'm litteraly going crazy with this code and can't look at it no more

kodasbatt gravatar imagekodasbatt ( 2015-02-20 05:48:24 -0500 )edit

berak please help

kodasbatt gravatar imagekodasbatt ( 2015-02-20 11:41:56 -0500 )edit

oh, apologies, forgot you.

it could not load the cascade file. path wrong. try an absolute one,/opencv/data/blah/mycascade.xml , c:/opencv/you/know/what/i/mean.xml

berak gravatar imageberak ( 2015-02-20 11:54:49 -0500 )edit

thanks once again. this solved the problem. and sorry for yesterday(i was exausted :D) -the error was pretty obvious as you said..

I'm now having problems again with the videoCapture though >.< in fact doing

VideoCapture capture;
if (capture.isOpened())

does not enter the body of the if cycle.

kodasbatt gravatar imagekodasbatt ( 2015-02-21 05:52:32 -0500 )edit

i managed to: problem was .open(-1) i've set that to.open(0) and it works fine!

thank you berak for all of your help

kodasbatt gravatar imagekodasbatt ( 2015-02-21 09:04:31 -0500 )edit