我试图在我的图像区域上进行卷积,只需一个像素,就更准确了。我还希望在计算此ROI的边界时,此卷积使用ROI之外的值。为了测试这个,我写了下面的代码。
float kernelData[25] = {0, 0, 0, 0, 0,
0, 0, 1, 0, 0,
0, 0, 1, 0, 0,
0, 0, 0, 0, 0,
0, 0, 0, 0, 0};
float imgData[9*9] = {0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 2, 256, 128, 0, 0, 0,
0, 0, 0, 4, 1, 64, 0, 0, 0,
0, 0, 0, 8, 16, 32, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0};
Mat img = Mat(9, 9, CV_32FC1, (void*)imgData);
Mat kernel = Mat(5, 5, CV_32FC1, (void*)kernelData);
Mat window = Mat(img, Rect2i(Point2i(4, 4), Size(1, 1)));
Mat convolved1 = Mat(1, 1, CV_32FC1);
Mat convolved2 = Mat(1, 1, CV_32FC1);
Mat convolved3 = Mat(1, 1, CV_32FC1);
Mat convolved4 = Mat(1, 1, CV_32FC1);
Mat convolved5 = Mat(1, 1, CV_32FC1);
cv::filter2D(window, convolved1, CV_32F, kernel, Point(-1, -1), 0, BORDER_CONSTANT);
cv::filter2D(window, convolved2, CV_32F, kernel, Point(-1, -1), 0, BORDER_REPLICATE);
cv::filter2D(window, convolved3, CV_32F, kernel, Point(-1, -1), 0, BORDER_REFLECT);
cv::filter2D(window, convolved4, CV_32F, kernel, Point(-1, -1), 0, BORDER_REFLECT_101);
cv::filter2D(window, convolved5, CV_32F, kernel, Point(-1, -1), 0, BORDER_TRANSPARENT);
cout << img << endl;
cout << kernel << endl;
cout << window << endl;
cout << convolved1 << endl;
cout << convolved2 << endl;
cout << convolved3 << endl;
cout << convolved4 << endl;
cout << convolved5 << endl;
此代码的执行显示了以下结果:
[0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 2, 256, 128, 0, 0, 0;
0, 0, 0, 4, 1, 64, 0, 0, 0;
0, 0, 0, 8, 16, 32, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0;
0, 0, 1, 0, 0;
0, 0, 1, 0, 0;
0, 0, 0, 0, 0;
0, 0, 0, 0, 0]
[1]
[5]
[5]
[5]
[5]
[5]
我测试了所有边框类型,但没有一个得到正确的结果,257。 我也尝试修改内核,改变1和0的位置,我得到的结果总是错的。
有趣的是,这个相同的代码在我的笔记本上正确运行,但在我的桌面上却没有。
为什么会这样?