1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
private PlanarImage input,output;
private int width,height;
// A matrix for the pixel values, one for the selected labels, one which indicates
// whether a pixel already has a label.
private byte[][] pixels;
private int[][] labels;
// The position, i.e. number of estimated algorithm steps we've already done.
private long position;
// The number of regions on the (finished) task.
private int numberOfRegions;
// Counters for pixels in each region
private Map<Integer,Integer> count;
public Segmentation(PlanarImage im)
{
input = im;
Raster inputRaster = input.getData();
// Create the data structures needed for the algorithm.
width = input.getWidth();
height = input.getHeight();
labels = new int[width][height];
pixels = new byte[width][height];
// Fill the data structures.
for(int h=0;h<height;h++)
for(int w=0;w<width;w++)
{
pixels[w][h] = (byte)inputRaster.getSample(w,h,0);
labels[w][h] = -1;
}
position = 0;
count = new TreeMap<Integer, Integer>();
}
/**
* This method performs the bulk of the processing. It runs a classic stack-based
* region growing algorithm:
* 1 - Find a pixel which is not labeled. Label it and store its coordinates on a
* stack.
* 2 - While there are pixels on the stack, do:
* 3 - Get a pixel from the stack (the pixel being considered).
* 4 - Check its neighboors to see if they are unlabeled and close to the
* considered pixel; if are, label them and store them on the stack.
* 5 - Repeat from 1) until there are no more pixels on the image.
*/
public void run()
{
numberOfRegions = 0;
Stack<Point> mustDo = new Stack<Point>();
for(int h=0;h<height;h++)
for(int w=0;w<width;w++)
{
position++;
// Is this pixel unlabeled?
if (labels[w][h] < 0)
{
numberOfRegions++;
mustDo.add(new Point(w,h));
labels[w][h] = numberOfRegions; // label it as one on a new region
count.put(numberOfRegions,1);
}
// Check all the pixels on the stack. There may be more than one!
while(mustDo.size() > 0)
{
Point thisPoint = mustDo.get(0); mustDo.remove(0);
// Check 8-neighborhood
for(int th=-1;th<=1;th++)
for(int tw=-1;tw<=1;tw++)
{
int rx = thisPoint.x+tw;
int ry = thisPoint.y+th;
// Skip pixels outside of the image.
if ((rx < 0) || (ry < 0) || (ry>=height) || (rx>=width)) continue;
if (labels[rx][ry] < 0)
if (pixels[rx][ry] == pixels[thisPoint.x][thisPoint.y])
{
mustDo.add(new Point(rx,ry));
labels[rx][ry] = numberOfRegions;
count.put(numberOfRegions, count.get(numberOfRegions)+1);
}
} // ended neighbors checking
} // ended stack scan
} // ended image scan
position = width*height;
}
/**
* This method returns the number of regions on the segmetation task. This
* number may be partial if the task has not finished yet.
*/
public int getNumberOfRegions()
{
return numberOfRegions;
}
/**
* This method returns the pixel count for a particular region or -1 if the
* region index is outside of the range.
*/
public int getPixelCount(int region)
{
Integer c = count.get(region);
if (c == null) return -1; else return c;
}
/**
* This method returns the estimated size (steps) for this task. We estimate it as
* being the size of the image.
*/
public long getSize()
{
return width*height;
}
/**
* This method returns the position on the image processing task.
*/
public long getPosition()
{
return position;
}
/**
* This method returns true if the image processing task has finished.
*/
public boolean isFinished()
{
return (position == width*height);
}
/**
* This method returns the output image. It may be sort of corrupted if the image
* processing task is still running.
*/
public PlanarImage getOutput()
{
// Create a new image based on the labels array.
int[] imageDataSingleArray = new int[width*height];
int count=0;
for(int h=0;h<height;h++)
for(int w=0;w<width;w++)
imageDataSingleArray[count++] = labels[w][h];
// Create a Data Buffer from the values on the single image array.
DataBufferInt dbuffer = new DataBufferInt(imageDataSingleArray,
width*height);
// Create a byte data sample model.
SampleModel sampleModel =
RasterFactory.createBandedSampleModel(DataBuffer.TYPE_INT,width,height,1);
// Create a compatible ColorModel.
ColorModel colorModel = PlanarImage.createColorModel(sampleModel);
// Create a WritableRaster.
Raster raster = RasterFactory.createWritableRaster(sampleModel,dbuffer,new Point(0,0));
// Create a TiledImage using the SampleModel and ColorModel.
TiledImage tiledImage = new TiledImage(0,0,width,height,0,0,sampleModel,colorModel);
// Set the data of the tiled image to be the raster.
tiledImage.setData(raster);
return tiledImage;
}
} |
Partager