Ask Your Question

Revision history [back]

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]