Hi, I need help with an method in my little project. I've tried to create a method that counts white sheep from an image and displays the number I trued using union. But something is not working right, does anyone see if I'm doing something wrong?
Code
public void countSheep() {
ArrayList<Integer> sheepCounter = new ArrayList<>();
for (int i = 0; i <imageArray.length; i++) {
//Union find
if (imageArray[i] > 0 && imageArray[i+1] > 0 || imageArray[i] > 0 && imageArray[i + pic.getWidth()] > 0) {
if (imageArray[i] > 0 && imageArray[i + 1] > 0) {
union(imageArray, imageArray[i], i + 1);
}
System.out.println("The root of "+ i +" is " + find(imageArray, i));
if (imageArray[i] > 0 && imageArray[i + pic.getWidth()] > 0) {
union(imageArray, imageArray[i], i + pic.getWidth());
}
}
if(imageArray[i] > 0)
System.out.println("The root of " + i + " is " + find(imageArray, i));
}
int rootNode;
for (int nodes : imageArray) {
rootNode = find(imageArray, nodes);
if((!sheepCounter.contains(rootNode)) && (rootNode != 0)) {
sheepCounter.add(rootNode);
}
}
System.out.println(sheepCounter.size());
sheepEstimate.setText("Estimate of sheep = " + Integer.toString(sheepCounter.size()));
sheepEstimate.setVisible(true);
}
Regards,
Marcel