Also habe ich ein Programm, das versucht, eine einfache 3x3 Faltungsmatrix auf ein Bild anzuwenden.Opencv Faltungsmatrix gibt ungewöhnliche Ergebnisse
Dies ist die Funktion, die die Arbeit tut:
Mat process(Mat image) {
int x = 2;
int y = 2;
Mat nimage(image); //just a new mat to put the resulting image on
while (y < image.rows-2) {
while (x < image.cols-2) {
nimage.at<uchar>(y,x) = //apply matrix to pixel
image.at<char>(y-1,x-1)*matrix[0]+
image.at<char>(y-1,x)*matrix[1]+
image.at<char>(y-1,x+1)*matrix[2]+
image.at<char>(y,x-1)*matrix[3]+
image.at<char>(y,x)*matrix[4]+
image.at<char>(y,x+1)*matrix[5]+
image.at<char>(y+1,x-1)*matrix[6]+
image.at<char>(y+1,x)*matrix[7]+
image.at<char>(y+1,x+1)*matrix[8];
//if (total < 0) total = 0;
//if (total > 255) total = 255;
//cout << (int)total << ": " << x << "," << y << endl;
x++;
}
x = 0;
y++;
}
cout << "done" << endl;
return nimage;
}
Und die Matrix sieht dann so aus wie diese
double ar[9] = {-1,0,0,
0,2,0,
0,0,0};
Und das Bild, das als Eingabe verwendet wird, sieht:
Die gewünschte Ausgabe (Ich lief die gleiche Matrix auf das Eingangsbild in GIMP):
Und das Ergebnis ist ... seltsam:
Ich denke, das mit dem Datentyp zu tun hat, ich benutze, wenn ich eine Set Pixel des neuen Bildes (nimage.at<uchar>(y,x) = ...
), denn wenn ich es ändere, bekomme ich ein anderes, aber immer noch falsches Ergebnis.
Warum nicht Standard Faltung fonctions in opencv verfügbar verwenden? – tfv