WatershedStructure.java

Go to the documentation of this file.
00001 
00006 package watershed;
00007 
00008 /*
00009  * Watershed algorithm
00010  *
00011  * Copyright (c) 2003 by Christopher Mei (christopher.mei@sophia.inria.fr)
00012  *
00013  * This plugin is free software; you can redistribute it and/or modify
00014  * it under the terms of the GNU General Public License version 2 
00015  * as published by the Free Software Foundation.
00016  *
00017  * This program is distributed in the hope that it will be useful,
00018  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  * GNU General Public License for more details.
00021  *
00022  * You should have received a copy of the GNU General Public License
00023  * along with this plugin; if not, write to the Free Software
00024  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00025  */
00026 
00027 import java.awt.Rectangle;
00028 import java.util.ArrayList;
00029 import java.util.Collections;
00030 
00039 public class WatershedStructure {
00040         private ArrayList watershedStructure;
00041 
00042         public WatershedStructure(short[] pixels, int width, int height) {
00043 
00044                 Rectangle r = new Rectangle(0, 0, width, height);
00045 
00046                 int offset, topOffset, bottomOffset, i;
00047 
00048                 watershedStructure = new ArrayList(r.width * r.height);
00049 
00051                 for (int y = r.y; y < (r.y + r.height); y++) {
00052                         offset = y * width;
00053 
00054                         for (int x = r.x; x < (r.x + r.width); x++) {
00055                                 i = offset + x;
00056 
00057                                 int indiceY = y - r.y;
00058                                 int indiceX = x - r.x;
00059 
00060                                 watershedStructure.add(new WatershedPixel(indiceX, indiceY,
00061                                                 pixels[i]));
00062                         }
00063                 }
00064 
00069                 for (int y = 0; y < r.height; y++) {
00070 
00071                         offset = y * width;
00072                         topOffset = offset + width;
00073                         bottomOffset = offset - width;
00074 
00075                         for (int x = 0; x < r.width; x++) {
00076                                 WatershedPixel currentPixel = (WatershedPixel) watershedStructure
00077                                                 .get(x + offset);
00078 
00079                                 if (x + 1 < r.width) {
00080                                         currentPixel
00081                                                         .addNeighbour((WatershedPixel) watershedStructure
00082                                                                         .get(x + 1 + offset));
00083 
00084                                         if (y - 1 >= 0)
00085                                                 currentPixel
00086                                                                 .addNeighbour((WatershedPixel) watershedStructure
00087                                                                                 .get(x + 1 + bottomOffset));
00088 
00089                                         if (y + 1 < r.height)
00090                                                 currentPixel
00091                                                                 .addNeighbour((WatershedPixel) watershedStructure
00092                                                                                 .get(x + 1 + topOffset));
00093                                 }
00094 
00095                                 if (x - 1 >= 0) {
00096                                         currentPixel
00097                                                         .addNeighbour((WatershedPixel) watershedStructure
00098                                                                         .get(x - 1 + offset));
00099 
00100                                         if (y - 1 >= 0)
00101                                                 currentPixel
00102                                                                 .addNeighbour((WatershedPixel) watershedStructure
00103                                                                                 .get(x - 1 + bottomOffset));
00104 
00105                                         if (y + 1 < r.height)
00106                                                 currentPixel
00107                                                                 .addNeighbour((WatershedPixel) watershedStructure
00108                                                                                 .get(x - 1 + topOffset));
00109                                 }
00110 
00111                                 if (y - 1 >= 0)
00112                                         currentPixel
00113                                                         .addNeighbour((WatershedPixel) watershedStructure
00114                                                                         .get(x + bottomOffset));
00115 
00116                                 if (y + 1 < r.height)
00117                                         currentPixel
00118                                                         .addNeighbour((WatershedPixel) watershedStructure
00119                                                                         .get(x + topOffset));
00120                         }
00121                 }
00122 
00123                 Collections.sort(watershedStructure);
00124                 // IJ.showProgress(0.8);
00125         }
00126 
00127         @Override
00128         public String toString() {
00129                 StringBuffer ret = new StringBuffer();
00130 
00131                 for (int i = 0; i < watershedStructure.size(); i++) {
00132                         ret.append(((WatershedPixel) watershedStructure.get(i)).toString());
00133                         ret.append("\n");
00134                         ret.append("Neighbours :\n");
00135 
00136                         ArrayList neighbours = ((WatershedPixel) watershedStructure.get(i))
00137                                         .getNeighbours();
00138 
00139                         for (int j = 0; j < neighbours.size(); j++) {
00140                                 ret.append(((WatershedPixel) neighbours.get(j)).toString());
00141                                 ret.append("\n");
00142                         }
00143                         ret.append("\n");
00144                 }
00145                 return ret.toString();
00146         }
00147 
00148         public int size() {
00149                 return watershedStructure.size();
00150         }
00151 
00152         public WatershedPixel get(int i) {
00153                 return (WatershedPixel) watershedStructure.get(i);
00154         }
00155 }

Generated on Fri Nov 13 08:57:08 2009 for Theba by  doxygen 1.6.1