Advertisement
Guest User

image

a guest
Oct 1st, 2016
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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 line, type, maxpx, comment;
  13.     int cols, rows;
  14.     const int SIZE = 3;
  15.    
  16.     infile.open("/Users/dotun/Desktop/file/file/lena.pgm", ios::binary);
  17.    
  18.     getline(infile, type);  //Read first line and grab the type of file
  19.     //getline(infile, comment);  //Read second line and discard it
  20.    
  21.     infile >> rows >> cols; //Read third line and get columns and rows
  22.    
  23.     getline(infile, line);
  24.     getline(infile, maxpx);  //Read highest pixel
  25.  
  26.    
  27.     vector<vector<char>> image(rows, vector<char>(cols, '\0'));
  28.     vector<vector<char>> out(rows, vector<char>(cols, '\0'));
  29.     vector<vector<int>> filter = { { 0, -1, 0 }, { -1, 5, -1 }, { 0, -1, 0 } }; //Sharpen
  30.    
  31.     for (int i = 0; i < rows; i++)
  32.         for (int j = 0; j < cols; j++)
  33.             infile >> image[i][j];
  34.    
  35.     infile.close();
  36.    
  37.     outfile.open("/Users/dotun/Desktop/file/file/output.pgm", ios::binary);
  38.     outfile << type << "\n" << comment << "\n" << rows << " " << cols << "\n" << maxpx << "\n";
  39.    
  40.     for (int i = SIZE / 2; i < rows - SIZE / 2; i++)
  41.     {
  42.         for (int j = SIZE / 2; j < cols - SIZE / 2; j++)
  43.         {
  44.             char sum = '\0';
  45.             for (int k = -SIZE / 2; k <= SIZE / 2; k++)
  46.             {
  47.                 for (int l = -SIZE / 2; l <= SIZE / 2; l++)
  48.                 {
  49.                     sum += image[i+k][j+l] * filter[k + SIZE / 2][l + SIZE / 2];
  50.                 }
  51.             }
  52.             out[i][j] = sum;
  53.         }
  54.     }
  55.    
  56.     for (int i = 0; i < rows; i++)
  57.         for (int j = 0; j < cols; j++)
  58.             outfile << image[i][j];
  59.    
  60.     outfile.close();
  61.    
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement