将示例图像与图像数据库进行比较时出现意外结果 - Opencv c++



我正在研究图像匹配程序,它从红外摄像机捕获图像并将其存储在一个目录中。映像数据库位于不同的目录中。我在执行时得到意外的结果。我不知何故意识到这是因为我弄乱了不同的目录和一些基本的东西。有什么帮助可以解决错误吗?下面是一段代码。

VideoCapture cap(0);    
Mat frame;
Mat src, dst, tmp,img_3,img_4;
filename3 = (char *)malloc(sizeof(char));
printf("n-------------------------------------------------n");
printf("nOne to Many Image matching with SURF Algorithm.n");
printf("n-------------------------------------------------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");
scanf("%s",filename3);
//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 ("%sn", 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( !img_3.data)
                {
                    std::cout<< " --(!) Invalid filename or file not found! " << std::endl;
                    return -1;
                }

正因为如此,我总是得到--(!无效的文件名或找不到文件消息。有没有办法比较目录中的一个图像和另一个目录中的另一个图像?我希望我问的不会令人困惑。请帮忙。

在Opencv上工作 - 在Ubuntu 12.04 LTS上C++

您得到未定义的行为,因为您只为以下行中的filename3分配了 1 个字符的空间:

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

所以在下一行...

scanf("%s",filename3);

任何大于 1 个字符的文件名都将调用未定义的行为。

在我看来,没有必要malloc.您可以像这样分配足够大的固定大小filename3

char filename3[256];

最新更新