【问题标题】:Adding images using opencv使用opencv添加图像
【发布时间】:2010-09-14 13:24:12
【问题描述】:

我正在尝试使用 opencv 添加几个图像。我认为我的源代码应该是正确的,并且编译没有任何问题。但是当我启动程序时,for循环中会发生错误。问题是我不明白为什么会这样。

    #include <iostream>
    #include <sys/types.h>
    #include <dirent.h>
    #include <errno.h>
    #include <vector>
    #include <string>
    #include <fstream>

    #include <cv.h>
    #include <highgui.h>

using namespace std;


int get_files(string dir,
              vector<string> &files);



int main( int argc, char** argv ){

    //---------- Get the names of all *.png files in the directory 
    string directory = string(".");
    vector<string> files = vector<string>();

    get_files(directory,files);


    //---------- Erase all filenames that aren't *.png files 
    vector<string> files_format = vector<string>();

    for (unsigned int ii = 0; ii < files.size(); ii++) {

        files_format.push_back(files[ii]);

        string::iterator it;
        string format;
        files_format[ii].erase(0,files_format[ii].length()-3);

        if (files_format[ii] != "png") files.erase(files.begin() + ii);

     }

    files.erase(files.begin()); // in order to remove the ".." in the beginning

    int number_of_files = files.size();



    //---------- Create the necessary images 

    if (files.size() == 0)
        return -1;

    IplImage* img_firstimage = cvLoadImage(files[0].c_str());

    IplImage* img_totalsum = cvCreateImage(cvGetSize(img_firstimage), 8, 1 );
    cvCvtColor(img_firstimage, img_totalsum, CV_BGR2GRAY );


    //---------- Apply threshold 
    cvThreshold(img_totalsum, img_totalsum, 150, 1, 1);


    //---------- Add all the images 
    for (unsigned int ii=1; ii < files.size(); ii++){

        IplImage* img_load = cvLoadImage(files[ii].c_str());
        IplImage* img_add = cvCreateImage(cvGetSize(img_load), 8, 1 );

        cvCvtColor(img_load, img_add, CV_BGR2GRAY );

        cvThreshold(img_add, img_add, 150, 1, 1);

        //----- add the image to the total sum -----
        cvAdd(img_totalsum, img_add, img_totalsum);

        // ----- release images -----
        cvReleaseImage(&img_load);
        cvReleaseImage(&img_add);
    }


    //---------- Invert the total sum image 
    // -> dense regions are plotted in black
    //cvNot(img_totalsum, img_totalsum);

    cvNot(img_firstimage, img_firstimage);

    //---------- Show the images
    cvShowImage("Density Distribution", img_totalsum);
    cvShowImage("Negative", img_firstimage);

    cvWaitKey(0);


    // ----- release images -----
    cvReleaseImage(&img_firstimage);
    cvReleaseImage(&img_totalsum);

    return 0;
}

    int get_files(string dir,
           vector<string> &files){
DIR *dp;
struct dirent *dirp;
if((dp  = opendir(dir.c_str())) == NULL) {
       cout << "Error(" << errno << ") opening " << dir << endl;
         return errno;
     }

     while ((dirp = readdir(dp)) != NULL) {
         files.push_back(string(dirp->d_name));
     }
     closedir(dp);
     return 0;

【问题讨论】:

  • 究竟什么是“错误”?
  • 程序运行时突然停止并调用调试器。
  • 程序到底在哪里停止?调试器通常会停在错误位置。

标签: c++ opencv


【解决方案1】:

看来,您在每次循环迭代中都会释放img_add,但它只创建一次。将 cvReleaseImage(&amp;img_add); 指令移到您的 for 循环之外(正下方)。那应该可以解决它。

编辑:

好吧,看来,你已经解决了。现在还能用吗?

顺便说一句,在for 循环中为每个新加载的图像创建和释放img_add 是不必要的,并且可能会更慢,因为有多个内存分配和释放。最好在进入循环前分配,循环后释放。

【讨论】:

  • 我将源代码更改如下,但它也不起作用:我放置了 IplImage* img_add = cvCreateImage(cvGetSize(img_firstimage), 8, 1 );在循环之前并在循环之后直接释放图像。
  • @supergranu:它会在同一个位置崩溃吗?您能否将更改后的代码添加到您的问题中?
  • 是的,它在同一个位置崩溃。 - 我用另一个小程序测试了这个问题。看来一般内存分配有问题。
  • 主要问题是,很难找到一种方法让 for 循环一个接一个地读取多个图像(但为此只使用一个变量)并对其进行一些图像处理。
  • @supergranu:好的,您需要确保的另一个重要事实是,所有图像必须具有相同的格式,这意味着相同的尺寸和平面数量。否则cvCvtColor() 会崩溃。你能验证一下,所有图片都兼容吗?
【解决方案2】:

我解决了这个问题。我在工作目录中有一些其他文件,它们不是 *.png 文件,然后循环不起作用。绝对清楚该程序无法加载其他文件并使用它们...我只是不明白,为什么程序的一部分不工作应该解决这个问题...不知何故if (files_format[ii] != "png") files.erase(files.begin() + ii); 没有正常工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-19
    • 2017-11-11
    • 1970-01-01
    • 2013-04-13
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多