import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
public class ChainCalculator {
private ArrayList<ArrayList<Integer>> chainArray;
private int imageHeight;
private int[][] imageArray;
private int[] tempArray;
private int startX;
private int startY;
private final int pixelIgnoranceFactor = 4;
public ChainCalculator() {
chainArray = new ArrayList<ArrayList<Integer>>();
ImageToArray ita = new ImageToArray();
try {
tempArray = ita.getArray("C:\\5050.jpg");
} catch (Exception e) {
e.printStackTrace();
}
imageHeight = ita.getHeight();
toDoubleArray();
}
public void toDoubleArray() {
imageArray = new int[imageHeight][imageHeight];
for (int x = 0 ; x < imageHeight; x++) {
for (int y= 0 ; y < imageHeight; y++) {
imageArray[x][y] = tempArray[x+y*imageHeight];
}
}
}
public void calculateChains() {
BufferedWriter out = null;
String toPrint = "";
try {
out = new BufferedWriter(new FileWriter("c:\\imageTest.txt"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ArrayList<Integer> chain = new ArrayList<Integer>();
for (int x = 0 ; x < imageHeight; x++) {
for (int y = 0 ; y < imageHeight; y++) {
int pixel = imageArray[x][y];
if (pixel == 1 && onlyHasOneNeighbour(x, y)) {
chain.add(x);chain.add(y);chain.add(0);
startX = x;startY = y;
imageArray[x][y] = 0;
neighbourAnalysis(x,y, chain, false);
System.out.println("chain added of size: "+chain.size());
chainArray.add(chain);
chain = new ArrayList<Integer>();
//toPrint += printAway();
}
}
}
for (int x = 0 ; x < imageHeight; x++) {
for (int y = 0 ; y < imageHeight; y++) {
int pixel = imageArray[x][y];
if (pixel == 1) {
chain.add(x);chain.add(y);chain.add(0);
startX = x;startY = y;
imageArray[x][y] = 0;
neighbourAnalysis(x,y, chain, false);
System.out.println("chain added of size: "+chain.size());
chainArray.add(chain);
chain = new ArrayList<Integer>();
//toPrint += printAway();
}
}
}
for (int i = 0; i<chainArray.size(); i++) {
System.out.println(i+" : " + chainArray.get(i).size());
}
try {
out.write(toPrint);
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public boolean onlyHasOneNeighbour(int x, int y){
int numberOfNeighbours = 0;
for(int neighbourX = -1; neighbourX <=1;neighbourX++){
for(int neighbourY = -1; neighbourY <=1; neighbourY++){
if(imageArray[x+neighbourX][y+neighbourY] == 1)
if(neighbourX == 0 && neighbourY == 0);
else numberOfNeighbours++;
}
}
if(numberOfNeighbours == 1) return true;
else return false;
}
public String printAway() {
String toPrint = "";
for (int i = 0; i<imageHeight ; i++) {
for (int j = 0; j<imageHeight; j++) {
toPrint += imageArray[j][i];
}
toPrint += "\n";
}
toPrint+= "\n";
return toPrint;
}
public void neighbourAnalysis(int x, int y, ArrayList<Integer> chain, boolean last) {
String s = "";
if (chain.get(chain.size()-1) == 0) {
s = "12345678";
}
else {
int previous = chain.get(chain.size()-1);
s = validSet(previous);
}
if (y > 0 && x > 0 && imageArray[x-1][y-1] == 1 && s.contains("1")) {
chain.add(1);
imageArray[x-1][y-1] = 0;
if(last) return;
neighbourAnalysis(x-1,y-1,chain, false);
}
else if (y > 0 && imageArray[x][y-1] == 1 && s.contains("2")) {
chain.add(2);
imageArray[x][y-1] = 0;
if(last) return;
neighbourAnalysis(x,y-1,chain, false);
}
else if (y > 0 && x < imageHeight-1 && imageArray[x+1][y-1] == 1 && s.contains("3")) {
chain.add(3);
imageArray[x+1][y-1] = 0;
if(last) return;
neighbourAnalysis(x+1,y-1,chain, false);
}
else if (x > 0 && imageArray[x-1][y] == 1 && s.contains("4")) {
chain.add(4);
imageArray[x-1][y] = 0;
if(last) return;
neighbourAnalysis(x-1,y,chain, false);
}
else if (x < imageHeight-1 && imageArray[x+1][y] == 1 && s.contains("5")) {
chain.add(5);
imageArray[x+1][y] = 0;
if(last) return;
neighbourAnalysis(x+1,y,chain, false);
}
else if (y < imageHeight-1 && x > 0 && imageArray[x-1][y+1] == 1 && s.contains("6")) {
chain.add(6);
imageArray[x-1][y+1] = 0;
if(last) return;
neighbourAnalysis(x-1,(y+1),chain, false);
}
else if (y < imageHeight-1 && imageArray[x][y+1] == 1 && s.contains("7")) {
chain.add(7);
imageArray[x][y+1] = 0;
if(last) return;
neighbourAnalysis(x,(y+1),chain, false);
}
else if (y < imageHeight-1 && x < imageHeight-1
&& imageArray[x+1][y+1] == 1 && s.contains("8")) {
chain.add(8);
imageArray[x+1][y+1] = 0;
if(last) return;
neighbourAnalysis(x+1,(y+1),chain, false);
}
else if(!(chain.size() <= pixelIgnoranceFactor) && !last){
isThisACircle(x, y, chain);
}
}
public void isThisACircle(int x, int y, ArrayList<Integer> chain){
int differenceX = (x - startX);
int differenceY = (y - startY);
if(-1 <= differenceX && differenceX <= 1 &&
-1 <= differenceY && differenceY <= 1){
imageArray[startX][startY] = 1;
neighbourAnalysis(x, y, chain, true);
}
}
public String validSet(int i) {
if (i == 1) return "23461";
else if (i == 2) return "13452";
else if (i == 3) return "12583";
else if (i == 4) return "12674";
else if (i == 5) return "23785";
else if (i == 6) return "14786";
else if (i == 7) return "46857";
else if (i == 8) return "35678";
return null;
}
public ArrayList<ArrayList<Integer>> getChainArray() {
return chainArray;
}
public void removeSingleEntryChains() {
for (int i = 0; i < chainArray.size(); i++) {
if (chainArray.get(i).size() <= pixelIgnoranceFactor) {
chainArray.remove(i);
i--;
}
}
}
public int getImageHeight(){
return imageHeight;
}
}
/*
(1):x-1,y-1: (2):x,y-1: (3):x+1,y-1
(4):x-1,y : ( ):x,y : (5):x+1,y
(6):x-1,y+1: (7):x,y+1: (8):x+1,y+1
(1):x-1,y-1: (2):x,y-1: (3):x+1,y-1
(8):x-1,y : (9):x,y : (4):x+1,y
(7):x-1,y+1: (6):x,y+1: (5):x+1,y+1
*/
No comments:
Post a Comment