OpenCV 获取 Mat 偶数行的副本
OpenCV get a copy of even rows of a Mat
我想获得 3 个通道的偶数 rows/cols,如下所示:
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'
int j = 0;
for (int i = 0; i< A.size(); i+2)
{
destMat.row(j) = (A.row(i));
j++;
}
我终于可以做到了。这是解决方案
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));
}
您可以滥用 resize() 功能:
resize(bigImage, smallImage, Size(), 0.5, 0.5, INTER_NEAREST);
resize() 函数将创建新图像,其大小是原始图像的一半。
INTER_NEAREST表示小图的值将按"nearest neighbor"的方式计算。在这种特定情况下,这意味着小图像中位置 (1,2) 处的像素值将从大图像中位置 (2,4) 处的像素中获取。
我想获得 3 个通道的偶数 rows/cols,如下所示:
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'
int j = 0;
for (int i = 0; i< A.size(); i+2)
{
destMat.row(j) = (A.row(i));
j++;
}
我终于可以做到了。这是解决方案
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));
}
您可以滥用 resize() 功能:
resize(bigImage, smallImage, Size(), 0.5, 0.5, INTER_NEAREST);
resize() 函数将创建新图像,其大小是原始图像的一半。
INTER_NEAREST表示小图的值将按"nearest neighbor"的方式计算。在这种特定情况下,这意味着小图像中位置 (1,2) 处的像素值将从大图像中位置 (2,4) 处的像素中获取。