Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert an OpenCV cv::Mat into a float* that can be fed into Vlfeat vl_dsift_process ?

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.

like image 794
Jiayuan Ma Avatar asked Oct 28 '11 08:10

Jiayuan Ma


People also ask

What is a CV :: mat?

In OpenCV the main matrix class is called Mat and is contained in the OpenCV-namespace cv. This matrix is not templated but nevertheless can contain different data types. These are indicated by a certain type-number. Additionally, OpenCV provides a templated class called Mat_, which is derived from Mat.

What is CV_32FC1?

CV_32F defines the depth of each element of the matrix, while. CV_32FC1 defines both the depth of each element and the number of channels.

What is mat type in OpenCV?

The Mat class of OpenCV library is used to store the values of an image. It represents an n-dimensional array and is used to store image data of grayscale or color images, voxel volumes, vector fields, point clouds, tensors, histograms, etc.

What is CV_64F?

CV_64F is the same as CV_64FC1 . So if you need just 2D matrix (i.e. single channeled) you can just use CV_64F. EDIT. More generally, type name of a Mat object consists of several parts.


2 Answers

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.

like image 138
Sam Avatar answered Nov 14 '22 21:11

Sam


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]);
like image 33
user1228855 Avatar answered Nov 14 '22 22:11

user1228855