0

I'm new to OpenCV and its developing. I have two channel matrix

Mat alleigen( Size(3,3) , CV_32FC2) 

I have data:

A=[1,2,3]

B=[4,5,6]

c=[7,8,9]

I want to insert as like M matrix

M= [1 4 7
    2 5 8
    3 6 9]

How do I do that in OpenCV?

Miki
  • 40,887
  • 13
  • 123
  • 202
waruni guna
  • 69
  • 1
  • 11
  • Why is your matrix 2 channel? `M` seems a single channel matrix to me. Which data of `A`, `B` or `C` should go on the first or second channel? Are you sure you want a **2 dimensional** matrix, instead of 2 channel? – Miki Nov 19 '15 at 12:30
  • CV_32FC2 means two 32-bit float channels – AlexStepanov Nov 19 '15 at 12:34
  • @Miki how to do it in single channel matrix any idea ? – waruni guna Nov 19 '15 at 12:34
  • Read this: http://stackoverflow.com/questions/1824787/opencv-multi-channel-element-access – AlexStepanov Nov 19 '15 at 12:35
  • @Miki i want to do it by using loops for coloum one loop and rows one loop i dont want to insert data manually. i need two loops to insert those values. any idea ? – waruni guna Nov 19 '15 at 12:38
  • You don't need loop to insert values in rows. Check my answer and let me know – Miki Nov 19 '15 at 12:38
  • @AlexStepanov that answer was fine 6 years ago for multichannel matrices, now the api evolved a lot :D. Also here seems that he doesn't need multichannel matrices at all. – Miki Nov 19 '15 at 12:41
  • @Miki Ah, I wish everybody could hear your words and finally stop writing plain C code and compile it with a C++ compiler – AlexStepanov Nov 19 '15 at 12:44
  • @waruniguna please edit your code into the question, because right now it's unclear where A,B,C comes from, and which values you want to put in M. – Miki Nov 19 '15 at 12:48
  • @Miki could you please provide me a good article to read. – waruni guna Nov 19 '15 at 12:48
  • as soon as I understand what you want to do, I'll look for something specific – Miki Nov 19 '15 at 12:49

1 Answers1

1

You probably didn't want a 2 channel matrix, but a simple 2 dimensional matrix:

This following code will produce the desired output:

A:
[1, 2, 3]

B:
[4, 5, 6]

C:
[7, 8, 9]

M:
[1, 2, 3;
 4, 5, 6;
 7, 8, 9]

Code:

#include <opencv2/opencv.hpp>
#include <iostream>
using namespace std;
using namespace cv;

int main()
{
    Mat1f A = (Mat1f(1, 3) << 1, 2, 3);
    Mat1f B = (Mat1f(1, 3) << 4, 5, 6);
    Mat1f C = (Mat1f(1, 3) << 7, 8, 9);

    Mat1f M(3,3);

    A.copyTo(M.rowRange(0, 1));
    B.copyTo(M.rowRange(1, 2));
    C.copyTo(M.rowRange(2, 3));

    cout << "A: " << endl << A << endl << endl;
    cout << "B: " << endl << B << endl << endl;
    cout << "C: " << endl << C << endl << endl;
    cout << "M: " << endl << M << endl << endl;

    return 0;
}
Miki
  • 40,887
  • 13
  • 123
  • 202
  • i want to use loop like for(int i=0;i – waruni guna Nov 19 '15 at 12:40
  • How do you declare the matrices A,B and C? – Miki Nov 19 '15 at 12:40
  • Do you just want to declare M with subsequent values? Or do you want fill it with values from matrices A, B, and C? – Miki Nov 19 '15 at 12:42
  • A is in code Mat destination(3, 3, CV_8UC1); which has values. – waruni guna Nov 19 '15 at 12:43
  • Can you show the code you have so far? edit it into the question, please – Miki Nov 19 '15 at 12:44
  • thank you very much. if you can please drop me a suitable tutorial to read. thank you so much. – waruni guna Nov 19 '15 at 12:51
  • I just drop a some links to get you started: [this](http://docs.opencv.org/2.4/doc/tutorials/core/mat_the_basic_image_container/mat_the_basic_image_container.html), [this](http://stackoverflow.com/a/31894250/5008845), [this](http://stackoverflow.com/a/31338186/5008845). Also note that OpenCV documentation comes with [nice tutorials](http://docs.opencv.org/2.4/doc/tutorials/tutorials.html), and you can find most answers to your need with Google or StackOverflow – Miki Nov 19 '15 at 12:55