Segmentation fault core dumped

拈花ヽ惹草 提交于 2020-01-17 03:05:09

问题


I am trying to create a json file from c++. 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)". When I comment out iwrite command neither json nor cout work and I am receiving again the same message. What is the matter here?? The eigenFacesExtraction function (which may cause the problems) is the following:

 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;

}

回答1:


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

What is a segmentation fault?



来源:https://stackoverflow.com/questions/20612172/segmentation-fault-core-dumped

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!