Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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);
    randn(m33f, 1, 3);
    cout << "m33f" << m33f << endl << endl;

    //create a 9x1 matrix, fill it previous 3x3 matrix reshaped to a 9x1 matrix
    Mat m91f = m33f.reshape(0, 9);
    cout << "m91f" << m91f << endl << endl;
    return 0;
}

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);
    randn(m33f, 1, 3);
    cout << "m33f" << m33f << endl << endl;

    //create a 9x1 matrix, fill it previous 3x3 matrix reshaped to a 9x1 matrix
    Mat m91f = m33f.reshape(0, 9);
    cout << "m91f" << m91f << endl << endl;
     return 0;
}

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);
    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;
}

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;
}