Ask Your Question
1

How to understand Mat_<float>

asked 2017-11-07 00:20:42 -0500

yode gravatar image

updated 2017-11-07 00:21:40 -0500

I have such code

Mat srcImage = imread("1.jpg", 0);
Mat planes[] = {Mat_<float>(srcImage), Mat::zeros(srcImage.size(), CV_32F)};
Mat complexI;
merge(planes, 2, complexI);

How to understand this Mat_<float>(srcImage)? I cannot search any useful information about it. Could help to understand it?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2017-11-07 01:45:23 -0500

berak gravatar image

updated 2017-11-07 02:11:17 -0500

it's just a clever trick (maybe too clever, if it's hard to understand ...)

i guess, the context here is some dft analysis, which requires complex, float input.

normally, you would have to do:

Mat srcImage = imread("1.jpg", 0); // read the grayscale image. it's still uchar.
Mat realPart; 
srcImage.convertTo(realPart, CV_32F); // manual conversion
Mat imPart = Mat::zeros(srcImage.size(), CV_32F); 
Mat planes [] = { realPart, imPart };
merge(planes, 2, complexI);

the Mat_<float>(img) constructor does an internal, automatic conversion to float (it's a template object, it's type is determined at compile time, so it obviously cannot hold any other data type). it's a shortcut, saving some lines of code, simply.

those template Mat_'s also have some other, interesting properties:

since the type is already handled in the constructor, you no more need at<type> to access the elements, but can use a straightforward m(2,2)

then, it has an overloaded comma operator, so you can fill it like:

Mat_<float> T(3,3);
T << .2, .2, .4,
     .1, .2, .1,
     .2, .1, .3;

(which is quite useful for quickly setting up transformation Matrices, or filter kernels)

edit flag offensive delete link more

Comments

You guess is right totally. What is your meaning of cannot hold any other data type? Where can I find more information about this usage of Mat_?

yode gravatar imageyode ( 2017-11-07 01:49:08 -0500 )edit
1
berak gravatar imageberak ( 2017-11-07 01:56:57 -0500 )edit

Thanks very much. It is useful fo me..

yode gravatar imageyode ( 2017-11-07 02:06:16 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-11-07 00:20:42 -0500

Seen: 97 times

Last updated: Nov 07 '17