//============================================================================ // Name : ej4_t2.cpp // Author : PC // Programa calculo de suavizado de una imagen //============================================================================ #include #include #include void suavizado( int , int , int, int **, float **); void read_matrix(int , int , int **); void write_matrix(int , int, int **); void write_matrix(int , int, float **); using namespace std; int main(int argc, char *argv[]) { int **matrix; float **matrixs; int m, n, p; cout << "Enter the number of rows and columns for the matrix: "; cin >> m >> n ; cout << "Enter the number of surrounding mean filter: "; cin >> p ; matrix = new int *[m]; for (int i = 0; i < m; i++) matrix[i] = new int [n]; read_matrix(m, n, matrix); cout << "\nImage:" << endl; write_matrix(m, n, matrix); cout << "\nSuavizado: " << endl; matrixs = new float *[m]; for (int i = 0; i < m; i++) matrixs[i] = new float [n]; suavizado (m, n, p, matrix, matrixs); write_matrix(m, n, matrixs); return 0; } void read_matrix(int row, int col, int **matrix) { cout << "Now enter the element for the matrix: " << endl; for (int i=0; i < row; i++) for (int j=0; j < col; j++) { cout << "Row " << (i+1) << " Col " << (j+1) << " : "; cin >> matrix[i][j]; } } void write_matrix(int row, int col, int **matrix) { for (int i=0; i < row; i++) { for (int j=0; j < col; j++) cout << matrix[i][j] << " "; cout << endl; } } void write_matrix(int row, int col, float **matrix) { for (int i=0; i < row; i++) { for (int j=0; j < col; j++) cout << matrix[i][j] << " "; cout << endl; } } void suavizado( int m, int n, int p, int **matrix, float **matrixs) { int cont, x, y, i, j; float suma; for (x = 0; x < m; x++) for (y = 0; y < n; y++) { cont = 0; suma = 0; for (i = -p; i <= p; i++) for (j = -p; j <= p; j++) if ((x + i) >= 0 && (x + i) < m && (y + j) >= 0 && (y + j) < n) { cont++; suma += matrix[x+i][y+j]; } matrixs[x][y] = suma/(cont*cont); } }