openCV2.3中卷积方法高斯模糊的错误
我编写使用 openCV api 创建高斯内核,然后将其传递给 Conv2ByDFT 函数进行卷积。但程序崩溃了,我不知道为什么。这是代码。
void Conv2ByFFT(const Mat& f,const Mat& g,Mat& result)
{
result.create(abs(f.rows-g.rows+1),abs(f.cols-g.cols+1),f.type());
Size dftSize;
dftSize.width = getOptimalDFTSize(f.cols + g.cols - 1);
dftSize.height = getOptimalDFTSize(f.rows + g.cols -1);
Mat tmpF(dftSize,f.type(),Scalar::all(0));
Mat tmpG(dftSize,g.type(),Scalar::all(0));
dft(tmpF,tmpF,0,f.rows);
dft(tmpG,tmpG,0,g.rows);
mulSpectrums(tmpF,tmpG,tmpF,0);
dft(tmpF,tmpF,DFT_INVERSE+DFT_SCALE,result.rows);
tmpF(Rect(0,0,result.cols,result.rows)).copyTo(result);
}
这是 main() 中调用上面函数的一些代码,
Mat gaussianFilter = getGaussianKernel(7,2.0,CV_64F); // create Gaussian kernel
Conv2ByFFT(src,gaussianFilter,result); // do the convolution
我不知道 getGaussianKernel() 函数是否有问题,或者我的 Conv2ByFFT() 函数有问题......任何人都可以帮助我吗?多谢!
i write use openCV api to create a Gaussian kernel and then pass it to the Conv2ByDFT function to do the convolution. But the program crash and i don't know why. Here is the code.
void Conv2ByFFT(const Mat& f,const Mat& g,Mat& result)
{
result.create(abs(f.rows-g.rows+1),abs(f.cols-g.cols+1),f.type());
Size dftSize;
dftSize.width = getOptimalDFTSize(f.cols + g.cols - 1);
dftSize.height = getOptimalDFTSize(f.rows + g.cols -1);
Mat tmpF(dftSize,f.type(),Scalar::all(0));
Mat tmpG(dftSize,g.type(),Scalar::all(0));
dft(tmpF,tmpF,0,f.rows);
dft(tmpG,tmpG,0,g.rows);
mulSpectrums(tmpF,tmpG,tmpF,0);
dft(tmpF,tmpF,DFT_INVERSE+DFT_SCALE,result.rows);
tmpF(Rect(0,0,result.cols,result.rows)).copyTo(result);
}
Here is some code in the main() to call the function above
Mat gaussianFilter = getGaussianKernel(7,2.0,CV_64F); // create Gaussian kernel
Conv2ByFFT(src,gaussianFilter,result); // do the convolution
I don't know if there's something wrong with the getGaussianKernel() function or something wrong with my Conv2ByFFT() function...Can anyone help me? THANKS A LOT!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的猜测是您的 src 矩阵不是 CV_32F 或 CV_64F(32 或 64 位浮点)格式。如果它是图像,您可能需要将其转换。
OpenCV文档提醒:
矩阵元素的类型以CV_(S|U|F)C形式指定,例如:CV_8UC1表示8位无符号单通道矩阵,CV_32SC2表示32位有符号矩阵
有两个通道。
以下是源定义的完整列表:
My guess is that your
src
matrix is not in CV_32F or CV_64F (32 or 64 bit floating point) format. If it is an image, you might need to convert it.Reminder from OpenCV documentation:
The type of matrix elements is specified in form CV_(S|U|F)C, for example: CV_8UC1 means an 8-bit unsigned single-channel matrix, CV_32SC2 means a 32-bit signed matrix
with two channels.
Here is a complete list of defines from the source: