我正在进行Sobel掩模边缘检测,但不使用任何特殊库。我想得到的输出是一个512x512矩阵的文本文件,其值介于0到1之间。
我已经通过使用较小的值(如50)替换'ROW-2'和'COL-2'来验证代码是否工作。
然而,如果我将它们放回去,代码需要很长时间才能运行。
常量值为:
这是SobelConvolution函数的定义:
常量值为:
const int ROW = 512;
const int COL = 512;
const double Gx [3][3] = { {-1.0,0.0,1.0},{-2.0,0.0,2.0},{-1.0,0.0,1.0}};
const double Gy [3][3] = { {1.0,2.0,1.0},{0.0,0.0,0.0},{-1.0,-2.0,-1.0}};
这是主要函数:
int main()
{
double NewImage[ROW][COL] = {0};
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
NewImage[i][j] = 0;
}
}
for (int i = 0; i < ROW-2; i++)
{
for (int j = 0; j < COL-2; j++)
{
NewImage[i+1][j+1] = SobelConvolution(i,j);
}
}
ofstream newImage;
string filename;
filename = "output image.txt";
newImage.open (filename.c_str());
for(int rows = 0; rows < ROW; rows++)
{
for(int cols = 0; cols < COL; cols++)
{
newImage << NewImage[ROW][COL] <<" ";
}
newImage << endl;
}
newImage.close();
return 0;
}
这是SobelConvolution函数的定义:
double SobelConvolution(int row, int col)
{
double convX;
double convY;
double conv;
convX = ImageReader(row,col)*Gx[2][2]
+ ImageReader(row,col+1)*Gx[2][1]
+ ImageReader(row,col+2)*Gx[2][0]
+ ImageReader(row+1,col)*Gx[1][2]
+ ImageReader(row+1,col+1)*Gx[1][1]
+ ImageReader(row+1,col+2)*Gx[1][0]
+ ImageReader(row+2,col)*Gx[0][2]
+ ImageReader(row+2,col+1)*Gx[0][1]
+ ImageReader(row+2,col+2)*Gx[0][0];
convY = ImageReader(row,col)*Gy[2][2]
+ ImageReader(row,col+1)*Gy[2][1]
+ ImageReader(row,col+2)*Gy[2][0]
+ ImageReader(row+1,col)*Gy[1][2]
+ ImageReader(row+1,col+1)*Gy[1][1]
+ ImageReader(row+1,col+2)*Gy[1][0]
+ ImageReader(row+2,col)*Gy[0][2]
+ ImageReader(row+2,col+1)*Gy[0][1]
+ ImageReader(row+2,col+2)*Gy[0][0];
conv = sqrt((convX*convX) + (convY*convY));
return conv;
}
这是ImageReader函数:
double ImageReader(int r, int c)
{
double OrigImage[ROW][COL];
ifstream defaultImage ("image.txt");
if (defaultImage.good())
{
for (int i = 0; i < ROW; i++)
{
for (int j = 0; j < COL; j++)
{
defaultImage >> OrigImage[i][j];
}
}
}
return OrigImage [r][c];
}
有什么提示或建议吗?提前感谢!
NewImage
的第一个和最后一个值(只将它们保留为0
)吗? - Rakete1111