如果我将输入图像和内核设置为:
cv::Mat conv2D(cv::Mat input,cv::Mat kernel){
printf("\nINPUT [ %d x %d ]\n",input.rows,input.cols);
cout << input <<endl;
printf("\nKERNEL [ %d x %d ]\n",kernel.rows,kernel.cols);
cout << kernel <<endl;
cv::Mat flipped_kernel;
cv::Point2i pad;
cv::Mat result;
cv::Mat padded;
cv::Point anchor(kernel.cols -kernel.cols/2 -1,kernel.rows -kernel.rows/2 -1 );
cv::flip( kernel, flipped_kernel, -1 );
printf("\nFLIPPED KERNEL [ %d x %d ]\n",flipped_kernel.rows,flipped_kernel.cols);
cout << flipped_kernel <<endl;
pad = cv::Point2i( kernel.cols - 1, kernel.rows - 1);
cv::copyMakeBorder( input, padded, pad.y, pad.y, pad.x, pad.x, cv::BORDER_CONSTANT,cv::Scalar(0) );
cv::Rect region = cv::Rect( pad.x / 2, pad.y / 2, padded.cols - pad.x, padded.rows - pad.y);
cv::filter2D( padded, result , padded.depth(), flipped_kernel, anchor, 0, cv::BORDER_CONSTANT );
cv::Mat out = result( region );
printf("\nOUT [ %d x %d ]\n",out.rows,out.cols);
cout << out <<endl;
return out;
}
输出不同于:
conv2(input,kernel)
在网络上阅读时,有人说翻转是错误,而其他人则不是。所以我使用对称旋转矩阵作为内核,结果仍然是错误的。我如何在c ++中执行2d卷积和2d互相关?
答案 0 :(得分:0)
问题在于我通过了CV_32F类型的opencv垫,这给了我错误的结果,我使用以下方法将图像转换为CV_8UC1:
input.convertTo(input,CV_8UC1); flipped_kernel.convertTo(input,CV_8UC1);
并在操作后更改输出类型:
out.convertTo(input,CV_32F);
因此该方法有效!