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处的未处理异常:内存位置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文件的问题,使用在线检查器来捕捉错误。顺便说一句,我觉得向公众发布这样一个健壮性很差的代码有点自杀。
- OpenCV 4.2.0 文件存储分段错误
- 将系数存储在头文件的数组中("does not name a type"错误)
- 使用 char* 存储数据和产生的错误
- 将 c++ cmake-library 构建发布到本地存储库时出现 Gradle 错误
- 在自定义 STUCT 中存储值会产生编译器错误 [C++]
- 将 lambda 存储为 std::函数时出现分段错误
- 尝试将现有数组的反向存储到另一个数组中时C++错误。无法使用指针
- 在向量中存储字符串存在 SEG 错误
- OpenCV 文件存储 - 错误:元素之间的解析错误 (icvYMLParseValue) 缺失
- 将自定义可绘制对象存储在数组中会导致绘制时出现分割错误
- 为什么这段代码给出错误......'a'是指针到字符指针,应该存储's'的地址,因为s是指向数组的第一个元素的指针
- 代码将向量存储为向量<vector>,为什么没有错误消息?
- 将向量对象存储在共享指针投掷错误中
- Boost::序列化存储结构时的堆栈溢出错误
- 尝试读取包含 ints 并存储在 int vector 中的文件,但我不断收到错误"Segmentation fault (core dumped)"
- 读取文件时错误,其存储替代行数据
- 替换WCHAR_T错误返回存储地址
- SQL Server 2016 CLR 存储过程错误:"A system assertion check has failed"
- 在与C 双打中存储的大整数时,如何避免舍入/精确错误
- 当某些错误可以接受时,顺序加载存储原子的内存顺序应该是什么