Opencv 卷积矩阵给出不寻常的结果
Opencv convolution matrix gives unusual results
所以我有一个程序试图将一个简单的 3x3 卷积矩阵应用于图像。
这是正在执行工作的函数:
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) = ...
) 的像素时使用的数据类型有关,因为每当我更改它时,我都会得到不同但仍然不正确的结果。
来自关于 Mat
的复制构造函数的 OpenCV 文档,强调我的:
m – Array that (as a whole or partly) is assigned to the constructed matrix. No data is copied by these constructors. Instead, the header pointing to m data or its sub-array is constructed and associated with it. The reference counter, if any, is incremented. So, when you modify the matrix formed using such a constructor, you also modify the corresponding elements of m
. If you want to have an independent copy of the sub-array, use Mat::clone()
.
所以
Mat nimage(image); //just a new mat to put the resulting image on
实际上并没有创建新矩阵;它创建了一个新的 Mat
对象,但该对象仍然引用同一个矩阵。从那时起 nimage.at(y,x)
就像 image.at(y,x)
.
要复制图像,请使用
Mat nimage(image.clone()); //just a new mat to put the resulting image on
所以我有一个程序试图将一个简单的 3x3 卷积矩阵应用于图像。
这是正在执行工作的函数:
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) = ...
) 的像素时使用的数据类型有关,因为每当我更改它时,我都会得到不同但仍然不正确的结果。
来自关于 Mat
的复制构造函数的 OpenCV 文档,强调我的:
m – Array that (as a whole or partly) is assigned to the constructed matrix. No data is copied by these constructors. Instead, the header pointing to m data or its sub-array is constructed and associated with it. The reference counter, if any, is incremented. So, when you modify the matrix formed using such a constructor, you also modify the corresponding elements of
m
. If you want to have an independent copy of the sub-array, useMat::clone()
.
所以
Mat nimage(image); //just a new mat to put the resulting image on
实际上并没有创建新矩阵;它创建了一个新的 Mat
对象,但该对象仍然引用同一个矩阵。从那时起 nimage.at(y,x)
就像 image.at(y,x)
.
要复制图像,请使用
Mat nimage(image.clone()); //just a new mat to put the resulting image on