首页 \ 问答 \ OpenCV文件存储错误(OpenCV filestorage error)

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

当我尝试运行它时,它将引发此“ConsoleApplication2.exe中0x00007FF96ECA8A5C处的未处理异常:Microsoft C ++异常:内存位置0x000000A4D911BF40处的cv :: Exception”。

我认为它与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> 。 为什么在<blu>标题下的grn

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

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


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.


原文:https://stackoverflow.com/questions/36339509
更新时间:2023-05-06 12:05

最满意答案

该代码中有大量未转义的大括号。 Python认为所有的大括号都是占位符,并试图将它们全部替换掉​​。 但是,您只提供了一个值。

我希望你不希望你所有的大括号都成为占位符,所以你应该把你不想代替的那些加倍。 如:

template = """                                                                  
function routes(app, model){{
  app.get('/preNew{className}', function(req, res){{
    res.render('{className}'.ejs, {{}});                                           
  }};                                                      
}});""".format(className=className)

我还冒昧地为字符串文字使用三重引号,因此您不需要在每行末尾使用反斜杠。


You have a number of unescaped braces in that code. Python considers all braces to be placeholders and is trying to substitute them all. However, you have only supplied one value.

I expect that you don't want all your braces to be placeholders, so you should double the ones that you don't want substituted. Such as:

template = """                                                                  
function routes(app, model){{
  app.get('/preNew{className}', function(req, res){{
    res.render('{className}'.ejs, {{}});                                           
  }};                                                      
}});""".format(className=className)

I also took the liberty of using triple quotes for the string literal so you don't need the backslashes at the end of each line.

相关问答

更多
  • 该代码中有大量未转义的大括号。 Python认为所有的大括号都是占位符,并试图将它们全部替换掉。 但是,您只提供了一个值。 我希望你不希望你所有的大括号都成为占位符,所以你应该把你不想代替的那些加倍。 如: template = """ function routes(app, model){{ app.get('/preNew{className}', function( ...
  • 尝试在json转储中应用str.format是一个注定的想法,原因有几个,主要原因是字符串转储的封闭{}冲突/丢失格式。 我建议事先用命名字段预处理你的字典: import json data = { "u_in_record_type": '{type}', "u_company_source": '{source}' } type="Test" source="Source" new_data = {k:v.format(type=type,source=s ...
  • 问题是您在那里没有指定格式的键的{和}字符。 您需要将其加倍,因此请将代码更改为: addr_list_formatted.append(""" "{0}" {{ "gamedir" "str" "address" "{1}" }} """.format(addr_list_idx, addr)) The problem is those { and } characters you have there that don't specify a key for ...
  • 对问题的简单回答,单词处理程序应该是处理程序。 Simple answer to problem, the word handler should have been handlers.
  • 您有一个名为test.status的变量,其值为error: failed to open driver pseudo terminal : Device not configuredFAILURE test.status 但是dict status_map不包含任何这样的键,因此使用test.status键访问此dict会导致keyError。 status_map[test.status] KeyError: 'error: failed to open driver pseudo terminal ...
  • 您可以使用字典逐行执行,并使用**作为关键字参数传递字典 d=dict() d['model']=model d['content']=data d['theaders']=theaders html = html.format(**d) you could do it line by line using a dictionary and passing the dict as keyword arguments using ** d=dict() d['model']=model d['conten ...
  • 没有PDF问题 很遗憾,您没有分享有问题的PDF。 有问题的字典的警告输出 [ /Type, /Font, /Subtype, /Type0, /BaseFont, /ABCDEE+微软é, é», /Encoding, /Identity-H, /DescendantFonts, , /ToUnicode, ] 似乎表示一个无效的BaseFont名称被解释为名称( / ABCDEE +微软é )和另一个对 ...
  • save的第二个参数不是扩展名,它是图像文件格式中指定的格式参数,JPEG文件的格式说明符是JPEG ,而不是JPG 。 如果您希望PIL决定要保存哪种格式,则可以忽略第二个参数,例如: image.save(name) 请注意,在这种情况下,您只能使用文件名而不能使用文件对象。 有关详细信息,请参阅.save()方法的文档 : format - 可选格式覆盖。 如果省略,则使用的格式由文件扩展名确定。 如果使用文件对象而不是文件名,则应始终使用此参数。 或者,您可以检查扩展名并手动确定格式。 例如: d ...
  • 你没有正确地传递你的论点。 def runFile(model, datafile, time_scale, max_PEs, tank_address_dict, PMT_dict, channel_ref): hawc_func.runFile(model,data,4000,TankDict,PTMDict,time_scale,channel_ref) 在这里,你有max_PEs = TankDict 。 这可能不是你唯一的问题。 首先修复此问题,如果您仍然遇到问题,请使用固定代码更新您的帖子, ...
  • 当你可以看到你试图在其中定位数据的JSON对象的嵌套结构时,更容易看到发生了什么: 工作示例#1 - 使用Python 2.6.9和2.7.10以及3.3.5和3.5.0进行测试 import json json_string = ''' { "REQUEST": "FOUND", "739c5b1cd5681e668f689aa66bcc254c": { "plain": "test", "hexplain": "74657374", "a ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)