Sie können die nutzlosen Zeilen und Spalten, und die Arbeit auf einer Matrix mit der halben Größe der ursprünglichen Matrix entfernen.
du resize
Funktion, mit nächsten Interpolation tun kann leicht mit:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char **argv)
{
Mat1b mat = (Mat1b(4,4) << 0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11,
12, 13, 14, 15);
Mat1b res;
resize(mat, res, Size(0, 0), 0.5, 0.5, INTER_NEAREST);
cout << "Mat:" << endl << mat << endl << endl;
cout << "Res:" << endl << res << endl;
return 0;
}
Dann werden die Werte in res
sind nur die Werte an den Indizes Sie benötigen:
Mat:
[0, 1, 2, 3;
4, 5, 6, 7;
8, 9, 10, 11;
12, 13, 14, 15]
Res:
[0, 2;
8, 10]
Um die Werte wieder auf die ursprüngliche Position zu bringen, können Sie das Kronecker-Produkt (nicht in OpenCV verfügbar, aber easily implemented) mit einem Suit verwenden ble Muster. Dies erzeugt:
Mat:
[0, 1, 2, 3;
4, 5, 6, 7;
8, 9, 10, 11;
12, 13, 14, 15]
Res:
[0, 2;
8, 10]
Res Modified:
[1, 3;
9, 11]
Restored:
[1, 0, 3, 0;
0, 0, 0, 0;
9, 0, 11, 0;
0, 0, 0, 0]
Code:
#include <opencv2/opencv.hpp>
#include <algorithm>
#include <iostream>
using namespace cv;
using namespace std;
Mat kron(const Mat A, const Mat B)
{
CV_Assert(A.channels() == 1 && B.channels() == 1);
Mat1d Ad, Bd;
A.convertTo(Ad, CV_64F);
B.convertTo(Bd, CV_64F);
Mat1d Kd(Ad.rows * Bd.rows, Ad.cols * Bd.cols, 0.0);
for (int ra = 0; ra < Ad.rows; ++ra)
{
for (int ca = 0; ca < Ad.cols; ++ca)
{
Kd(Range(ra*Bd.rows, (ra + 1)*Bd.rows), Range(ca*Bd.cols, (ca + 1)*Bd.cols)) = Bd.mul(Ad(ra, ca));
}
}
Mat K;
Kd.convertTo(K, A.type());
return K;
}
int main(int argc, char **argv)
{
Mat1b mat = (Mat1b(4, 4) << 0, 1, 2, 3,
4, 5, 6, 7,
8, 9, 10, 11,
12, 13, 14, 15);
Mat1b res;
resize(mat, res, Size(0, 0), 0.5, 0.5, INTER_NEAREST);
cout << "Mat:" << endl << mat << endl << endl;
cout << "Res:" << endl << res << endl << endl;
// Work on Res
res += 1;
cout << "Res Modified:" << endl << res << endl << endl;
// Define the pattern
Mat1b pattern = (Mat1b(2,2) << 1, 0,
0, 0);
// Apply Kronecker product
Mat1b restored = kron(res, pattern);
cout << "Restored:" << endl << restored << endl << endl;
return 0;
}
macht mich fragen, ob dies die OP Absicht in erster Linie war? Ein bisschen Wheel Re-Erfindung verletzt nie jemand :) –
Vielen Dank. Dies ist eine sehr gute Lösung. Wenn ich irgendwie in der Lage wäre, eine 4 * 4-Matrix zu erhalten, bei der nur die Indizes [0,2; 8,10] nicht Null sind, ist der Rest null. z.B. Nach anderen Operationen 'res = [1, 9; 5, 7]; 'Ich möchte diese Werte in ihren Positionen in 4 * 4-Matrix bekommen und der Rest ist Null. Die letzte Matrix wird "[1,0,9,0; 0,0,0,0; 5,0,7,0; 0,0,0,0] '. Dies wird sicherlich beschleunigen, aber ich bin ein bisschen gierig :) – smttsp
@sm Ich verstehe es nicht, können Sie bitte, bitte? – Miki