openCV2.3中卷积方法高斯模糊的错误

发布于 2024-12-06 22:41:06 字数 931 浏览 1 评论 0原文

我编写使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一花一树开 2024-12-13 22:41:06

我的猜测是您的 src 矩阵不是 CV_32F 或 CV_64F(32 或 64 位浮点)格式。如果它是图像,您可能需要将其转换。

OpenCV文档提醒:

矩阵元素的类型以CV_(S|U|F)C形式指定,例如:CV_8UC1表示8位无符号单通道矩阵,CV_32SC2表示32位有符号矩阵
有两个通道。

以下是源定义的完整列表:

#define     CV_16S   3
#define     CV_16SC(n)   CV_MAKETYPE(CV_16S,(n))
#define     CV_16SC1   CV_MAKETYPE(CV_16S,1)
#define     CV_16SC2   CV_MAKETYPE(CV_16S,2)
#define     CV_16SC3   CV_MAKETYPE(CV_16S,3)
#define     CV_16SC4   CV_MAKETYPE(CV_16S,4)
#define     CV_16U   2
#define     CV_16UC(n)   CV_MAKETYPE(CV_16U,(n))
#define     CV_16UC1   CV_MAKETYPE(CV_16U,1)
#define     CV_16UC2   CV_MAKETYPE(CV_16U,2)
#define     CV_16UC3   CV_MAKETYPE(CV_16U,3)
#define     CV_16UC4   CV_MAKETYPE(CV_16U,4)
#define     CV_32F   5
#define     CV_32FC(n)   CV_MAKETYPE(CV_32F,(n))
#define     CV_32FC1   CV_MAKETYPE(CV_32F,1)
#define     CV_32FC2   CV_MAKETYPE(CV_32F,2)
#define     CV_32FC3   CV_MAKETYPE(CV_32F,3)
#define     CV_32FC4   CV_MAKETYPE(CV_32F,4)
#define     CV_32S   4
#define     CV_32SC(n)   CV_MAKETYPE(CV_32S,(n))
#define     CV_32SC1   CV_MAKETYPE(CV_32S,1)
#define     CV_32SC2   CV_MAKETYPE(CV_32S,2)
#define     CV_32SC3   CV_MAKETYPE(CV_32S,3)
#define     CV_32SC4   CV_MAKETYPE(CV_32S,4)
#define     CV_64F   6
#define     CV_64FC(n)   CV_MAKETYPE(CV_64F,(n))
#define     CV_64FC1   CV_MAKETYPE(CV_64F,1)
#define     CV_64FC2   CV_MAKETYPE(CV_64F,2)
#define     CV_64FC3   CV_MAKETYPE(CV_64F,3)
#define     CV_64FC4   CV_MAKETYPE(CV_64F,4)
#define     CV_8S   1
#define     CV_8SC(n)   CV_MAKETYPE(CV_8S,(n))
#define     CV_8SC1   CV_MAKETYPE(CV_8S,1)
#define     CV_8SC2   CV_MAKETYPE(CV_8S,2)
#define     CV_8SC3   CV_MAKETYPE(CV_8S,3)
#define     CV_8SC4   CV_MAKETYPE(CV_8S,4)
#define     CV_8U   0
#define     CV_8UC(n)   CV_MAKETYPE(CV_8U,(n))
#define     CV_8UC1   CV_MAKETYPE(CV_8U,1)
#define     CV_8UC2   CV_MAKETYPE(CV_8U,2)
#define     CV_8UC3   CV_MAKETYPE(CV_8U,3)
#define     CV_8UC4   CV_MAKETYPE(CV_8U,4)

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:

#define     CV_16S   3
#define     CV_16SC(n)   CV_MAKETYPE(CV_16S,(n))
#define     CV_16SC1   CV_MAKETYPE(CV_16S,1)
#define     CV_16SC2   CV_MAKETYPE(CV_16S,2)
#define     CV_16SC3   CV_MAKETYPE(CV_16S,3)
#define     CV_16SC4   CV_MAKETYPE(CV_16S,4)
#define     CV_16U   2
#define     CV_16UC(n)   CV_MAKETYPE(CV_16U,(n))
#define     CV_16UC1   CV_MAKETYPE(CV_16U,1)
#define     CV_16UC2   CV_MAKETYPE(CV_16U,2)
#define     CV_16UC3   CV_MAKETYPE(CV_16U,3)
#define     CV_16UC4   CV_MAKETYPE(CV_16U,4)
#define     CV_32F   5
#define     CV_32FC(n)   CV_MAKETYPE(CV_32F,(n))
#define     CV_32FC1   CV_MAKETYPE(CV_32F,1)
#define     CV_32FC2   CV_MAKETYPE(CV_32F,2)
#define     CV_32FC3   CV_MAKETYPE(CV_32F,3)
#define     CV_32FC4   CV_MAKETYPE(CV_32F,4)
#define     CV_32S   4
#define     CV_32SC(n)   CV_MAKETYPE(CV_32S,(n))
#define     CV_32SC1   CV_MAKETYPE(CV_32S,1)
#define     CV_32SC2   CV_MAKETYPE(CV_32S,2)
#define     CV_32SC3   CV_MAKETYPE(CV_32S,3)
#define     CV_32SC4   CV_MAKETYPE(CV_32S,4)
#define     CV_64F   6
#define     CV_64FC(n)   CV_MAKETYPE(CV_64F,(n))
#define     CV_64FC1   CV_MAKETYPE(CV_64F,1)
#define     CV_64FC2   CV_MAKETYPE(CV_64F,2)
#define     CV_64FC3   CV_MAKETYPE(CV_64F,3)
#define     CV_64FC4   CV_MAKETYPE(CV_64F,4)
#define     CV_8S   1
#define     CV_8SC(n)   CV_MAKETYPE(CV_8S,(n))
#define     CV_8SC1   CV_MAKETYPE(CV_8S,1)
#define     CV_8SC2   CV_MAKETYPE(CV_8S,2)
#define     CV_8SC3   CV_MAKETYPE(CV_8S,3)
#define     CV_8SC4   CV_MAKETYPE(CV_8S,4)
#define     CV_8U   0
#define     CV_8UC(n)   CV_MAKETYPE(CV_8U,(n))
#define     CV_8UC1   CV_MAKETYPE(CV_8U,1)
#define     CV_8UC2   CV_MAKETYPE(CV_8U,2)
#define     CV_8UC3   CV_MAKETYPE(CV_8U,3)
#define     CV_8UC4   CV_MAKETYPE(CV_8U,4)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文