Ask Your Question

Unexpected result when comparing sample image with database of images opencv c++

asked 2014-04-06 11:31:04 -0500

techlearner gravatar image

I am working on Image matching program, it captures image from IR camera stores it in one directory. A database of images exist in a different directory. I get an unexpected result while executing. I somehow realize that it's because I'm messing with different directories and something basic. Any help in getting around the mistake? Here's a snippet of the code.


VideoCapture cap(0);    
Mat frame;
Mat src, dst, tmp,img_3,img_4;

filename3 = (char *)malloc(sizeof(char));
printf("\nOne to Many Image matching with SURF Algorithm.\n");

//Get the Vein image from Webcam that needs to be compared with the database.
// The below function gets frame and saves it in /home/srikbaba/opencv/veins
veincnt = captureVein(veincnt, cap);
if(veincnt == -1)
     cout << "A problem has occured" << endl;
printf("\nPlease enter the filename of saved image with the extension.\n");
//Scan the directory for images.
DIR *dir;
struct dirent *ent;
clock_t tstart1 = clock(); //start clock function, do something!!!!
if ((dir = opendir ("/home/srikbaba/images")) != NULL) 
    /* print all the files and directories within directory */
    while ((ent = readdir (dir)) != NULL) 
        if(ent->d_type!= DT_DIR)
            //Print the images
            printf ("%s\n", ent->d_name);
            //Store the Filename
            img_db = ent->d_name;
            //Open each file name as Mat identifier and loop the process
            img_3 = imread (filename3, IMREAD_GRAYSCALE);

//Filename3 is saved in /home/srikbaba/opencv/veins --> different directory
// I feel this is the problem
                img_4 = imread (img_db, IMREAD_GRAYSCALE);
                if( !
                    std::cout<< " --(!) Invalid filename or file not found! " << std::endl;
                    return -1;

Because of this I always get --(!) Invalid filename or file not found message. Is there a way I can compare one image from a directory and another image from a different directory? I hope what I asked is not confusing. Kindly help.

Working on Opencv - C++ on Ubuntu 12.04 LTS

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2014-04-06 19:52:30 -0500

Well, you have declared filename3 as follows:

filename3 = (char *)malloc(sizeof(char));

What you are doing there is to declare a pointer to a single char (and only one char). I guess that what you want is to allocate memory for a certain number of characters, not only one. Try something like this:

filename3 = (char *)malloc(sizeof(char)*APPROPIRATE_SIZE_FOR_FILENAME3);

Where APPROPRIATE_SIZE_FOR_FILENAME3 may be, for example, 1024. By doing this your file names will be allowed to be 1023 characters long.

Also, you didn't include the definition of img_db, but you might have the same problem there.

I hope this helps.

edit flag offensive delete link more


Thank you for the reply. I corrected those and I still get the same result. The problem is, the images are not being searched in the directory specified by dir. Instead it searches the image in the default directory where OpenCV is installed.

techlearner gravatar imagetechlearner ( 2014-04-07 19:06:29 -0500 )edit

You probably need to build the image_db path as follows (this is pseudo code): image_db = "/home/srikbaba/images/"+ent->d_name

Martin Peris gravatar imageMartin Peris ( 2014-04-09 02:45:23 -0500 )edit

@martin :Thank you so much, that did the trick!

techlearner gravatar imagetechlearner ( 2014-04-09 19:20:46 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2014-04-06 11:31:04 -0500

Seen: 242 times

Last updated: Apr 06 '14