2016-08-02 73 views
3

我正在使用Sobel掩码进行边缘检测而不使用任何特殊的库。我想要得到的输出是一个512x512矩阵的文本文件,其值在0到1之间。 我已经通过放置较小的值(如50而不是'ROW-2'和'COL-2')来检查代码的工作情况, 。 但是,如果我把它们放回去,代码将永远运行。我不明白为什么我的C++代码运行速度很慢

常数的值是:

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]; 
} 

任何提示或建议?提前致谢!

+0

是否故意忽略'NewImage'的第一个和最后一个值(只留下它们到'0')? – Rakete1111

+0

是的。我认为这样做不会太复杂,不用担心边缘的值。这就是为什么程序导致问题的原因吗? –

+0

不能:)有太多不必要的循环。 ;) – Rakete1111

回答

2

您是否真的打算单次打开18次图像文件并读取每行和每列的所有数据,以便返回单个行和列18次?为什么不一次读取图像文件并将图像数据数组传递给函数?

+0

噢,我只是把它当作一种价值而已,忘了我实际上是在每次打开整个图像文件时都这样做。谢谢! –

+0

@RyanKim你每次都在遍历整个数组:) – Rakete1111

4

这里有一些注意事项:

  • ImageReader

    返回数组中只有一个值,无需通过整个阵列阅读每时候,你只需要一个单一的值。在我看来,这个功能是多余的。

  • SobelConvolution

    此功能是好的,但有一个不必要的变量 - conv

  • main

    我不知道为什么你的NewImage每个值初始化为0,当他们已经0也实际上并不需要NewImage

这里就是我会写(与广泛评论):

double SobelConvolution(int row, int col) 
{ 
    //ImageReader has been removed, it was unnecessary. The code has been moved here 
    double oldImage[ROW][COL]; 
    std::ifstream defaultImage{ "image.txt" }; 

    //Error handling if file doesn't exist - consider doing something else :) 
    if (!defaultImage.is_open()) 
     return 0; 

    //Initialize array 
    for (int i = 0; i < ROW; ++i) 
     for (int j = 0; j < COL; ++j) 
      defaultImage >> oldImage[i][j]; 

    //You should always declare variables where they are first used, this 
    //reduces the possibility of errors 
    //We can just access the array directly 
    double convX = oldImage[row][col] * Gx[2][2] 
     + oldImage[row][col + 1] * Gx[2][1] 
     + oldImage[row][col + 2] * Gx[2][0] 
     + oldImage[row + 1][col] * Gx[1][2] 
     + oldImage[row + 1][col + 1] * Gx[1][1] 
     + oldImage[row + 1][col + 2] * Gx[1][0] 
     + oldImage[row + 2][col] * Gx[0][2] 
     + oldImage[row + 2][col + 1] * Gx[0][1] 
     + oldImage[row + 2][col + 2] * Gx[0][0]; 

    double convY = oldImage[row][col] * Gy[2][2] 
     + oldImage[row][col + 1] * Gy[2][1] 
     + oldImage[row][col + 2] * Gy[2][0] 
     + oldImage[row + 1][col] * Gy[1][2] 
     + oldImage[row + 1][col + 1] * Gy[1][1] 
     + oldImage[row + 1][col + 2] * Gy[1][0] 
     + oldImage[row + 2][col] * Gy[0][2] 
     + oldImage[row + 2][col + 1] *Gy[0][1] 
     + oldImage[row + 2][col + 2]*Gy[0][0]; 

    //No need to create a separate variable just to return it 
    return sqrt((convX*convX) + (convY*convY)); 
} 


int main() 
{ 
    //= {} Initializes every element to 0, you don't need to do it :) Just so you know :) 
    //Note that it crashes here, because my stack size was too small, 
    //maybe consider using a dynamic array (512 * 512 is pretty big) :) 
    //double NewImage[ROW][COL] = {}; 
    //The array is not really needed, see below 

    std::string filename = "oimage.txt"; 
    std::ofstream newImage{ filename }; 

    //No need to create another array just to output it again, 
    //Just output the calculated values - this doesn't ignore the first/last values 
    for (int rows = 0; rows < ROW; rows++) 
    { 
     for (int cols = 0; cols < COL; cols++) 
      newImage << SobelConvolution(rows, cols) << " "; 
     newImage << '\n'; //std::endl flushes the stream, while \n does not - it is faster :) 
    } 

    newImage.close(); 

    return 0; 
} 
+0

我花了一点时间来看看你的评论。谢谢!它真的帮了我很多。我并不完全确定如何以这种方式声明数组,所以这让我放了更多变量来澄清,这使得它过于冗余。 –

+0

@RyanKim欢迎您,如果您还有其他问题,请提问:) – Rakete1111

2

你在做什么不只是一点点效率低下,这是-sorry-完全疯了。

对于图像的每个像素,您调用SobelConvolution,然后调用ImageReader 18次(其中6个没用,因为对应的系数为零)。但可怕的是,ImageReader每次都会从文本文件中读取完整的图像,只需简单的数组查找即可。

因此,您总共执行4718592个文件流打开/关闭和1236950581248值读取文件,其中只有1个打开/关闭和262144读取就足够了。 (不要指望单个读取比直接访问更昂贵)。完整的运行可以持续两个小时或更长时间。

相关问题