How to work on convolution programming ?

Hi guys , please help me .. i need to blur an image on pgm format . but i dont know how to do it ..

i done do the output file ,, and try to work on it , but . .im not getting the result . it really make me confuse . :/

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

}



then ,what i suppose to do is :

c) Work on image convolution programming

i. For a start, assign the convolution window size to 3.

ii. You need to use 4 nested for loops in this operation. 2 nested for loops to step through the original image row-by-row. Within the 2 nested for loops, you need to calculate the mean of the values in the convolution window, which uses another 2 nested for loops. To make programming easier, define a function for the convolution window to calculate the mean values.

iii. Store the mean values to array meanVal then write to an external file.

. . . can anyone help me to do it ?

please kindly assist . Thank you
You should open and treat it as binary. Or just use a framework/library that provides image editing functionality.
Golden Lizard wrote:
You should open and treat it as binary. Or just use a framework/library that provides image editing functionality.

Do you know how .pgm files work? It's so simple there's no reason to open it in binary mode.

http://netpbm.sourceforge.net/doc/pgm.html#plainpgm

Also, there wouldn't be a point in writing code for opening .pgm files if a framework of library is used, would there? Unless it is to get a task done (non-learning purposes).
Last edited on
Topic archived. No new replies allowed.