【发布时间】:2014-09-05 09:36:38
【问题描述】:
您好,我必须使用以下内核对我的 32 位原始图像执行 2D 卷积
h(x,y)= a(b* exp^(-squareroot(x^2+y^2))
我不知道如何执行它,因为我是编码新手。我的图像尺寸是 1024*768。我应该保持相同大小的内核并执行卷积还是应该保持一个小内核?他们俩会有所作为吗?如果我保留一个小内核,我如何将它与整个图像进行卷积?
请帮忙
请检查生成内核的代码是否正确
感谢两位的回答。请您在下面查看生成内核和卷积的代码。我不确定我是否做得对
int krowhalf=krow/2,kcolhalf=kcol/2;
// sum is for normalization
float sum = 0.0;
// generate kernel
for (int x = -krowhalf; x <= krowhalf; x++)
{
for(int y = -kcolhalf; y <= kcolhalf; y++)
{
r = sqrtl(x*x + y*y);
gKernel[x + krowhalf][y + kcolhalf] = a*(b*exp(-(r));
sum += gKernel[x + krowhalf][y + kcolhalf];
}
}
//normalize the Kernel
for(int i = 0; i < krow; ++i)
for(int j = 0; j < kcol; ++j)
gKernel[i][j] /= sum;
float **convolve2D(float** in, float** out, int h, int v, float **kernel, int kCols, int kRows)
{
int kCenterX = kCols / 2;
int kCenterY = kRows / 2;
int i,j,m,mm,n,nn,ii,jj;
for(i=0; i < h; ++i)
// rows
{
for(j=0; j < v; ++j)
// columns
{
for(m=0; m < kRows; ++m) // kernel rows
{
mm = kRows - 1 - m; // row index of flipped kernel
for(n=0; n < kCols; ++n) // kernel columns
{
nn = kCols - 1 - n; // column index of flipped kernel
//index of input signal, used for checking boundary
ii = i + (m - kCenterY);
jj = j + (n - kCenterX);
// ignore input samples which are out of bound
if( ii >= 0 && ii < h && jj >= 0 && jj < v )
//out[i][j] += in[ii][jj] * (kernel[mm+nn*29]);
out[i][j] += in[ii][jj] * (kernel[mm][nn]);
}
}
}
}
返回; }
【问题讨论】:
-
你为什么有
exp(-r*r)?您写道,您在第一部分中取平方根而不是平方。除此之外,大多数情况下看起来都是正确的方法(没有检查所有内容) -
很抱歉,我只是在尝试使用基本的高斯卷积。如果没问题,你能检查一下卷积部分吗?我可以用这种方法使用一个小内核让我们说 3*3 吗?
-
它应该可以工作,小心检查你的尺寸是奇数而不是偶数,否则你会产生一些段错误。我假设您的数组已正确完成,因为您没有显示声明。纯粹出于性能考虑,将一维数组用于内核会更有效,因为您无法确定编译器是否会对其进行优化
-
好的,非常感谢。它对你很有帮助。我注意到你所有的建议。
标签: c image-processing convolution