【问题标题】:Unexpected result when Comparing sample image with database of images - Opencv c++将样本图像与图像数据库进行比较时出现意外结果 - Opencv c++
【发布时间】:2014-04-04 00:22:46
【问题描述】:

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

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 ("%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( !img_3.data)
                {
                    std::cout<< " --(!) Invalid filename or file not found! " << std::endl;
                    return -1;
                }

因此,我总是收到 --(!) Invalid filename or file not found 消息。有没有办法可以比较一个目录中的一个图像和另一个目录中的另一个图像?我希望我所问的不会令人困惑。请帮忙。

在 Ubuntu 12.04 LTS 上使用 Opencv - C++

【问题讨论】:

    标签: c++ image opencv directory


    【解决方案1】:

    您正在获得未定义的行为,因为您在以下行中为 filename3 仅分配 1 个字符的空间:

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

    所以在下面一行...

    scanf("%s",filename3);
    

    任何大于 1 个字符的文件名都会引发未定义的行为。

    在我看来,不需要malloc。您可以像这样分配足够大的固定大小的filename3:

    char filename3[256];
    

    【讨论】:

    • Char filename3[256] 也显示相同的结果。我觉得比较的目录有问题。尽管我将 filename3 保存在不同的目录中,但它会在安装 opencv 的默认目录中搜索该文件,因此出现该错误。但我不知道如何纠正它。
    猜你喜欢
    • 1970-01-01
    • 2011-11-04
    • 2013-04-11
    • 2021-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-20
    相关资源
    最近更新 更多