Ask Your Question

Revision history [back]

i tested @FooBar 's comment with the code below. as he said cv:resize() do the trick.

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

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    Mat m = (Mat_<uchar>(5,5) <<
             0,1,0,1,0,
             0,1,0,1,0,
             0,1,0,1,0,
             0,1,0,1,0,
             0,1,0,1,0);

    cout << "Mat m = (Mat_<int>(5,5) << 0,1,...)\n" << m << "\n" << endl;

    Mat m_resized;
    resize( m, m_resized, Size(), 0.5,0.5, INTER_LINEAR );

    cout << "resized ( INTER_LINEAR )\n" << m_resized << "\n" << endl;

    resize( m, m_resized, Size(), 0.5,0.5, INTER_NEAREST );

    cout << "resized ( INTER_NEAREST )\n" << m_resized << "\n" << endl;

    m = m.t();

    cout << "m = m.t()\n" << m << "\n" << endl;

    resize( m, m_resized, Size(), 0.5,0.5, INTER_LINEAR );

    cout << "resized ( INTER_LINEAR )\n" << m_resized << "\n" << endl;

    resize( m, m_resized, Size(), 0.5,0.5, INTER_NEAREST );

    cout << "resized ( INTER_NEAREST )\n" << m_resized << "\n" << endl;
    return 0;
}

output of the code:

Mat m = (Mat_<int>(5,5) << 0,1,...)
[  0,   1,   0,   1,   0;
   0,   1,   0,   1,   0;
   0,   1,   0,   1,   0;
   0,   1,   0,   1,   0;
   0,   1,   0,   1,   0]

resized ( INTER_LINEAR )
[  1,   1;
   1,   1]

resized ( INTER_NEAREST )
[  0,   0;
   0,   0]

m = m.t()
[  0,   0,   0,   0,   0;
   1,   1,   1,   1,   1;
   0,   0,   0,   0,   0;
   1,   1,   1,   1,   1;
   0,   0,   0,   0,   0]

resized ( INTER_LINEAR )
[  1,   1;
   1,   1]

resized ( INTER_NEAREST )
[  0,   0;
   0,   0]

i tested @FooBar 's comment with the code below. below.

as he said cv:resize() with INTER_NEAREST flag do the trick.

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

using namespace cv;
using namespace std;

int main( int argc, char** argv )
{
    Mat m = (Mat_<uchar>(5,5) (Mat_<uchar>(6,6) <<
             0,1,0,1,0,
             0,1,0,1,0,
             0,1,0,1,0,
             0,1,0,1,0,
             0,1,0,1,0);
0,2,0,2,0,2,
             0,2,0,2,0,2,
             0,2,0,2,0,2,
             0,2,0,2,0,2,
             0,2,0,2,0,2,
             0,2,0,2,0,2);

    cout << "Mat m = (Mat_<int>(5,5) << 0,1,...)\n" (Mat_<uchar>(6,6) << 0,2,...)\n" << m << "\n" << endl;

    Mat m_resized;
     resize( m, m_resized, Size(), 0.5,0.5, INTER_LINEAR 0.5, 0.5, INTER_NEAREST );

    cout << "resized m ( INTER_LINEAR with INTER_NEAREST flag )\n" << m_resized << "\n" << endl;

    resize( m, m_resized, Size(), 0.5,0.5, INTER_NEAREST 0.5, 0.5, INTER_LINEAR );

    cout << "resized m ( INTER_NEAREST with INTER_LINEAR flag )\n" << m_resized << "\n" << endl;

    m = m.t();

    cout << "m = m.t()\n" << m << "\n" << endl;

    resize( m, m_resized, Size(), 0.5,0.5, INTER_LINEAR 0.5, 0.5, INTER_AREA );

    cout << "resized m ( INTER_LINEAR )\n" << m_resized << "\n" << endl;

    resize( m, m_resized, Size(), 0.5,0.5, INTER_NEAREST );

    cout << "resized ( INTER_NEAREST with INTER_AREA flag )\n" << m_resized << "\n" << endl;
    return 0;
}

output of the code:

Mat m = (Mat_<int>(5,5) << 0,1,...)
(Mat_<uchar>(6,6) << 0,2,...)
[  0,   1,   0,   1,   0;
   0,   1,   0,   1,   0;
   0,   1,   0,   1,   0;
   0,   1,   0,   1,   0;
   0,   1,   0,   1,   0]
2,   0,   2,   0,   2;
   0,   2,   0,   2,   0,   2;
   0,   2,   0,   2,   0,   2;
   0,   2,   0,   2,   0,   2;
   0,   2,   0,   2,   0,   2;
   0,   2,   0,   2,   0,   2]

resized m ( INTER_LINEAR with INTER_NEAREST flag )
[  1,   1;
   1,   1]
0,   0,   0;
   0,   0,   0;
   0,   0,   0]

resized m ( INTER_NEAREST with INTER_LINEAR flag )
[  0,   0;
   0,   0]

m = m.t()
[  0,   0,   0,   0,   0;
   1,   1,   1,   1,   1;
   0,   0,   0,   0,   0;
   1,   1,   1,   1,   1;
   0,   0,   0,   0,   0]
1,   1,   1]

resized m ( INTER_LINEAR with INTER_AREA flag )
[  1,   1,   1;
   1,   1,   1;
   1,   1,   1]

resized ( INTER_NEAREST )
[  0,   0;
   0,   0]