OpenCV get a copy of even rows of a Mat

北城以北 提交于 2019-12-11 20:22:02

问题


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'

回答1:


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.




回答2:


int j = 0;
for (int i = 0; i< A.size(); i+2)
{
    destMat.row(j) = (A.row(i));
    j++;
}



回答3:


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));
}


来源:https://stackoverflow.com/questions/27905929/opencv-get-a-copy-of-even-rows-of-a-mat

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!