C++ 使用opencv添加图像

C++ 使用opencv添加图像,c++,opencv,C++,Opencv,我正在尝试使用opencv添加几个图像。我认为我的源代码应该是正确的,并且编译时没有任何问题。但是当我启动程序时,for循环中出现了一个错误。问题是我不明白为什么会这样 #include <iostream> #include <sys/types.h> #include <dirent.h> #include <errno.h> #include <vector> #include &l

我正在尝试使用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;
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int get_文件(字符串目录,
向量和文件);
int main(int argc,字符**argv){
//----------获取目录中所有*.png文件的名称
字符串目录=字符串(“.”);
向量文件=向量();
获取_文件(目录、文件);
//----------删除所有不是*.png文件的文件名
矢量文件_format=vector();
for(unsigned int ii=0;ii密集区域以黑色绘制
//cvNot(img_totalsum,img_totalsum);
cvNot(img_firstimage,img_firstimage);
//----------显示图像
cvShowImage(“密度分布”,img_总和);
cvShowImage(“负片”,img_firstimage);
cvWaitKey(0);
//----发布图片-----
cvReleaseImage(&img_firstimage);
cvReleaseImage(&img_totalsum);
返回0;
}
int get_文件(字符串目录,
向量和文件){
DIR*dp;
结构方向*dirp;
if((dp=opendir(dir.c_str())==NULL){

cout看起来,您在每个循环迭代中都会释放
img\u add
,但它只创建一次。将
cvReleaseImage(&img\u add);
指令移到
for
循环的外部(正下方)。这应该会解决它

编辑:

好吧,看来你已经修好了。现在能用了吗


顺便说一句,为每个新加载的映像创建和释放
img\u add
for
循环内部的
img
是不必要的,而且可能会更慢,因为有多个内存分配和释放。你最好在进入循环之前分配它,然后在循环之后释放它。

我解决了这个问题。我有一些经验她的文件在工作目录中,不是*.png文件,然后循环就不起作用了。很明显,程序无法加载其他文件并使用它们…我只是不明白,为什么程序中应该解决这个问题的部分不起作用…不知怎的,
if(files\u format[ii]!=“png”)文件被删除了(files.begin()+ii);
工作不正常

究竟什么构成“错误”?当运行程序时,它突然停止并调用调试器。程序到底在哪里停止?调试器通常会在错误位置停止。我更改了源代码,如下所示,但它也不工作:我放置了IplImage*img_add=cvCreateImage(cvGetSize(img_firstimage),8,1);在循环之前,并在循环之后直接释放图像。@supergranu:它是否在同一位置崩溃?能否请您将更改后的代码添加到您的问题中?是的,它在同一位置崩溃。-我用另一个小程序测试了这个问题。似乎总的来说内存分配有问题。主要问题是很难找到一种方法让for循环一个接一个地读取多个图像(但只使用一个变量)“supergranu:好的,还有一个更重要的事实,你需要确定的是,所有图像必须具有相同的格式,即相同的尺寸和平面数。否则,
cvtcolor()
将崩溃。你能验证一下,所有图像都是兼容的吗?”?