I have some MATLAB code that I want to migrate to OpenCV. The data that the MATLAB code uses is stored in a .mat file which is then loaded at run time.
I converted this .mat file into a .csv file and am then reading this data into OpenCV as a string using ifstream. I am having problems converting this string into a data-structure that I can then use in OpenCV.
Is there anyway that I can convert the .mat file / .csv file to a Mat data structure in OpenCV?
Edit: Based on the Answer I received, I was successful in reading MATLAB data into OpenCV using a YML file. This I did in a MAC environment. However, when I try to read the file with the same piece of code in a Windows environment, the file is not being read. Just wondering if anyone ran into such an issue. Below is my code snippet:
// OpenCVDemo.cpp : Defines the entry point for the console application.
// Created for build/install tutorial, Microsoft Visual Studio and OpenCV 2.4.0
#include "stdafx.h"
#include <cv.h>
#include <cxcore.h>
#include <highgui.h>
#include <iostream>
using namespace cv;
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
cout << "Loading the basis." << endl;
FileStorage fs1("basis.yml", FileStorage::READ);
cv::FileNode B = fs1["B"];
if (B.EMPTY)
{
cout << "File is empty or does not exist" << endl;
return 1;
}
fs1["B"] >> basis;
cout << basis.cols << endl;
fs1.release();
return 0;
}
You can use the XML/YAML file storages provided by the OpenCV class Filestorage.
As an example, if you have a yml file like this one, that I'll call demo.yml
Then, you can use OpenCV FileStorage class to load the variables contained on this demo.yml file as:
Now, what you can do is writing your own Matlab parser, similarly to my matlab2opencv.m below:
So you could run something like:
obtaining newStorageFile.yml:
from which you could read
varA
andvarB
as previously explained forVariable1
andVariable2
.Hope it helps
You can use the Matlab bridge from opencv contrib. All you need from Opencv Contrib is to copy contrib/modules/matlab/include/opencv2/matlab folder into the include/opencv2 folder.
along with the Matlab Compiler Runtime (just libmx.lib, libmex.lib and libmat.lib).
Besides using XML/YAML file storages suggested by @Drodbar, you can also try cvmatio, which provides APIs to directly load MATLAB MAT files to OpenCV.
The code will be simply like: