所以我有一個程序試圖將一個簡單的3x3卷積矩陣應用於圖像。Opencv卷積矩陣給出了不尋常的結果
這是正在做工作中的作用:
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;
}
和矩陣看起來像這樣
double ar[9] = {-1,0,0,
0,2,0,
0,0,0};
,這是用作輸入的圖像看起來是這樣的:
期望的輸出(我跑了相同的矩陣上在GIMP輸入圖像):
,其結果是...奇怪:
我認爲這與當我設置一個我使用的數據類型做新圖像的像素(nimage.at<uchar>(y,x) = ...
),因爲無論何時我改變它,我都會得到一個不同但仍然不正確的結果。
你爲什麼不使用標準的卷積fonctions提供的OpenCV? – tfv