Ask Your Question
0

Question of Reshaping a matrix

asked 2018-01-19 21:26:05 -0600

Weifa Gan gravatar image

updated 2018-01-19 21:33:09 -0600

I am a new learner for opencv. When I want to reshape m33f to m91f, it will be error due to no "typedef Matx<float,9,1> Matx91f". Then what shoud i do ?pleace help me to deal with the problem. Thank you in advance. image description image description.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-01-19 23:13:34 -0600

phillity gravatar image

updated 2018-01-19 23:28:11 -0600

Using Matx:

#include "opencv2/core.hpp"

#include <iostream>

using namespace cv;
using namespace std;


int main(int argc, char *argv[])
{
    //create and fill a 3x3 matrix with normally distributed random numbers with mean of 1 and standard deviation of 3
    Matx33f m33f = Matx33f::randn(1, 3);
    cout << "m33f" << m33f << endl << endl;

    //create a 9x1 matrix, fill it previous 3x3 matrix reshaped to a 9x1 matrix
    Matx<float, 9, 1> m91f = m33f.reshape<9, 1>();
    cout << "m91f" << m91f << endl << endl;

    return 0;
}

Alternatively, using Mat:

#include "opencv2/core.hpp"

#include <iostream>

using namespace cv;
using namespace std;


int main(int argc, char *argv[])
{
    //create a 3x3 matrix with floating point datatype
    Mat m33f = Mat(3, 3, CV_32F);
    //fill matrix with normally distributed random numbers with mean of 1 and standard deviation of 3
    randn(m33f, 1, 3);
    cout << "m33f" << m33f << endl << endl;

    //create a 9x1 matrix, fill it previous 3x3 matrix reshaped to a 9x1 matrix
    //reshape has 0 for first argument meaning no change in number of channels and 9 for second argument meaning 9 rows 
    Mat m91f = m33f.reshape(0, 9);
    cout << "m91f" << m91f << endl << endl;

    return 0;
}
edit flag offensive delete link more

Comments

1

Great! Thanks your help.

Weifa Gan gravatar imageWeifa Gan ( 2018-01-20 00:21:57 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-01-19 21:26:05 -0600

Seen: 253 times

Last updated: Jan 19 '18