How to convert an OpenCV cv::Mat
into a float*
that can be fed into Vlfeat vl_dsift_process
?
I cannot understand how vl_dsift_process
works on a one-dimensional float array.
The official samples only demonstrate how to use MatLab API.
It seems that there are no samples about C API, and the only clue is the MEX file in the source package.
-
May be this problem is related to how a matrix is passed from Matlab to C ? – Jiayuan Ma Oct 28 '11 at 10:09
Add a comment
|
float* matData = (float*)myMat.data;
Make sure the matrix is not deleted/goes out of scope before finishing using the pointer to data. And make sure the matrix contain floats.
-
1The matrix should be checked for being continious, otherwise it can't be interpreted as a one-dimensional array. And I think it's more appropriate to use
reinterpret_cast
here as it clearly indicates the dangerous nature of this cast. Anyway, DSIFT extraction would probably be many orders of magnitude slower than converting the matrix tovector<float>
, so I think the associated overhead is negligible and worth the added safety and portability. – lizarisk Jul 30 '13 at 12:35
I cannot understand how vl_dsift_process works on a one-dimensional float array
DSIFT expects a grayscale image where intensivity of pixel (x, y) is stored in float_array[y * width + x] as float value. In OpenCV images are stored as unsigned chars so simple conversion of Mat::data to float* will not work. You have to manually convert every value to float:
Mat mat = imread("image_name.jpg", 0); // 0 stands for grayscale
vector<float> img;
for (int i = 0; i < mat.rows; ++i)
for (int j = 0; j < mat.cols; ++j)
img.push_back(mat.at<unsigned char>(i, j));
vl_dsift_process(dsift, &img[0]);