package dokumenty; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Queue; import pl.wroc.pwr.IOperator; import pl.wroc.pwr.IOperator1P; import pl.wroc.pwr.imagechannel.IImageChannel; import pl.wroc.pwr.imagechannel.basic.CloneChannel; import pl.wroc.pwr.roi.IROI; import pl.wroc.pwr.roi.model.ROIPixels; public class SzybkiSplitSeparate implements IOperator1P { private float threshold; private boolean[][] workingChanel; private boolean[][] fastQueueMap; private int xSize; private int ySize; private List imageSegments; private ROIPixels workingSegment; private Queue positionQueue; private IROI[] result; class Pixel { public int x; public int y; public Pixel( int x, int y ) { this.x = x; this.y = y; } @Override public boolean equals( Object obj ) { Pixel p = ( Pixel )obj; return ( ( p.x == this.x )&&( p.y == this.y ) ); } } public SzybkiSplitSeparate(float threshold) { this.threshold = threshold; } public SzybkiSplitSeparate() { this(0); } private void checkNextPosition( int x, int y ) { if ((x < 0)||(y < 0)||(x >= this.xSize)||(y >= this.ySize)) { return; } if (this.workingChanel[x][y]) { if (!this.fastQueueMap[x][y]) { Pixel nextPosition = new Pixel(x, y); this.positionQueue.add(nextPosition); this.fastQueueMap[x][y] = true; } } } private void process( ) { while (!this.positionQueue.isEmpty()) { Pixel position = this.positionQueue.poll(); this.fastQueueMap[position.x][position.y] = false; this.workingChanel[position.x][position.y] = false; this.workingSegment.quickAdd(position.x, position.y); int x = position.x; int y = position.y; this.checkNextPosition(x - 1, y + 0); this.checkNextPosition(x + 1, y + 0); this.checkNextPosition(x + 0, y - 1); this.checkNextPosition(x + 0, y + 1); this.checkNextPosition(x - 1, y - 1); this.checkNextPosition(x + 1, y - 1); this.checkNextPosition(x - 1, y + 1); this.checkNextPosition(x + 1, y + 1); } } private void processPixel(int x, int y) { if (this.workingChanel[x][y]) { Pixel position = new Pixel(x, y); this.workingSegment = new ROIPixels(this.xSize, this.ySize); this.positionQueue = new LinkedList(); this.positionQueue.add(position); this.fastQueueMap[position.x][position.y] = true; this.process(); this.imageSegments.add(this.workingSegment); } } public IROI[] apply(IImageChannel imageChannel) { this.xSize = imageChannel.getXSize(); this.ySize = imageChannel.getYSize(); this.workingChanel = new boolean[this.xSize][this.ySize]; this.fastQueueMap = new boolean[this.xSize][this.ySize]; for (int y = 0; y < this.ySize; y++) { for (int x = 0; x < this.xSize; x++) { this.workingChanel[x][y] = imageChannel.getValue(x, y) > this.threshold; } } this.imageSegments = new ArrayList(); for (int y = 0; y < this.ySize; y++) { for (int x = 0; x < this.xSize; x++) { this.processPixel(x, y); } } this.result = new IROI[this.imageSegments.size()]; for (int i = 0; i < this.result.length; i++) { this.result[i] = this.imageSegments.get(i); } return this.result; } }