I am using OpenCV and also want to add some of cool functions from mlpack, which is using Armadillo matrices.
Is there an easy way to convet between cv::Mat and arms::mat?
Thanks!
OpenCV's Mat has a pointer to its data. Armadillo has a constructor that is capable of reading from external data. It's easy to put them together. Remember that Armadillo stores in column-major order, whereas OpenCV uses row-major. I suppose you'll need to add another step for transformation, before or after.
cv::Mat opencv_mat; //opencv's mat, already transposed.
arma::mat arma_mat( reinterpret_cast<double*>opencv_mat.data, opencv_mat.rows, opencv_mat.cols )
The cv::Mat constructor has a form that accepts pointer to data, and arma::mat has a function for a double* pointer to its data called memptr().
So, if you'd like to convert from arma::mat to cv::Mat, this should work:
cv::Mat opencv_mat( rows, cols, CV_64FC1, arma_mat.memptr() )
Related
I am new to opencv and I have a data array for a binary image, like [1,0,0,1,1,0,0,1...]. My goal is to read it in opencv and manipulate it with opencv functions, so how to create a binary image from raw data array with opencv?
you can easily create a Mat from that data :
unsigned char bits[] = {1,0,0,1,1,0,0,1,1};
Mat m( 3,3, CV_8UC1, bits );
if your bits were an int[] you'd have to use CV_32SC1.
I have a 3x3x1000 OpenCV Mat matrix created using
int sz[] = {3,3,1000};
Mat bigCube(3, sz, CV_8U);
I want to do matrix operations on the 1000 separate 3x3 sub-matrices. But I can not find a way to do this.
The most obvious would be in a for-loop using Range, something like;
bigCube(Range(0,3),Range(0,3),Range(i,i+1));
//Do some operations...
But this won't compile. Is there a way to do this?
I have 3 matrices (R G B) in double precision format, so all I need is OpenCV version of matlab cat function to display image. If you have any sources or references please post, I would be grateful.
There is merge() for such tasks. I'm assuming that you're using C++ for the following example code, but this function is available for C and Python too (see docs).
// Suppose you have 3 matrices (instances of class cv::Mat)
// named channelR, channelG and channelB that hold your data
std::vector<cv::Mat> channels;
channels.push_back(channelR);
channels.push_back(channelG);
channels.push_back(channelB);
cv::Mat outputImage;
merge(channels, outputImage);
There is a IplImage and CvMat in OpenCV. What are the full names of them?
IPL in IplImage stands for Intel Processing Library, which is a remnant of when OpenCV was maintained by Intel.
CV in cvMat stands for Computer Vision Matrix, which is a data structure commonly used in graphics.
IplImage is an old structure, which I believe is internally converted into cv::Mat, or just Mat if you're in the cv namespace already. Likewise, cvMat is converted into Mat as well.
http://opencv.willowgarage.com/documentation/cpp/basic_structures.html?highlight=iplimage
I'm working on a face recognition project in which we are using PCA to reduce feature vector size of an image. The trouble is, during training, I create the PCA object by incorporating all the training images. Now, during testing, I need the PCA object obtained earlier.
I cannot seem to figure out how to write the PCA object to a file, so that I can use it during testing. One alternative is that I write it's eigenvectors to the file. But it would be so much more convenient to write the object itself. Is there a way to do this?
As far as I know, there is no generic way of saving PCA objects to a file. You will need to save eigenvectors, eigenvalues and mean to a file, and then put them into a new PCA after loading. You have to remember to use a format that doesn't lose precision, especially for mean.
Here is some example code:
#include "opencv2/core/core.hpp"
#include <iostream>
...
cv::PCA pca1;
cv::PCA pca2;
cv::Mat eigenval,eigenvec,mean;
cv::Mat inputData;
cv::Mat outputData1,outputData2;
//input data has been populated with data to be used
pca1(inputData,Mat()/*dont have previously computed mean*/,
CV_PCA_DATA_AS_ROW /*depends of your data layout*/);//pca is computed
pca1.project(inputData,outputData1);
//here is how to extract matrices from pca
mean=pca1.mean.clone();
eigenval=pca1.eigenvalues.clone();
eigenvec=pca1.eigenvectors.clone();
//here You can save mean,eigenval and eigenvec matrices
//and here is how to use them to make another pca
pca2.eigenvalues=eigenval;
pca2.eigenvectors=eigenvec;
pca2.mean=mean;
pca2.project(inputData,outputData2);
cv::Mat diff;//here some proof that it works
cv::absdiff(outputData1,outputData2,diff);
std::cerr<<sum(diff)[0]<<std::endl; //assuming Youre using one channel data, there
//is data only in first cell of the returned scalar
// if zero was printed, both output data matrices are identical
You may try this.
void save(const string &file_name,cv::PCA pca_)
{
FileStorage fs(file_name,FileStorage::WRITE);
fs << "mean" << pca_.mean;
fs << "e_vectors" << pca_.eigenvectors;
fs << "e_values" << pca_.eigenvalues;
fs.release();
}
int load(const string &file_name,cv::PCA pca_)
{
FileStorage fs(file_name,FileStorage::READ);
fs["mean"] >> pca_.mean ;
fs["e_vectors"] >> pca_.eigenvectors ;
fs["e_values"] >> pca_.eigenvalues ;
fs.release();
}
Here is the source.