OpenCV filestorage错误

时间:2016-03-31 17:04:03

标签: c++ xml opencv export file-storage

您好我想将3个直方图(rgb)导出到.xml文件,然后从.xml中读取并保存到另一个矩阵。

代码:

int main(int argc, char** argv)
{
    Mat org,cmp, dst;

    /// Load image
    org = imread("arrowr.png");
    cmp = imread("cat.jpg");

    if (!org.data)
    {
        return -1;
    }
    /// Separate the image in 3 places ( B, G and R )
    Mat bgr_planes_org[3] = {};         //zmena z vector<Mat> bgr_planes;
    split(org, bgr_planes_org);

    /// Establish the number of bins
    int histSize = 256;

    /// Set the ranges ( for B,G,R) )
    float range[] = { 0, 256 };
    const float* histRange = { range };

    bool uniform = true; bool accumulate = false;

    Mat b_hist_org, g_hist_org, r_hist_org;


    /// Compute the histograms:
    calcHist(&bgr_planes_org[0], 1, 0, Mat(), b_hist_org, 1, &histSize, &histRange, uniform, accumulate);
    calcHist(&bgr_planes_org[1], 1, 0, Mat(), g_hist_org, 1, &histSize, &histRange, uniform, accumulate);
    calcHist(&bgr_planes_org[2], 1, 0, Mat(), r_hist_org, 1, &histSize, &histRange, uniform, accumulate);

    // Draw the histograms for B, G and R
    int hist_w = 512; int hist_h = 400;
    int bin_w = cvRound((double)hist_w / histSize);

    Mat histImage(hist_h, hist_w, CV_8UC3, Scalar(0, 0, 0));

    /// Normalize the result to [ 0, histImage.rows ]
    normalize(b_hist_org, b_hist_org, 0, histImage.rows, NORM_MINMAX, -1, Mat());
    normalize(g_hist_org, g_hist_org, 0, histImage.rows, NORM_MINMAX, -1, Mat());
    normalize(r_hist_org, r_hist_org, 0, histImage.rows, NORM_MINMAX, -1, Mat());

    Mat mat_r, mat_g, mat_b;
    string filename = "test.xml";
    FileStorage fs(filename, FileStorage::WRITE);
    fs << "blu" << b_hist_org;
    fs << "grn" << g_hist_org;
    fs << "red" << r_hist_org;
    fs.release();

    FileStorage fs(filename, FileStorage::READ);
    fs["blu"] >> mat_b;
    fs["red"] >> mat_r;
    fs["grn"] >> mat_g;
    fs.release();

        cout << mat_r << endl;
        cout << mat_g << endl;
        cout << mat_b << endl;

        system("pause");

    return 0;
}

当我尝试运行它时,它会抛出这个&#34;在ConsoleApplication2.exe中的0x00007FF96ECA8A5C处的未处理异常:Microsoft C ++异常:cv ::内存位置0x000000A4D911BF40的异常。&#34;

我认为它与FileStorage::READ有关。当我评论从.xml读取的部分时,它可以正常工作。当我只放入一个矩阵然后读取它时,它也有效。

我还注意到在.xml文件中我有这样的导出数据:

<blu type_id="opencv-matrix">
<rows>256</rows>
<cols>1</cols>
<dt>f</dt>
<data>
"blue matrix data"
</data></blu>
<blu>grn</blu>
<blu type_id="opencv-matrix">
<rows>256</rows>
<cols>1</cols>
<dt>f</dt>
<data>
"green matrix data"    
</data></blu>
<red type_id="opencv-matrix">
<rows>256</rows>
<cols>1</cols>
<dt>f</dt>
<data>
"red matrix data"
</data></red>
</opencv_storage>

为什么会出现<blu>grn</blu>?我希望我的绿色矩阵带有标题<grn>。为什么grn位于<blu>标题下?

根据openCV,每个矩阵应该有三个标题。 http://docs.opencv.org/2.4/doc/tutorials/core/file_input_output_with_xml_yml/file_input_output_with_xml_yml.html

解决方案的任何帮助/链接都会很酷。

2 个答案:

答案 0 :(得分:0)

您的代码是正确的(除非您重新定义FileStorage fs...,只需更改名称)。

通常情况下,如果您在OpenCV中出现Unhandled exception错误,则表示您在发布时使用了调试库,反之亦然。

从评论中可以看出这实际上是问题所在。请务必仅链接到版本中的opencv_world310.lib和调试中的opencv_world310d.lib

答案 1 :(得分:0)

在我的情况下,我试图加载格式错误的XML(不要转义与号);我解决了使用online checker来纠正错误的问题,从而纠正了我的XML文件。顺便说一句,我有点自杀,无法向公众发布这样的健壮代码。