Advertisement
Guest User

Untitled

a guest
Oct 3rd, 2016
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <vector>
  5.  
  6. using namespace std;
  7.  
  8. int main(int argc, char* argv[])
  9. {
  10.     ifstream infile;
  11.     ofstream outfile;
  12.     string feed, type, maxpx, comment;
  13.     int cols, rows;
  14.     const int SIZE = 3;
  15.    
  16.     infile.open("lena.pgm");
  17.     getline(infile, type);
  18.     infile >> rows >> cols;    
  19.     getline(infile, feed);
  20.     getline(infile, maxpx);
  21.  
  22.     vector<vector<char>> image(rows, vector<char>(cols, '\0'));
  23.     vector<vector<char>> out(rows, vector<char>(cols, '\0'));
  24.     vector<vector<int>> filter = { { 0, -1, 0 }, { -1, 5, -1 }, { 0, -1, 0 } };
  25.    
  26.     for (int i = 0; i < rows; i++)
  27.         for (int j = 0; j < cols; j++)
  28.             infile >> image[i][j];
  29.     infile.close();
  30.     outfile.open("output.pgm");
  31.     outfile << type << "\n" << comment << "\n" << rows << " " << cols << "\n" << maxpx << "\n";
  32.    
  33.     for (int i = SIZE / 2; i < rows - SIZE / 2; i++)
  34.     {
  35.         for (int j = SIZE / 2; j < cols - SIZE / 2; j++)
  36.         {
  37.             char sum = '\0';
  38.             for (int k = -SIZE / 2; k <= SIZE / 2; k++)
  39.             {
  40.                 for (int l = -SIZE / 2; l <= SIZE / 2; l++)
  41.                 {
  42.                     sum += image[i+k][j+l] * filter[k + SIZE / 2][l + SIZE / 2];
  43.                 }
  44.             }
  45.             out[i][j] = sum;
  46.         }
  47.     }
  48.    
  49.     for (int i = 0; i < rows; i++)
  50.         for (int j = 0; j < cols; j++)
  51.             outfile << image[i][j];
  52.    
  53.     outfile.close();
  54.    
  55.     return 0;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement