My question is similar to this one Add elements to Arraylist and it replaces all previous elements in Java . Though my variables are not static. Still everytime I add one, the other values will be that value.
Important code:
int counter = 1;
// Threshold the image to get a binary image
image.threshold(44);
image.showImage();
int[] directionFIRST = new int[2];
// Get the first white pixel on the boundary
int[] pixelFIRST = image.getFirstBoundaryPixel();
image.updatePicture(pixelFIRST[0], pixelFIRST[1]);
directionFIRST = getInitialDirection(image, pixelFIRST);
//Create an array for the output. It will hold the (x,y) coordinates of
//every pixel around the border of the region to be contour-traced. The
//directions are also saved for the chain code.
List<int[]> listCONTOUR = new ArrayList<int[]>();
List<int[]> listDIRECTION = new ArrayList<int[]>();
// Create a variable which will be used to tell the algorithm when to stop:
boolean stopCondition = false;
int[][] ROTmatrix90 = new int[][]{{0, 1}, {-1, 0}};
int[][] ROTmatrix180 = new int[][]{{-1, 0}, {0, -1}};
int[] tempPIX = pixelFIRST;
int[] tempDIR = directionFIRST;
while (!stopCondition) {
//Take the direction opposit the current direction
tempDIR = multiply(ROTmatrix180, tempDIR);
tempPIX[0] = tempPIX[0] + tempDIR[0];
tempPIX[1] = tempPIX[1] + tempDIR[1];
if (image.get(tempPIX[0], tempPIX[1]) == 1) {
listCONTOUR.add(tempPIX);
listDIRECTION.add(tempDIR);
} else {
tempDIR = multiply(ROTmatrix90, tempDIR);
tempPIX[0] = tempPIX[0] + tempDIR[0];
tempPIX[1] = tempPIX[1] + tempDIR[1];
if (image.get(tempPIX[0], tempPIX[1]) == 1) {
listCONTOUR.add(tempPIX);
listDIRECTION.add(tempDIR);
} else {
tempDIR = multiply(ROTmatrix90, tempDIR);
tempPIX[0] = tempPIX[0] + tempDIR[0];
tempPIX[1] = tempPIX[1] + tempDIR[1];
if (image.get(tempPIX[0], tempPIX[1]) == 1) {
listCONTOUR.add(tempPIX);
listDIRECTION.add(tempDIR);
} else {
tempDIR = multiply(ROTmatrix90, tempDIR);
tempPIX[0] = tempPIX[0] + tempDIR[0];
tempPIX[1] = tempPIX[1] + tempDIR[1];
if (image.get(tempPIX[0], tempPIX[1]) == 1) {
listCONTOUR.add(tempPIX);
listDIRECTION.add(tempDIR);
} else {
tempDIR = multiply(ROTmatrix90, tempDIR);
tempPIX[0] = tempPIX[0] + tempDIR[0];
tempPIX[1] = tempPIX[1] + tempDIR[1];
if (image.get(tempPIX[0], tempPIX[1]) == 1) {
listCONTOUR.add(tempPIX);
listDIRECTION.add(tempDIR);
}
}
}
}
}
counter++;
image.updatePicture(tempPIX[0], tempPIX[1]);
System.out.println(tempPIX[0] + " , " + tempPIX[1]);
if(tempPIX[0]== tempPIX[1]){
System.out.println("test");
}
if ((listCONTOUR.size() > 2) && (tempPIX[0] == listCONTOUR.get(1)[0]) && (tempPIX[0] == listCONTOUR.get(1)[1])) {
stopCondition = true;
listCONTOUR.remove(listCONTOUR.get(listCONTOUR.size() - 1));
listDIRECTION.remove(listDIRECTION.get(listDIRECTION.size() - 1));
}
}
When I check the values of listCONTOUR after lets say 5 runs of the loop, all the values are the same, which is not possible. I searched for a solution, but all solutions are pointing towards the fact that the variable is static. While in my case it isn't. It is just a simple local variable which is initiated in a function and used within 1 function.