Viktor Bendik
Viktor Bendik

Reputation: 97

OpenCV filestorage error

Hello I would like to export 3 histograms (rgb) to a .xml file then read from .xml and save to another matrix.

Code:

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

When I try to run it it throws me this "Unhandled exception at 0x00007FF96ECA8A5C in ConsoleApplication2.exe: Microsoft C++ exception: cv::Exception at memory location 0x000000A4D911BF40."

I think its something with FileStorage::READ. When i comment the section where i read from .xml it works without error. It also works when i put only one matrix in and then read it .

I also noticed that in the .xml file i have the exported data like this:

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

Why is there this <blu>grn</blu>? I want my green matrix with the header <grn>. Why is the grn under the <blu> header?

According to openCV i should have three headers for each matrix. http://docs.opencv.org/2.4/doc/tutorials/core/file_input_output_with_xml_yml/file_input_output_with_xml_yml.html

Any help/link to solution would be cool.

Upvotes: 3

Views: 3500

Answers (2)

Zac
Zac

Reputation: 4695

In my case I was trying to load a malformed XML (not escaping an ampersand); I solved correcting my XML file, using an online checker to catch the error. BTW I find a bit suicidal to release such a poorly robust code to the public.

Upvotes: 0

Miki
Miki

Reputation: 41765

Your code is correct (aside that you redefine FileStorage fs..., just change the name).

Usually, if you have an Unhandled exception error in OpenCV, it means that you're using debug libraries in release, or viceversa.


From the comments it turns out that this was in fact the issue. Be sure to link only to opencv_world310.lib in Release and to opencv_world310d.lib in Debug.

Upvotes: 1

Related Questions