📜  使用C++去除盐和胡椒粉的噪音

📅  最后修改于: 2021-05-30 07:20:58             🧑  作者: Mango

椒盐噪声是有时在图像上看到的一种噪声形式。也称为脉冲噪声。这种噪声可能是由图像信号中的尖锐和突然的干扰引起的。它表示为稀疏出现的白色和黑色像素。一种有效的降噪方法是中值滤波器或形态滤波器。

在本文中,我们将学习如何仅使用C++(而不使用任何外部图像处理库,例如OpenCV)从图像中去除椒盐噪声。

方法:

  1. 将输入图像的像素值存储在数组中。
  2. 对于每个像素值,如果它包含0(黑色)或255(白色),则计算其相邻像素值的平均值,否则该像素值存储在另一个数组中。
  3. 新数组的像素值用于输出文件。

以下是Salt-Pepper噪声消除的简单C++实现:

#include 
#include 
#include 
  
using namespace std;
  
int array[2000][2000]; // used for input image
int arr[2000][2000]; // used for output image
int hist[255];
  
int main()
{
    int i, row = 0, j = 0, col = 0, numrows = 0, numcols = 0, MAX = 0;
      
    // input image
    ifstream infile("saltpepper.pgm"); 
    stringstream ss;
    string inputLine = "";
  
    // First line : version
    getline(infile, inputLine);
    if (inputLine.compare("P2") != 0)
        cerr << "Version error" << endl;
    else
        cout << "Version : " << inputLine << endl;
  
    // Continue with a stringstream
    ss << infile.rdbuf();
      
    // Secondline : size
    ss >> numcols >> numrows >> MAX;
      
    // print total number of columns,
    // rows and maximum intensity of the image
    cout << numcols << " columns and " << numrows << " rows" << endl
         << " Maximium Intesity " << MAX << endl;
  
    // Initialize a new array of
    // same size of the image with 0
    for (row = 0; row <= numrows; ++row)
        array[row][0] = 0;
  
    for (col = 0; col <= numcols; ++col)
        array[0][col] = 0;
  
    printf("****\n");
      
    // Following lines : data
    for (row = 1; row <= numrows; ++row) {
        for (col = 1; col <= numcols; ++col) {
            // original data store in new array
            ss >> array[row][col]; 
        }
    }
  
    for (row = 1; row <= numrows; ++row) {
        for (col = 1; col <= numcols; ++col) {
              
            // check if intensity is black or white
            if (array[row][col] == 0 || array[row][col] == 255) 
            {
                // compute average of neighbours pixels
                // and store the pixel value in another
                // array for output image
                arr[row][col] = (array[row - 1][col] +
                                 array[row - 1][col - 1] +
                                 array[row - 1][col + 1] +
                                 array[row][col - 1] +
                                 array[row][col + 1] +
                                 array[row + 1][col + 1] +
                                 array[row + 1][col] +
                                 array[row + 1][col - 1]) / 8; 
            }
            else {
                // store pixel value in another
                // array for output image
                arr[row][col] = array[row][col]; 
            }
        }
    }
  
    ofstream outfile;
      
    // new file open to store the output image
    outfile.open("salt and pepper.pgm"); 
    outfile << "P2" << endl;
    outfile << numcols << " " << numrows << endl;
    outfile << "255" << endl;
  
    for (row = 1; row <= numrows; ++row) {
        for (col = 1; col <= numcols; ++col) {
              
            // store resulatant pixel intensity to the output file
            outfile << arr[row][col] << " "; 
        }
    }
      
    outfile.close();
    infile.close();
    return 0;
}

输入图片:

输出图像:

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”