【发布时间】:2018-12-04 15:11:07
【问题描述】:
对于我的计算机视觉课程,我们目前正在研究 Canny 边缘检测算法。对于那些熟悉的人来说,该算法涉及使用图像的灰度来为每个像素创建梯度向量。因此,我的代码有两个矩阵来存储这些信息,一个是幅度,一个是角度。
double edge[height][width];
double max = 0;
for(int r = 0; r<height; r++)
{
for(int c = 0; c<width; c++)
{
if(r==0||c==0||r+1==height||c+1==width)
{
edge[r][c]=0;
}
else
{
edge[r][c]=sqrt(pow((2*greyscale[r-1][c])+greyscale[r-1][c+1]+greyscale[r-1][c-1]-(2*greyscale[r+1][c])-greyscale[r+1][c+1]-greyscale[r+1][c-1],2.0)+pow((2*greyscale[r][c-1])+greyscale[r+1][c-1]+greyscale[r-1][c-1]-(2*greyscale[r][c+1])-greyscale[r-1][c+1]-greyscale[r+1][c+1],2.0));
if(edge[r][c]>max)
{
max=edge[r][c];
}
}
}
}
//cout<<"makes edge"<<endl;
double atans[height][width]; //should work, but creates memory error when uncommented
for(int r = 0; r<height; r++)
{
for(int c = 0; c<width; c++)
{
cout<<r<<", "<<c<<endl;
if(r==0||c==0||r+1==height||c+1==width)
{
atans[r][c]=0;
}
else
{
atans[r][c] = atan2(2*greyscale[r-1][c]+greyscale[r-1][c+1]+greyscale[r-1][c-1]-2*greyscale[r+1][c]-greyscale[r+1][c+1]-greyscale[r+1][c-1],2*greyscale[r][c-1]+greyscale[r+1][c-1]+greyscale[r-1][c-1]-2*greyscale[r][c+1]-greyscale[r-1][c+1]-greyscale[r+1][c+1]);
}
}
}
我的代码使边缘矩阵很好,但是当我尝试制作 atan 矩阵时会给我一个分段错误。 有关如何解决此问题的任何建议?
【问题讨论】:
-
除非
height和width是编译时常量,否则这是一个非标准VLA。请记住,堆栈通常在 Linux 上限制为 10MB,在 Windows 上限制为 1MB。 -
double edge[height][width];-- 我们需要确切地看到height和width被声明为什么。如前所述,如果height和width不是常量,则该行不是有效的 C++。 -
greyscalematrix 的尺寸是多少?应该大于[height][width] -
OT:考虑使用
std::hypot(x, y);而不是sqrt(pow(x, 2.0) + pow(y, 2.0));
标签: c++ canny-operator