OpenCV获得偶数行Mat的副本

时间:2015-01-12 15:56:04

标签: opencv rows mat

我想获得3个频道的偶数行/列,如下所示:

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

如何使用openCV执行此操作?

提前致谢。

编辑:

以下是我正在使用的代码:

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

但它引发了以下异常:

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'

3 个答案:

答案 0 :(得分:1)

您可以滥用resize()功能:

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

resize()函数将创建新图像,其大小为原始图像的一半。

INTER_NEAREST意味着小图像的值将通过“最近邻居”方法计算。在这种特殊情况下,它意味着小图像中位置(1,2)处的像素值将取自大图像中位置(2,4)的像素。

答案 1 :(得分:0)

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

答案 2 :(得分:-2)

我终于可以做到了。这是解决方案

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