/** * Method displayMatch that take a search image a template image and a point where the template * matches the search image and writes and displays the search image with a box drawn the size of * the template image at the point given. * * @param: search: The search image of type Picture * @param: template: The template image of type Picture * @param: minMatch: The point where the template appears in the search image, of type Point. */ public static void displayMatch(Picture search, Picture template, Point minMatch) { Turtle turtle = new Turtle((int) minMatch.getX(), (int) minMatch.getY(), search); turtle.setPenWidth(3); turtle.turnRight(); turtle.forward(template.getWidth()); turtle.turnRight(); turtle.forward(template.getHeight()); turtle.turnRight(); turtle.forward(template.getWidth()); turtle.turnRight(); turtle.forward(template.getHeight()); turtle.turnRight(); search.show(); search.write("Waldoboxed.png"); }
/** * 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; }
public static void main(String[] args) { Picture pic = new Picture(); pic.load("queen-mary.png"); for (int x = 0; x < pic.getWidth(); x++) { for (int y = 0; y < pic.getHeight(); y++) { Color original = pic.getColorAt(x, y); Color negative = new Color(255 - original.getRed(), 255 - original.getGreen(), 255 - original.getBlue()); pic.setColorAt(x, y, negative); } } }
// Makes the right half of the picture mirror the left public static void mirror(Picture pict) { int mirrorPoint = pict.getWidth() / 2; // calculating the midpoint Pixel leftPixel = null; // the left pixel to copy from Pixel rightPixel = null; // the right pixel to copy to // loop through the rows for (int y = 0; y < pict.getHeight(); y++) { // loop from 0 to just before the mirror point for (int x = 0; x < mirrorPoint; x++) { // Getting the left and right pixels leftPixel = pict.getPixel(x, y); rightPixel = pict.getPixel(pict.getWidth() - 1 - x, y); // Setting the right pixel to the left pixel's color rightPixel.setColor(leftPixel.getColor()); } } }