在C ++中适配2d高斯函数太慢 [英] Fitting 2d gauss function in C++ too slow
问题描述
我尝试对图像适用2d高斯函数(使用cv :: Mat格式),并且使用NLopt库。
使我的对象函数像这样:
for(i对于每一行)
b $ b {
//计算高斯函数值
double valuenow = x [0] * exp( - (x [3] *(jx [1])*(jx [1])+ 2 * x [4] *(jx [1])*(ix [2])+ x [5] *(ix [2])*(ix [2]
//将平方余数加到结果
result + =(valuenow-fitdata.at< double>(i,j))*(valuenow-fitdata.at double(i,j));
}
return result;
我的矩阵约为1000 * 1000大小,我使用LN_COBYLA算法。当我跑这个,原来是非常缓慢。我认为在我指定我的对象函数的方式肯定有一些问题,因为我曾经做过相同的事情在Matlab与lsqnonlinear,在第二个返回。
有人可以帮助我吗?提前致谢。
在<>()
函数很慢。如果速度是本质,那么在循环中使用它不是一个好主意。在循环外部使用指针,然后在循环中使用该指针。
一个相关的问题: I'm trying to fit a 2d gauss function to an image (in cv::Mat format), and I'm using the NLopt library. I put my object function like this: My matrix is about 1000*1000 size, I'm using LN_COBYLA algorithm. When I ran this, it turned out to be extremely slow. I think there must be something wrong with the way I specify my object function, because I used to do the same thing in Matlab with lsqnonlinear, which returned in a second. Can someone help me please? Thanks in advance. The A related question:
OpenCV Mat array access, which way is the fastest for and why? 这篇关于在C ++中适配2d高斯函数太慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
OpenCV Mat数组访问,哪种方式最快? / p> for(i for each row)
for(j for each col)
{
//compute the gauss function value
double valuenow=x[0]*exp(-( x[3]*(j-x[1])*(j-x[1]) + 2*x[4]*(j-x[1])*(i-x[2]) + x[5]*(i-x[2])*(i-x[2]) ));
//add square residual to result
result+=(valuenow-fitdata.at<double>(i,j))*(valuenow-fitdata.at<double>(i,j));
}
return result;
at<>()
function is slow. If speed is of essence, it's not a good idea to use it inside loops. Take a pointer outside the loop and then just use that pointer inside the loop.