分段故障核心被丢弃

时间:2013-12-16 13:29:42

标签: c++ json opencv core

我正在尝试从c ++创建一个json文件。使用的代码如下:

    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);

我的程序工作正常,json文件正确存储也cout打印消息,但我收到以下消息“Segmentation fault(core dumped)”。当我注释掉iwrite命令既没有json也没有cout工作,我再次收到相同的消息。这是怎么回事? 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;

}

1 个答案:

答案 0 :(得分:0)

当我们尝试仅更改可读内存或尝试从程序边界访问内存时,会发生分段错误。将它放在调试器中并彻底跟踪它。有关更多信息,请参阅此stackoverflow问题

What is a segmentation fault?