-1

How to efficiency linearized Mat (symmetric matrix) to one row by right triangle. For example, when I have:

0aabbb
b0aaaa
ba0bba
bac0aa
aaaa0c
abcab0

and then from that I get:

aabbbaaaabbaaac

Something like this:

...
template<class T>
Mat SSMJ::triangleLinearized(Mat mat){
    int c = mat.cols;
    Mat row = Mat(1, ((c*c)-c)/2, mat.type());
    int i = 0;
    for(int y = 1; y < mat.rows; y++)
    for(int x = y; x < mat.cols; x++) {
        row.at<T>(i)=mat.at<T>(y, x);
        i++;
    }
    return row;
}
...
gulliver
  • 86
  • 2
  • 9

1 Answers1

0

Since data in your mat is just a 1d array stored in row.data you can do whatever you want with it. I don't think you will find anything more special (w/o using vectorized methods) than just copying from this array.

int rows = 6;   
char data[] = { 0,1,2,3,4,5,
                0,1,2,3,4,5,
                0,1,2,3,4,5,
                0,1,2,3,4,5,
                0,1,2,3,4,5};
char result[100];
int offset = 0;
for (int i = 0; i < 5; offset += 5-i, i++) {
    memcpy(&result[offset] , &data[rows * i + i + 1], 5 - i);
}

Or with opencv Mat it would be

int rows = mat.cols;
char result[100];   // you can calculate how much data u need
int offset = 0;
for (int i = 0; i < 5; offset += 5-i, i++) {
    memcpy(&result[offset] , &mat.data[rows * i + i + 1], 5 - i);
}
Mat resultMat(1, offset, result);
Dmitrii Z.
  • 2,287
  • 3
  • 19
  • 29