Menu

(Solved) : 1 Partimage Class Start Class Defined Represents One Images Using 2 Dimensional Array Bool Q28169525 . . .

Assume that we have an assembly line that can take a picture of a machine part which moves along a conveyor belt. The picture (or image) is represented asa 2D grid of pixels which are either black or white. The pixels can be accessed by specifying the row and column of the pixel where rows and columns are specified by an integer value The machine examines the images and attempts to determine whether or not the parts are broken. A broken part will appear as a set of black pixels which are not all connected together (i.e., there isa separation between one or more sets of black pixel groups. Here are some examples of four possible images. Note that (c) and (d) represent images of broken parts. The red border represents the perimeter of a part composed of black pixels 10 rows 1 2 3 45 6 7 8 9 10 columns

(1) The PartImage class

Start with the class defined below that represents one of theseimages using a 2-dimensional array of booleans.

import javafx.geometry.Point2D;

public class PartImage {

private boolean[][] pixels;

private boolean[][] visited;

private int rows; private int cols;

public PartImage(int r, int c) {

rows = r;

cols = c;

visited = new boolean[r][c];

pixels = new boolean[r][c]; }

public PartImage(int rw, int cl, byte[][] data) {

this(rw,cl);

for (int r=0; r<10; r++) {

for (int c=0; c<10; c++) {

if (data[r][c] == 1)

pixels[r][c] = true;

else

pixels[r][c]= false; }

}

}

public int getRows() { return rows; }

public int getCols() { return cols; }

public boolean getPixel(int r, int c) { return pixels[r][c];}

// You will re-write the 5 methods below public void print(){}

public Point2D findStart() { return null; }

public int partSize() { return 0; }

private void expandFrom(int r, int c) { }

private int perimeterOf(int r, int c) { return 0; }

public boolean isBroken(){

Point2D p = findStart();

expandFrom((int)p.getX(),(int)p.getY());

return (partSize() != 0); }

public int perimeter() {

Point2D p = findStart();

return perimeterOf((int)p.getX(),(int)p.getY()); } }

Add the following static methods to this class, which representsthe examples shown earlier:

public static PartImage exampleA() {

byte[][] pix = {{0,0,0,0,0,0,0,0,0,0},

{0,1,1,1,1,1,1,0,0,0},

{0,1,1,1,1,1,1,0,0,0},

{0,1,1,1,1,1,1,1,1,0},

{0,0,0,1,1,1,1,1,1,0},

{0,1,1,1,1,1,1,1,1,0},

{0,1,1,1,1,1,1,1,1,0},

{0,1,1,1,1,1,1,0,0,0},

{0,0,0,0,1,1,1,0,0,0},

{0,0,0,0,0,0,0,0,0,0}};

return new PartImage(10,10, pix); }

public static PartImage exampleB() {

byte[][] pix = {{1,0,1,0,1,0,1,0,0,0},

{1,0,1,0,1,0,1,1,1,1},

{1,0,1,0,1,0,1,0,0,0},

{1,0,1,0,1,0,1,1,1,1},

{1,0,1,0,1,0,1,0,0,0},

{1,0,1,0,1,0,1,1,1,1},

{1,1,1,1,1,1,1,0,0,0},

{0,1,0,1,0,0,1,1,1,1},

{0,1,0,1,0,0,1,0,0,0},

{0,1,0,1,0,0,1,0,0,0}};

return new PartImage(10,10, pix); }

public static PartImage exampleC() {

byte[][] pix = {{1,1,1,0,0,0,1,0,0,0},

{1,1,1,1,0,0,1,1,1,0},

{1,1,1,1,1,1,1,1,1,1},

{0,1,1,1,0,0,1,0,0,0},

{0,0,1,0,0,0,0,0,0,0},

{1,0,0,0,1,1,0,1,1,1},

{1,1,0,1,1,1,1,1,1,1},

{1,1,1,1,1,1,1,1,1,1},

{0,0,1,1,0,1,1,1,1,1},

{0,0,1,0,0,0,1,1,0,0}};

return new PartImage(10,10, pix);}

public static PartImage exampleD() {

byte[][] pix = {{1,0,1,0,1,0,1,1,0,0},

{1,0,1,0,0,0,1,0,0,0},

{0,0,0,0,0,0,0,0,1,1},

{1,0,1,1,1,1,1,1,1,0},

{1,0,0,1,0,0,1,0,0,0},

{1,1,0,0,0,1,1,0,0,1},

{0,1,0,0,0,0,0,0,1,1},

{0,1,0,1,0,0,0,0,0,0},

{0,0,0,1,1,1,0,0,0,0},

{0,0,0,0,0,1,1,0,0,0}};

return new PartImage(10,10, pix);

}

Here is the test code to testeverything:

public class PartImageTester {

public static void main(String[] args){

PartImage piA =PartImage.exampleA();

PartImage piB =PartImage.exampleB();

PartImage piC =PartImage.exampleC();

PartImage piD =PartImage.exampleD();

System.out.println(“nPart A:”);

System.out.println(” starts at: ” +PartImage.exampleA().findStart());

System.out.println(” size: ” +PartImage.exampleA().partSize());

System.out.println(” broken: ” +PartImage.exampleA().isBroken());

System.out.println(” perimeter: ” +PartImage.exampleA().perimeter()+ “n”);

piA.print();System.out.println(“nPart B:”);

System.out.println(” starts at: ” +PartImage.exampleB().findStart());

System.out.println(” size: ” +PartImage.exampleB().partSize());

System.out.println(” broken: ” +PartImage.exampleB().isBroken());

System.out.println(” perimeter: ” +PartImage.exampleB().perimeter()+ “n”);

piB.print();

System.out.println(“nPart C:”);

System.out.println(” starts at: ” +PartImage.exampleC().findStart());

System.out.println(” size: ” +PartImage.exampleC().partSize());

System.out.println(” broken: ” +PartImage.exampleC().isBroken());

System.out.println(” perimeter: ” +PartImage.exampleC().perimeter()+ “n”);

piC.print();System.out.println(“nPart D:”); System.out.println(” starts at: “+ PartImage.exampleD().findStart());

System.out.println(” size: ” +PartImage.exampleD().partSize());

System.out.println(” broken: ” +PartImage.exampleD().isBroken());

System.out.println(” perimeter: ” +PartImage.exampleD().perimeter()+ “n”); piD.print(); } }

Assume that we have an assembly line that can take a picture of a machine part which moves along a conveyor belt. The picture (or image) is represented asa 2D grid of pixels which are either black or white. The pixels can be accessed by specifying the row and column of the pixel where rows and columns are specified by an integer value The machine examines the images and attempts to determine whether or not the parts are broken. A broken part will appear as a set of black pixels which are not all connected together (i.e., there isa separation between one or more sets of black pixel groups. Here are some examples of four possible images. Note that (c) and (d) represent images of broken parts. The red border represents the perimeter of a part composed of black pixels 10 rows 1 2 3 45 6 7 8 9 10 columns Show transcribed image text

Expert Answer


Answer to 1 Partimage Class Start Class Defined Represents One Images Using 2 Dimensional Array Bool Q28169525 . . .

OR