简体   繁体   English

分段故障核心已转储

[英]Segmentation fault core dumped

I am trying to create a json file from c++. 我正在尝试从c ++创建一个json文件。 The used code is the following: 使用的代码如下:

    Mat projected = eigenfacesExtraction(fileName);

    ofstream myfile;
    myfile.open ("detection.json");
    myfile << "{\n";
    myfile << "\t \"meta\":{\n";
    myfile << "\t \"duration\":\""<<duration<<"\",\n";  
    myfile << "\t \"fileName\":\""<<fileName<<"\"\n";
    myfile << "},\n";

    myfile << "\t \"num_of_people\":\""<< faces.size()<<"\",\n";
    myfile << "\t \"faces\":[\n";

    myfile << "\t \"projected\":[" << projected.size <<"] ,\n";

    for(int i=0; i<faces.size(); i++){

        if(!faces.empty()){ 
            if(i!= faces.size()-1){
            myfile << "\t \"coord\":\" \t[" << faces[i].x << ", "<<faces[i].y<< ", "<<      faces[i].width << ", "<<faces[i].height<<"],\n";
            myfile << "\t \"descr\":\" \t[" << projected.at<double>(0,1) << ", "<<projected.at<double>(0,2)<< ", "<<        projected.at<double>(0,3) << ", "<<projected.at<double>(0,4)<<"],\n";
            }
            else myfile << "\t \"coord\":\" \t[" << faces[i].x << ", "<<faces[i].y<< ", "<<     faces[i].width << ", "<<faces[i].height<<"]\n";
                 myfile << "\t \"descr\":\" \t[" << projected.at<double>(0,1) << ", "<<projected.at<double>(0,2)<< ", "<<       projected.at<double>(0,3)<< ", "<<projected.at<double>(0,4)<<"],\n";
        }
    }

    myfile << "]\n";
    myfile << "}";
    myfile.close();

    cout<< "Detection process exits...."<<endl;
    //imwrite( "/opencvAssets/results/"+fileName, image );
    imwrite("outputCapture.jpeg", image);
    //waitKey(0);
    //imshow("cropped image", croppedFaceImage);

My program works fine, json file is stored properly also cout prints the message, but I am receiving the following message "Segmentation fault (core dumped)". 我的程序运行正常,json文件存储正确,cout也会打印该消息,但是我收到以下消息“ Segmentation fault(core dumped)”。 When I comment out iwrite command neither json nor cout work and I am receiving again the same message. 当我注释掉iwrite命令时,json和cout都不起作用,并且我再次收到相同的消息。 What is the matter here?? 这是怎么回事? The eigenFacesExtraction function (which may cause the problems) is the following: eigenFacesExtraction函数(可能会导致问题)如下:

 Mat Detection::eigenfacesExtraction(string fileName){

    Ptr<FaceRecognizer> model = createEigenFaceRecognizer();
    model->load("eigenfaces.yml"); // Load eigenfaces parameters
    Mat eigenvalues = model->getMat("eigenvalues"); // Eigen values of PCA
    Mat convMat = model->getMat("eigenvectors"); //Convariance matrix which contains eigenvectors
    Mat mean = model->getMat("mean"); // Mean value

    convMat.convertTo(convMat, CV_32F);
    mean.convertTo(mean, CV_32F);

    string path = fileName;

    Mat sample ,pca_ed_sample, resizedSample;
    sample = imread(path, CV_LOAD_IMAGE_GRAYSCALE);

    sample.convertTo(sample, CV_32F);
    resize(sample,resizedSample,Size(60,60),0,0,INTER_LINEAR);
    Mat nu =  resizedSample.reshape(1,3600).t();
    pca_ed_sample = (nu-mean)*(convMat);

    return pca_ed_sample;

}

Segmentation fault occurs when we try to change only readable memory or when we try to access the memory out of your program boundaries. 当我们尝试仅更改可读存储器或试图在程序边界之外访问存储器时,就会发生分段错误。 Place it in a debugger and trace it thoroughly. 将其放在调试器中并进行彻底跟踪。 For more information chech this stackoverflow question 有关更多信息,请查看此stackoverflow问题

What is a segmentation fault? 什么是细分错误?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM