Here is the assignment:
Here is my code so far:A digitized image is an m X m matrix of pixels. In a binary image each pixel is either 0 or 1. A 0 pixel represents image background, while a 1 represents a point on the image component. We will refer to pixels whose value is 1 as component pixels. Two pixels are adjacent if one is to the left, above, right or below the other. Two component pixels that are adjacent are pixels of the same image component. The objective of component labeling is to label the component pixels so that two pixels get the same label if and only if they are pixels of the same image component.
Consider Figure 1(a) that shows a 7 X 7 image. The blank squares represent background pixels, and the 1s represent component pixels. Pixels (1,3) and (2,3) are pixels of the same component because they are adjacent. Since component pixels (2,3) and (2,4) are adjacent, they are also from the same component. Hence the three pixels (1,3), (2,3), and (2,4) are from the same component. Since no other image pixels are adjacent to these three pixels, these three define an image component. The image of figure 1(a) has four components. The first component is defined by the pixel set (1,3), (2,3), (2,4); the second is (3,5), (4,4), (4,5), (5,5); the third is (5,2), (6,1), (6,2), (6,3), (7,1), (7,2), (7,3); and the fourth is (5,7), (6,7), (7,6), (7,7). In Figure 1(b) the component pixels have been given labels so that two pixels have the same label if and only if they are part of the same component. We use the numbers 2,3,4,… as component identifiers; there is no component numbered 1 because 1 designates an unlabeled component pixel.
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package pixels; import java.util.LinkedList; import java.util.Queue; /** * * @author Jared */ public class Pixels { /** * @param args the command line arguments */ public static void main(String[] args) { //This is the 2D array that will be the input to the function... int[][] grid = new int[][]{ { 0, 0, 0, 0, 0, 0, 0, 0, 0}, { 0, 0, 0, 1, 0, 0, 0, 0, 0}, { 0, 0, 0, 1, 1, 0, 0, 0, 0}, { 0, 0, 0, 0, 1, 0, 0, 0, 0}, { 0, 0, 0, 0, 0, 0, 1, 1, 0}, { 0, 0, 0, 0, 0, 0, 0, 0, 0}, { 0, 0, 0, 0, 0, 0, 0, 0, 0}, { 0, 0, 0, 0, 0, 0, 0, 0, 0}, { 0, 0, 0, 0, 0, 0, 0, 0, 0} }; solver(grid); //System.out.println(grid[1][2]); } public static void solver(int[][] temp){ //Queues NOTE: will need to make more... Queue<point> Queue = new LinkedList<point>(); //Running count of number of groups int count = 2; //Iterating through the grid for (int x[] : temp){ int xAxis = 0; for (int y : x){ if (y == 1){ //Creates an object of the point point newPoint = new point(xAxis, y); //Adds point to the queue Queue.add(newPoint); //Changes its value y = count; //Checks neighbors while (!Queue.isEmpty()){ int tempX = Queue.peek().getX(); int tempY = Queue.peek().getY(); Queue.remove(); //Check neighbor up if (temp[tempX + 1][tempY] == 1){ point up = new point(tempX + 1, tempY); Queue.add(up); temp[tempX + 1][tempY] = count; } //Check neighbor down if(temp[tempX - 1][tempY] == 1){ point down = new point(tempX - 1, tempY); Queue.add(down); temp[tempX - 1][tempY] = count; } //Check neighbor right if(temp[tempX][tempY + 1] == 1){ point right = new point(tempX, tempY + 1); Queue.add(right); temp[tempX][tempY + 1] = count; } //Check neighbor left if(temp[tempX][tempY - 1] == 1){ point left = new point(tempX, tempY - 1); Queue.add(left); temp[tempX][tempY - 1] = count; } } //Changes group number count++; } } } //Outputs the matrix System.out.println(temp); } }
Here is the code for the point class:
/* * This class will define the point objects that will represent the individual pixels and be stored in the queue. */ package pixels; /** * * @author Jared */ public class point { //variables public int x, y; //constructor for point object public point(int x, int y){ //x-axis this.x = x; //y-axis this.y = y; } //Getters public int getX(){ return x; } public int getY(){ return y; } //Setters public void setX(int x){ this.x = x; } public void setY(int y){ this.y = y; } }
Here is the error that is occurring:
run:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at pixels.Pixels.solver(Pixels.java:76)
at pixels.Pixels.main(Pixels.java:34)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)