我在opencv c ++中实现了Filter2d的内置函数,我想在没有内置函数的情况下实现它,但我不能。这是我到目前为止所尝试过的。
int main(int argc, char** argv)
{
/// Declare variables
Mat src, dst, dsty;
Mat kernelx, kernely;
Point anchor;
double delta;
int ddepth;
int kernel_size;
namedWindow("filter2D", CV_WINDOW_AUTOSIZE);
src = imread("C:/Users/cvi/Desktop/2.jpg");
/// Initialize arguments for the filter
anchor = Point(-1, -1);
delta = 0;
ddepth = -1;
kernel_size = 2;
kernelx = Mat::ones(kernel_size, kernel_size, CV_32F) /
(float)(kernel_size*kernel_size);
kernely = Mat::ones(kernel_size, kernel_size, CV_32F) /
(float)(kernel_size*kernel_size);
kernelx.at<float>(0, 0) = -1;
kernely.at<float>(0, 0) = 1;
for (int i = 0; i < src.rows; ++i) {
for (int j = 0; j < src.cols; ++j) {
int tmp, sum;
for (int m = 0; m < kernelx.rows; ++m) {
for (int n = 0; n < kernely.cols; ++n) {
tmp = src[i + m - (kernel_size - 1) / 2][j + n - (kernel_size - 1) / 2];
float kernel_index = kernelx[m][n];
float index_mult = (float)index_mult * kernel_index;
sum = sum + index_mult;
}
}
imshow("filter2D Change in X", dst);
imshow("filter2D Change in Y", dsty);
}
}
cvWaitKey(0);
return 0;
}