1 | initial version |
opencv has nice row(i) col(i) functions, you can use those to build a new Mat from another one:
// this is only used for the easy initialization in this demo,
// usually you will have a plain Mat A:
Mat_<float> A(3,6);
A << 1,2,3,4,5,6,
1,2,3,4,5,6,
1,2,3,4,5,6;
// now we can start collecting the even cols of A,
// unfortunately opencv insists on pushing into *rows* here,
// so we will need to transpose the result later..
Mat B;
for (int i=0; i<A.cols; i+=2)
{
B.push_back(A.col(i));
}
// now we got 1 large 1d flat array with all the collected elements,
// let's make a 3x3 Mat of it again:
B = B.reshape(1,3); // 1 elem per channel, 3 rows.
B = B.t(); // transpose it
cerr << B << endl;
[1, 3, 5;
1, 3, 5;
1, 3, 5]
// here's the same thing for rows(no transpose needed):
Mat_<float> A2(6,3);
A2 << 1,1,1,
2,2,2,
3,3,3,
4,4,4,
5,5,5,
6,6,6;
Mat B2;
for (int i=0; i<A2.rows; i+=2)
{
B2.push_back(A2.row(i));
}
cerr << B2.reshape(1,3) << endl;
[1, 1, 1;
3, 3, 3;
5, 5, 5]
2 | No.2 Revision |
opencv has nice row(i) col(i) functions, you can use those to build a new Mat from another one:
// this is only used for the easy initialization in this demo,
// usually you will have a plain Mat A:
Mat_<float> A(3,6);
A << 1,2,3,4,5,6,
1,2,3,4,5,6,
1,2,3,4,5,6;
// now we can start collecting the even cols of A,
// unfortunately opencv insists on pushing into *rows* here,
// so we will need to transpose the result later..
Mat B;
for (int i=0; i<A.cols; i+=2)
{
B.push_back(A.col(i));
}
// now we got 1 large 1d flat (column) array with all the collected elements,
// let's make a 3x3 Mat of it again:
B = B.reshape(1,3); // 1 elem per channel, 3 rows.
B = B.t(); // transpose it
cerr << B << endl;
[1, 3, 5;
1, 3, 5;
1, 3, 5]
// here's the same thing for rows(no transpose needed):
Mat_<float> A2(6,3);
A2 << 1,1,1,
2,2,2,
3,3,3,
4,4,4,
5,5,5,
6,6,6;
Mat B2;
for (int i=0; i<A2.rows; i+=2)
{
B2.push_back(A2.row(i));
}
// also, since we were pushing row vectors already, we can skip the reshape:
cerr << B2.reshape(1,3) << B2<< endl;
[1, 1, 1;
3, 3, 3;
5, 5, 5]
3 | No.3 Revision |
opencv has nice row(i) col(i) functions, you can use those to build a new Mat from another one:
// this is only used for the easy initialization in this demo,
// usually you will have a plain Mat A:
Mat_<float> A(3,6);
A << 1,2,3,4,5,6,
1,2,3,4,5,6,
1,2,3,4,5,6;
// now we can start collecting the even cols of A,
// unfortunately opencv insists on pushing into *rows* here,
// so we will need to transpose the result later..
Mat B;
for (int i=0; i<A.cols; i+=2)
{
B.push_back(A.col(i));
}
// now we got 1 large 1d flat (column) array with all the collected elements,
// let's make a 3x3 Mat of it again:
B = B.reshape(1,3); // 1 elem per channel, B.reshape(-1,3); // keep channel count, but rearrange to 3 rows.
B = B.t(); // transpose it
cerr << B << endl;
[1, 3, 5;
1, 3, 5;
1, 3, 5]
// here's the same thing for rows(no transpose needed):
Mat_<float> A2(6,3);
A2 << 1,1,1,
2,2,2,
3,3,3,
4,4,4,
5,5,5,
6,6,6;
Mat B2;
for (int i=0; i<A2.rows; i+=2)
{
B2.push_back(A2.row(i));
}
// also, since we were pushing row vectors already, we can skip the reshape:
cerr << B2<< endl;
[1, 1, 1;
3, 3, 3;
5, 5, 5]
4 | No.4 Revision |
opencv has nice row(i) col(i) functions, you can use those to build a new Mat from another one:
// this is only used for the easy initialization in this demo,
// usually you will have a plain Mat A:
Mat_<float> A(3,6);
A << 1,2,3,4,5,6,
1,2,3,4,5,6,
1,2,3,4,5,6;
// now we can start collecting the even cols of A,
// unfortunately opencv insists on pushing into *rows* here,
// so we will need to transpose the result later..
Mat B;
for (int i=0; i<A.cols; i+=2)
{
B.push_back(A.col(i));
}
// now we got 1 large 1d flat (column) array with all the collected elements,
// let's make a 3x3 Mat of it again:
B = B.reshape(-1,3); B.reshape(0,3); // keep channel count, but rearrange to 3 rows.
B = B.t(); // transpose it
cerr << B << endl;
[1, 3, 5;
1, 3, 5;
1, 3, 5]
// here's the same thing for rows(no transpose needed):
Mat_<float> A2(6,3);
A2 << 1,1,1,
2,2,2,
3,3,3,
4,4,4,
5,5,5,
6,6,6;
Mat B2;
for (int i=0; i<A2.rows; i+=2)
{
B2.push_back(A2.row(i));
}
// also, since we were pushing row vectors already, we can skip the reshape:
cerr << B2<< endl;
[1, 1, 1;
3, 3, 3;
5, 5, 5]
5 | No.5 Revision |
opencv has nice row(i) col(i) functions, you can use those to build a new Mat from another one:
// this is only used for the easy initialization in this demo,
// usually you will have a plain Mat A:
Mat_<float> A(3,6);
A << 1,2,3,4,5,6,
1,2,3,4,5,6,
1,2,3,4,5,6;
// now we can start collecting the even cols of A,
// unfortunately opencv insists on pushing into *rows* here,
// so we will need to transpose the result later..
Mat B;
for (int i=0; i<A.cols; i+=2)
{
B.push_back(A.col(i));
}
// now we got 1 large 1d flat (column) array with all the collected elements,
// let's make a 3x3 Mat of it again:
B = B.reshape(0,3); B.reshape(0,A.cols/2); // keep channel count, but rearrange to 3 rows.
half the cols we had before.
B = B.t(); // transpose it
cerr << B << endl;
[1, 3, 5;
1, 3, 5;
1, 3, 5]
// here's the same thing for rows(no transpose needed):
Mat_<float> A2(6,3);
A2 << 1,1,1,
2,2,2,
3,3,3,
4,4,4,
5,5,5,
6,6,6;
Mat B2;
for (int i=0; i<A2.rows; i+=2)
{
B2.push_back(A2.row(i));
}
// also, since we were pushing row vectors already, we can skip the reshape:
cerr << B2<< endl;
[1, 1, 1;
3, 3, 3;
5, 5, 5]