/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package convolution; /** * @author jramonsoler * SIMPLE EXAMPLE OF A CONVOLUTION BETWEEN TWO MATRICES, (particular case: BLUR) * EJEMPLO SIMPLE CON LA CONVOLUCIÓN DE DOS MATRICES,(caso part. : desenfoque) */ public class Convolution { public static int[][] input = {{1,1,1,1,70,255,1,1,1}, {1,255,77,1,1,1,1,1,1}, {1,1,1,1,1,255,1,1,60}, {1,1,1,1,1,150,1,50,1}, {1,50,255,3,3,3,7,7,7}, {7,8,3,6,4,4,50,255,4}, {50,255,7,8,9,1,1,2,4}, {255,0,7,0,7,4,4,4,50}, {1,1,2,3,8,7,7,50,255}}; // 0.04 = 1/25 public static double[][] filter = {{0.04,0.04,0.04,0.04,0.04}, {0.04,0.04,0.04,0.04,0.04}, {0.04,0.04,0.04,0.04,0.04}, {0.04,0.04,0.04,0.04,0.04}, {0.04,0.04,0.04,0.04,0.04}}; /** * @param args the command line arguments */ public static void main(String[] args) { int inputRows = input.length; int inputColumns = input[0].length; int filterRows = filter.length; int filterColumns = filter[0].length; int fborder = filterRows/2; int cborder = filterColumns/2; int f_corner, c_corner; double acum; int[][] output = new int[inputRows-2*fborder][inputColumns-2*cborder]; // CONVOLUTION (BLUR FILTER) // Next two loops select the pixels to be calculated for (int f0 = fborder; f0 <= inputRows - 1 - fborder; f0++) { for (int c0 = cborder; c0 <= inputColumns - 1 - cborder; c0++) { // Begin of the inner loops which calaculate each new "pixel" acum = 0.0; f_corner = f0 - fborder; c_corner = c0 - cborder; for (int f = 0; f < filterRows; f++) { for (int c = 0; c < filterColumns; c++) { // Begin of the inner loops which calaculate each "pixel" acum += filter[f][c] * input[f_corner+f][c_corner+c]; } } output[f0-fborder][c0-cborder] = (int)Math.round(acum); } } // END OF CONVOLUTION (BLUR FILTER) // Output print for (int f = 0; f < output.length; f++) { for (int c = 0; c < output[0].length; c++) { System.out.printf("%4d",output[f][c]); } System.out.println(); } } }