OpenCV get a copy of even rows of a Mat

2019-08-27 00:29发布

I would like to get the even rows/cols of a mat of 3 channels, something like this:

A = 1 0 1 0 1 0
    1 0 1 0 1 0
    1 0 1 0 1 0

result = 1 1 1
         1 1 1

How to can I do this using openCV?

Thanks in advance.

EDITED:

Here is the code I'm using:

Mat img_object = imread(patternImageName);
Mat a;
for (int index = 0,j = 0; index < img_object.rows; index = index + 2, j++)
{
    a.row(j) = img_object.row(index);
}

But it throws the following exception:

OpenCV Error: Assertion failed (m.dims >= 2) in Mat, file /build/buildd/opencv-2.4.8+dfsg1/modules/core/src/matrix.cpp, line 269
terminate called after throwing an instance of 'cv::Exception'

标签: opencv rows mat
3条回答
家丑人穷心不美
2楼-- · 2019-08-27 00:47

I could finally do it. Here is the solution

Mat img_object = imread(patternImageName);
Mat B;
for (int i = 0; i < img_object.cols; i += 2)
{
     B.push_back(img_object.col(i));
}
// now we got 1 large 1d flat (column) array with all the collected elements,
B = B.reshape(0,(img_object.cols/2));// 1 elem per channel, 3 rows.
B = B.t();          // transpose it
Mat result;
for (int i = 0; i < B.rows; i += 2)
{
     result.push_back(B.row(i));
}
查看更多
家丑人穷心不美
3楼-- · 2019-08-27 00:57

You can abuse resize() function:

resize(bigImage, smallImage, Size(), 0.5, 0.5, INTER_NEAREST);

resize() function will create new image, whose size is half of the original image.

INTER_NEAREST means that the values of small image will be calculated by "nearest neighbor" approach. In this specific case it means that value of pixel at position (1,2) in small image will be taken from pixel at position (2,4) in big image.

查看更多
不美不萌又怎样
4楼-- · 2019-08-27 01:03
int j = 0;
for (int i = 0; i< A.size(); i+2)
{
    destMat.row(j) = (A.row(i));
    j++;
}
查看更多
登录 后发表回答