Ask Your Question
-1

copy even and columns columns from a matrix

asked 2016-08-03 06:05:48 -0600

How to copy even columns (column numbers: 0, 2, 4) from a matrix to another matrix (column numbers: 1, 2, 3)? and how to copy odd columns (column numbers: 1, 3, 5) from a matrix to another matrix (column numbers: 1, 2, 3)?

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
1

answered 2016-08-03 13:27:21 -0600

another approach is using cv:resize

#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>

using namespace cv;
using namespace std;

Mat resizeEvenOdd( const Mat source, int even_odd = 0 )
{
    Mat tmp;

    if( source.cols % 2)
    {
        if( even_odd )
        {
            tmp = source( Rect( even_odd, 0, source.cols-1, source.rows ));
        }
        else
            hconcat( source, source.col(source.cols-1), tmp );

    }
    else if( even_odd )
    {
        Mat tmp1;
        hconcat( source, source.col(source.cols-1), tmp1 );
        tmp = tmp1(Rect( even_odd, 0, source.cols, source.rows));
    }
    else   tmp = source;

    resize( tmp, tmp, Size(), 0.5, 1, INTER_NEAREST );

    return tmp;
}

int main( int argc, char** argv )
{
    Mat m = (Mat_<uchar>(5,5) <<
             1,2,3,4,5,
             6,7,8,9,10,
             11,12,13,14,15,
             16,17,18,19,20,
             21,22,23,24,25);

    cout << "Mat m\n" << m << "\n" << endl;

    Mat even_rows = resizeEvenOdd( m );
    Mat odd_rows = resizeEvenOdd( m, 1 );

    cout << "even_rows of Mat m\n" << even_rows << "\n" << endl;
    cout << "odd_rows of Mat m\n" << odd_rows << "\n" << endl;

    Mat m1;
    hconcat( m, m.col(0), m1 );

    cout << "Mat m1\n" << m1 << "\n" << endl;

    even_rows = resizeEvenOdd( m1 );
    odd_rows = resizeEvenOdd( m1, 1 );

    cout << "even_rows of Mat m1\n" << even_rows << "\n" << endl;
    cout << "odd_rows of Mat m1\n" << odd_rows << "\n" << endl;
    return 0;
}

output of test code

Mat m
[  1,   2,   3,   4,   5;
   6,   7,   8,   9,  10;
  11,  12,  13,  14,  15;
  16,  17,  18,  19,  20;
  21,  22,  23,  24,  25]

even_rows of Mat m
[  1,   3,   5;
   6,   8,  10;
  11,  13,  15;
  16,  18,  20;
  21,  23,  25]

odd_rows of Mat m
[  2,   4;
   7,   9;
  12,  14;
  17,  19;
  22,  24]

Mat m1
[  1,   2,   3,   4,   5,   1;
   6,   7,   8,   9,  10,   6;
  11,  12,  13,  14,  15,  11;
  16,  17,  18,  19,  20,  16;
  21,  22,  23,  24,  25,  21]

even_rows of Mat m1
[  1,   3,   5;
   6,   8,  10;
  11,  13,  15;
  16,  18,  20;
  21,  23,  25]

odd_rows of Mat m1
[  2,   4,   1;
   7,   9,   6;
  12,  14,  11;
  17,  19,  16;
  22,  24,  21]
edit flag offensive delete link more

Comments

thankyou! I was also residing near this approach

Ayesha Siddique gravatar imageAyesha Siddique ( 2016-08-04 03:40:52 -0600 )edit
1

answered 2016-08-03 09:48:24 -0600

berak gravatar image

updated 2016-08-03 13:42:36 -0600

there is no magic, it is just a lot of "footwork":

Mat_<float> M(6,6); // demo data
M << 1,2,3,4,5,6, 
     2,3,4,5,6,7,
     3,4,5,6,7,8,
     4,5,6,7,8,9,
     5,6,7,8,9,0,
     6,7,8,9,0,1;

Mat N; // target
int offset = 0; // 0 for even, 1 for odd
for (int i=offset; i<M.cols; i+=2)
{
    Mat col = M.col(i);
    if (N.empty()) // 1st time, copy
        N = col; 
    else
        hconcat(N,col,N); // append to the right
}

cerr << N << endl;

[1, 3, 5;
  2, 4, 6;
  3, 5, 7;
  4, 6, 8;
  5, 7, 9;
  6, 8, 0]
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-08-03 06:05:48 -0600

Seen: 266 times

Last updated: Aug 03 '16