1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
|
#include <iostream>
#include <conio.h>
#include <fstream>
#include <stdlib.h>
#include <iomanip>
#define ROWMAX 200
#define COLMAX 200
using namespace std;
ifstream inFile;
ofstream outFile;
// Prototype declarations
void avgFilter(int, int[200][200], int[200][200], int, int);
//====== main()
void main()
{
int greyVal[ROWMAX][COLMAX], // original values read from the pgm file
meanVal[ROWMAX][COLMAX]; // storing modified grey values for the output pgm file
char ftype[5]; // file type in pgm file: P2
// add other necessary variables declarations here
int ROW , COL , MAXval ;
// Open two different external files for reading and writing, and check files' status. Add your statements here.
inFile.open("Q.pgm");
if (inFile.fail() || outFile.fail()) {
cerr << " Program cannot read in the value ! " << endl;
_getch();
exit (-1); }
outFile.open("newQ.pgm");
if (outFile.fail()) {
cerr << " Cannot create output file ! " << endl;
_getch();
exit (-1); }
// start reading files, read file type: P2;
inFile >> ftype;
// read in values of 3 more integers here
inFile >> COL >> ROW >> MAXval ;
// by using nested for loops, write statements here to read grey values from input file to store in greyVal array
// read greyscale values
for( int i = 0 ; i < ROW ; i++ ) {
for( int j = 0 ; j < COL ; j++ ) {
inFile >> greyVal[i][j];
meanVal[i][j] = greyVal[i][j];
}
}
// Ask for the size of the convolution window and check the validity of the size here
int size = 0 ;
cout << "Input image :" << inFile << endl ;
cout << "Size of the image is " << ROW << " rows x " << COL << " columns." << endl ;
cout << "Enter size of the convulation window : " ;
cin >> size ;
// Output to external file, must be in exactly the same format as the input pgm file
outFile << ftype << endl; // This is the first line of the file.
// Write column and row to second line of the file
outFile << COL << " " << ROW << " " << endl ;
// Write the third line (255 as in Q.pgm)
outFile << MAXval << endl ;
// Write the contents of meanVal by using nested for loops
for( int i = 0 ; i < ROW ; i++ ) {
for( int j = 0 ; j < COL ; j++ ) {
avgFilter(size, greyVal, meanVal, ROW, COL );
outFile << setw(4) << setiosflags(ios::left) << meanVal[i][j] ; } // <==== add a space after each entry
outFile << endl; // <==== move here
}
outFile << endl;
inFile.close();
outFile.close();
cout << "Done! Please check output file.";
_getch();
}
void avgFilter(int size, int greyVal[200][200], int meanVal[200][200], int ROW, int COL)
{
// add necessary variables declarations/initialization statements here
int sum = 0 , i, j ;
int v = size/2 ;
for ( i = v ; i <= (ROW - v -1) ; i++) {
for ( j = v ; j <= (COL - v - 1) ; j++ ) ; }
for ( int r = (i - v) ; r <= (i + v) ; i++) {
for ( int s = (j - v) ; s <= (j + v) ; j++) {
sum = sum + greyVal[r][s] ;
if ( r == (i+v) && s == (j+v)){
meanVal[i][j] = sum/size*size ; }
}
}
}
|