高斯拉普拉斯算子 [英] Laplacian of Gaussian
本文介绍了高斯拉普拉斯算子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我很难实现拉普拉斯高斯核。我有以下代码,我正在尝试使用sigma = 1.4实现9x9内核。内核显示在此链接上
http://homepages.inf.ed.ac.uk/rbf/HIPR2/log.htm [ ^ ]
但是,我的值与那个内核中的值不同,我认为我的功能已关闭。非常感谢帮助。谢谢。
const Int32 N = 9; //,Nh = N / 2;
float [,] MASK = new float [N,N];
int min =(int)Math.Ceiling((double)-N /(double)2);
int max =(int)Math.Floor((double)N /(double)2);
float sigma = 1.4f;
float s2 = sigma * sigma;
float s4 = sigma * sigma * sigma * sigma;
int xCount = 0;
for(int x = min; x< = max; ++ x)
{
int yCount = 0;
for(int y = min; y< = max; ++ y)
{
float val =(float)( - 1.0f /(Math.PI *(s4)) )*(1.0f - ((x * x + y * y)/(2.0f *(s2))))*(float)Math.Exp(( - (x * x + y * y)/(2.0f *(s2))));
MASK [xCount,yCount] = val;
++ yCount;
}
++ xCount;
}
解决方案
对不起,我不能直接帮你解决问题,但是当我遇到问题时像这样复杂的数学变换我在一个非常小的简单测试数据样本集上测试我的代码。
有时我会在Excel(或纸上)中重现方程式,并将相同的测试数据输入我的代码并输入测试电子表格。如果它仍然不相关我将方程式向下打破,以便代码超过几行,然后逐步检查每行代码的结果,直到找到我的电子表格数据和我的程序结果之间的差异。
有时错误是一个简单的错字,它们很难被发现!
祝你好运,:) / blockquote>
我认为你非常接近。在执行实际卷积时,您将期望恒定输入(例如始终为1)在输出处产生相同的常量值。公式(和你的代码)没有考虑到这一点,你需要一个大约480的修正系数。
当然,他们已经显示了一个全部 - 整数近似。通常会发生这种情况我想:
- 选择修正因子;
- 将所有数字四舍五入为整数;
- 检查总和等于N * N;
- 迭代上面的内容(并在舍入时稍微调整一下)直到它完全匹配。
一个更简单的方法会是想出一维整数近似,然后用整数加倍自身。它仍然需要一些修复才能得到总和。
:)
是的。我不得不在代码中添加一个缩放因子,然后将该值四舍五入为int。谢谢大家的建议。非常感谢。
I am having difficulty implementing a Laplacian of Gaussian kernel. I have the following code and I am trying to implement a 9x9 kernel with sigma = 1.4. The kernel is shown on this link
http://homepages.inf.ed.ac.uk/rbf/HIPR2/log.htm[^]
However, my values are nothing like those in that kernel, I think my function is off. Help would be greatly appreciated. Thank you.
const Int32 N = 9; //, Nh = N / 2; float[,] MASK = new float[N, N]; int min = (int)Math.Ceiling((double)-N / (double)2); int max = (int)Math.Floor((double)N/ (double)2); float sigma = 1.4f; float s2 = sigma * sigma; float s4 = sigma * sigma * sigma * sigma; int xCount = 0; for(int x = min; x <= max; ++x) { int yCount = 0; for(int y = min; y <= max; ++y) { float val = (float)(-1.0f / (Math.PI * (s4))) * (1.0f - ((x * x + y * y) / (2.0f * (s2)))) * (float)Math.Exp((-(x * x + y * y) / (2.0f * (s2)))); MASK[xCount, yCount] = val; ++yCount; } ++xCount; }解决方案I'm sorry I can't help you directly with your problem, but when I have a issue complex mathmatical transformation like this I test my code on a very small sample set of simple test data.
Sometimes I reproduce the equations in Excel (or on paper) and feed the same test data into my code and into the test spreadsheet. If it still doesn't correlate I break the equations down so that the code is over a few lines then single step through checking the results on each line of code until I find the difference between my spreadsheet data and my program results.
Sometime the error is a simple typo, they can be hard to spot!
Good Luck, :)
I think you're pretty close. When performing the actual convolution you're going to expect that a constant input (say always 1) results in the same constant value at the output. The formula (and your code) don't account for that, you need a correction factor of around 480.
And then of course, they have shown an all-integer approximation. What normally happens is this I guess:
- choose a correction factor;
- round all numbers to integers;
- check the sum equals N*N;
- iterate the above (and fiddle a bit when rounding) until it all matches well.
A simpler approach would be to come up with a one-dimensional integer approximation, then multiple that to itself using integers only. It would still need some fixing up to get the sum exact though.
:)
Yes. I had to add a scaling factor to the code and then round this value to an int. Thank you all for your suggestions. Greatly appreciated.
这篇关于高斯拉普拉斯算子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文