我不明白为什么我的c ++代码运行得这么慢



我正在研究用于边缘检测的Sobel掩蔽,而不使用任何特殊的库。我想要得到的输出是一个512x512矩阵的文本文件,其值在0到1之间。我已经检查了代码是否通过放置较小的值来工作,比如50,而不是"ROW-2"one_answers"COL-2"。然而,如果我把它们放回原处,代码需要很长时间才能运行。

常数值为:

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

这是函数Sobel卷积:

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

有什么提示或建议吗?提前感谢!

以下是一些注意事项:

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

你真的想打开一个图像文件18次,读取每一行和每一列的所有数据,只返回一行和一列18次吗?为什么不读取一次图像文件并将图像数据数组传递给函数?

你所做的不仅有点低效,而且——对不起——完全是疯狂的。

对于图像的每个像素,您调用SobelConvolution,它依次调用ImageReader 18次(其中6次没有用,因为相应的系数为零)。但可怕的是,ImageReader每次都会从文本文件中执行完整的图像读取,其中简单的数组查找就足够了。

因此,总共要执行4718592个文件流打开/关闭操作和1236950581248个文件值读取操作,其中仅执行1个打开/关闭和262144个读取操作就足够了。(不算一次读取比直接阵列访问成本高得多。)一次完整的运行可能会持续两个小时或更长时间。

相关内容

  • 没有找到相关文章

最新更新