OpenCV文件存储错误

OpenCV filestorage error

本文关键字:错误 存储 文件 OpenCV      更新时间:2023-10-16

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

当我尝试运行它时,它会向我抛出"ConsoleApplication2.exe中0x00007FF96ECA8A5C处的未处理异常:内存位置0x000000A4D911BF40处的Microsoft C++异常:cv::异常。"

我认为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

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

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

通常,如果您在OpenCV中出现Unhandled exception错误,则表示您正在使用版本中的调试库,反之亦然。


从评论来看,事实证明这就是问题所在。请确保仅链接到Release中的opencv_world310.lib和Debug中的opencv_world310d.lib

在我的案例中,我试图加载一个格式错误的XML(没有转义"与"符号);我解决了更正XML文件的问题,使用在线检查器来捕捉错误。顺便说一句,我觉得向公众发布这样一个健壮性很差的代码有点自杀。