ConvexAnalyzer.java

Go to the documentation of this file.
00001 package theba.core.math;
00002 
00003 import java.awt.Point;
00004 import java.awt.Polygon;
00005 
00006 import theba.core.ImageFunctions;
00007 
00015 public class ConvexAnalyzer {
00016 
00027         public static double getSolidity(short[] data, int width, int height) {
00028                 Polygon pout = getConvexRegion(data, width, height);
00029 
00030                 int cnt = 0;
00031                 int convcnt = 0;
00032 
00033                 for (int xx = 0; xx < width; xx++) {
00034                         for (int yy = 0; yy < height; yy++) {
00035                                 if (data[xx + yy * width] != 0) {
00036                                         cnt++;
00037                                 }
00038                                 if (pout.contains(xx, yy))
00039                                         convcnt++;
00040                         }
00041                 }
00042 
00043                 if (convcnt == 0)
00044                         return 0;
00045                 return (cnt / (double) convcnt);
00046         }
00047 
00048         public static Polygon getConvexRegion(short[] data, int width, int height) {
00049 
00050                 int counter = 0;
00051 
00052                 int i, j, k = 0, m;
00053 
00054                 for (j = 0; j < width; j++) {
00055                         for (i = 0; i < height; i++) {
00056                                 if (data[j + i * width] != 0)
00057                                         counter++;
00058                         }
00059                 }
00060 
00061                 int[] x = new int[counter + 1];
00062                 int[] y = new int[counter + 1];
00063 
00064                 for (j = 0; j < width; j++) {
00065                         for (i = 0; i < height; i++) {
00066                                 if (data[j + i * width] != 0) {
00067                                         x[k] = i;
00068                                         y[k] = j;
00069                                         k++;
00070                                 }
00071                         }
00072                 }
00073 
00074                 // cnvx hull
00075                 int n = counter, min = 0, ney = 0, h, h2, dx, dy, temp, ax, ay;
00076                 double minangle, th, t, v, zxmi = 0;
00077 
00078                 for (i = 1; i < n; i++) {
00079                         if (y[i] < y[min])
00080                                 min = i;
00081                 }
00082 
00083                 temp = x[0];
00084                 x[0] = x[min];
00085                 x[min] = temp;
00086                 temp = y[0];
00087                 y[0] = y[min];
00088                 y[min] = temp;
00089                 min = 0;
00090 
00091                 for (i = 1; i < n; i++) {
00092                         if (y[i] == y[0]) {
00093                                 ney++;
00094                                 if (x[i] < x[min])
00095                                         min = i;
00096                         }
00097                 }
00098                 temp = x[0];
00099                 x[0] = x[min];
00100                 x[min] = temp;
00101                 temp = y[0];
00102                 y[0] = y[min];
00103                 y[min] = temp;
00104 
00105                 min = 0;
00106                 m = -1;
00107                 x[n] = x[min];
00108                 y[n] = y[min];
00109                 if (ney > 0)
00110                         minangle = -1;
00111                 else
00112                         minangle = 0;
00113 
00114                 while (min != n + 0) {
00115                         m = m + 1;
00116                         temp = x[m];
00117                         x[m] = x[min];
00118                         x[min] = temp;
00119                         temp = y[m];
00120                         y[m] = y[min];
00121                         y[min] = temp;
00122 
00123                         min = n;
00124                         v = minangle;
00125                         minangle = 360.0;
00126                         h2 = 0;
00127 
00128                         for (i = m + 1; i < n + 1; i++) {
00129                                 dx = x[i] - x[m];
00130                                 ax = Math.abs(dx);
00131                                 dy = y[i] - y[m];
00132                                 ay = Math.abs(dy);
00133 
00134                                 if (dx == 0 && dy == 0)
00135                                         t = 0.0;
00136                                 else
00137                                         t = (double) dy / (double) (ax + ay);
00138 
00139                                 if (dx < 0)
00140                                         t = 2.0 - t;
00141                                 else {
00142                                         if (dy < 0)
00143                                                 t = 4.0 + t;
00144                                 }
00145                                 th = t * 90.0;
00146 
00147                                 if (th > v) {
00148                                         if (th < minangle) {
00149                                                 min = i;
00150                                                 minangle = th;
00151                                                 h2 = dx * dx + dy * dy;
00152                                         } else {
00153                                                 if (th == minangle) {
00154                                                         h = dx * dx + dy * dy;
00155                                                         if (h > h2) {
00156                                                                 min = i;
00157                                                                 h2 = h;
00158                                                         }
00159                                                 }
00160                                         }
00161                                 }
00162                         }
00163 
00164                         zxmi = zxmi + Math.sqrt(h2);
00165                 }
00166                 m++;
00167 
00168                 int[] hx = new int[m];// ROI polygon array
00169                 int[] hy = new int[m];
00170 
00171                 for (i = 0; i < m; i++) {
00172                         hx[i] = x[i]; // copy to new polygon array
00173                         hy[i] = y[i];
00174                 }
00175 
00176                 // We flip x and y to avoid the coordinates being turned
00177                 Polygon pout = new Polygon(hy, hx, hx.length); // new output
00178 
00179                 return pout;
00180 
00181         }
00182 
00191         public static double getConvexity(short[] data, int width, int height) {
00192                 Polygon pout = getConvexRegion(data, width, height);
00193                 int cnt = 0;
00194                 int convcnt = 0;
00195 
00196                 for (int xx = 0; xx < width; xx++) {
00197                         for (int yy = 0; yy < height; yy++) {
00198                                 if (data[xx + yy * width] != 0) {
00199                                         cnt++;
00200                                 }
00201                                 if (pout.contains(xx, yy))
00202                                         convcnt++;
00203                         }
00204                 }
00205 
00206                 Point start = null;
00207                 for (int xx = 0; xx < width; xx++) {
00208                         for (int yy = 0; yy < height; yy++) {
00209                                 if (data[xx + yy * width] != 0) {
00210                                         start = new Point(xx, yy);
00211                                         xx = width;
00212                                         yy = height;
00213                                 }
00214                         }
00215 
00216                 }
00217 
00218                 for (int xx = 0; xx < width; xx++) {
00219                         for (int yy = 0; yy < height; yy++) {
00220                                 if (data[xx + yy * width] != 0)
00221                                         data[xx + yy * width] = (short) 150;
00222                         }
00223                 }
00224 
00225                 if (start == null) {
00226                         System.out.println("No region found!");
00227                         return 0.0;
00228                 }
00229 
00230                 int count1 = ImageFunctions.countOutlinePixels(data, start.x, start.y, width, height, (short) 100);
00231                 short[] data2 = new short[data.length];
00232                 for (int xx = 0; xx < width; xx++) {
00233                         for (int yy = 0; yy < height; yy++) {
00234                                 if (pout.contains(xx, yy))
00235                                         data2[xx + yy * width] = (short) 0x150;
00236                                 else
00237                                         data2[xx + yy * width] = 0;
00238                         }
00239                 }
00240 
00241                 int count2 = ImageFunctions.countOutlinePixels(data2, start.x, start.y,
00242                                 width, height, (short) 100);
00243 
00244                 return (count2 / (double) count1);
00245                 
00246         }
00247 
00248 }

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