Skip to content

Problems with loading MatND from xml file in c++ and python #17272

@arc130

Description

@arc130
System information (version)
  • OpenCV => 3.2( c++ ) 4.2(python)
  • Operating System / Platform => Windows 64 Bit
  • Compiler => Visual Studio 2010

I have saved an numpy array of size(780, 570, 14) to a xml file using cv2.FileStorage in python. However, when I try loading/reading it again using cv::FileStorage in c++, it fails and gives me the error below:
OpenCV Error: Unspecified error (Too complex format for the matrix) in icvDecodeSimpleFormat, file ..\..\..\opencv\sources\modules\core\src\persistence.cpp, line 4722

The head of xml file is below:

<?xml version="1.0"?>
<opencv_storage>
<Label type_id="opencv-matrix">
  <rows>780</rows>
  <cols>570</cols>
  <dt>"14i"</dt>
  <data> 

when saving in c++, I find the head of xml file is:

<?xml version="1.0"?>
<opencv_storage>
<Label type_id="opencv-nd-matrix">
  <sizes>
    780 570 14</sizes>
  <dt>w</dt>
  <data>

I change the head of previous xml file and loading succeed.
Is there any way to save python file in type_id: opencv-nd-matrix or loading type_id: opencv-matrix in c++.

The c++ loading code is:

	cv::MatND  label
	cv::FileStorage fs("./Label.xml", cv::FileStorage::READ);

	// if failed to open a file
	if(!fs.isOpened()){
		fs.release();
		return -1;
	}
	fs["Label"] >> label;
	fs.release();
	return 0;

The c++ saving code is:

	int dims[]={780, 540, 14};
	cv::MatND mat3d(3, dims, CV_16UC1);
	cv::FileStorage fs("./Mat.xml", cv::FileStorage::WRITE);
	fs<<"Label"<<mat3d;
	fs.release();
	return 0;

The python saving code is:

    labels = np.random.randn(780, 540, 14)
    fs = cv2.FileStorage('./Label.xml', cv2.FileStorage_WRITE)
    fs.write('Label', labels)
    fs.release()

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions