OpenCV 文件存储错误

OpenCV filestorage error

你好,我想将 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;
}

当我尝试 运行 它抛出这个 "Unhandled exception at 0x00007FF96ECA8A5C in ConsoleApplication2.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000A4D911BF40."

我认为它与 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>?我想要带有 header <grn> 的绿色矩阵。为什么grn<blu>下面header?

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

任何 help/link 的解决方案都会很棒。

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

通常,如果您在 OpenCV 中遇到 Unhandled exception 错误,这意味着您正在使用发行版中的调试库,反之亦然。


从评论中可以看出,这确实是问题所在。请务必 link 在 Release 中仅 opencv_world310.lib,在 Debug 中 opencv_world310d.lib

在我的例子中,我试图加载格式错误的 XML(不转义&符号);我解决了更正我的 XML 文件,使用 online checker 来捕获错误。顺便说一句,我发现向 public.

发布如此健壮的代码有点自杀。