2015-01-12 89 views
0
的偶数行的副本

我想获得3个通道的垫子,这样的东西的偶数行/的cols:的OpenCV获得垫

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' 

回答

-1

我终于可以做到这一点。下面是解

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)); 
} 
0
int j = 0; 
for (int i = 0; i< A.size(); i+2) 
{ 
    destMat.row(j) = (A.row(i)); 
    j++; 
} 
+0

j的效用是什么?以及为什么我们将0添加到A.row(i)? – Maystro

+0

不知道+0,我把它从别的东西上扯下来。 J在代码更新中反映出来。这是为索引destMat的行,因为你不想增加2 – GPPK

+0

编辑的问题 – Maystro

1

可以滥用resize()功能:

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

调整大小()函数将创建一个新的形象,其大小是原始图像的一半。

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

+0

我不想改变图像中的任何东西,我只是想要图像中的偶数行/列。 – Maystro

+0

这是调整大小。它创建的图像只有偶数行和原始图像的列。当然,这是滥用调整大小功能,因为这不是它的原始目的,但如果这样做的伎俩... –

+0

我试过了,它的工作。由于它比我的解决方案更容易,我会接受你的答案而不是我的答案。 – Maystro