/** * Method sadMatch that computes the SAD matches by translating the template image along the * search image between two images and outputs a 2d int array of the SAD matches. * * @param: template: The template image of type Picture * @param: search: The search image of type Picture * @return: sadMatches: A 2d int array of the SAD matches between both images */ public static int[][] sadMatch(Picture template, Picture search) { // Set 2d array for the template and search images int[][] templateArray = new int[template.getHeight()][template.getWidth()]; int[][] searchArray = new int[search.getHeight()][search.getWidth()]; // Get pixel array for template and search images Pixel[] templatePixels = template.getPixels(); Pixel[] searchPixels = search.getPixels(); int widthSearch = search.getWidth(); int heightSearch = search.getHeight(); int widthTemplate = template.getWidth(); int heightTemplate = template.getHeight(); // Get the red values of each pixel and move that into the correct position for (Pixel pT : templatePixels) { templateArray[pT.getY()][pT.getX()] = pT.getRed(); } for (Pixel pS : searchPixels) { searchArray[pS.getY()][pS.getX()] = pS.getRed(); } int[][] sadMatches = new int[heightSearch - heightTemplate][widthSearch - widthTemplate]; for (int i = 0; i < sadMatches.length; i++) { // go from top to bottom for (int j = 0; j < sadMatches[0].length; j++) { // go left to right at each height int[][] sadMatchLocal = new int[heightTemplate][widthTemplate]; // hold the sads in this area for (int k = 0; k < heightTemplate; k++) { // find the differences in this area for (int l = 0; l < widthTemplate; l++) { sadMatchLocal[k][l] = Math.abs(templateArray[k][l] - searchArray[i + k][j + l]); } } // compute and set the differences int sad = 0; for (int y = 0; y < sadMatchLocal.length; y++) { for (int z = 0; z < sadMatchLocal[0].length; z++) { sad += sadMatchLocal[y][z]; } // find the sad } sadMatches[i][j] = sad; // set sad } } return sadMatches; }