/Users/lyon/j4p/src/j2d/ShortImageBeans.java
|
1 package j2d;
2
3 import ip.transforms.Kernels;
4 import math.Mat1;
5
6 import java.awt.*;
7 import java.util.Vector;
8
9 /**
10 * GPL code by DocJava, Inc.
11 * User: lyon
12 * Date: Mar 5, 2003
13 * Time: 5:44:03 PM
14 *
15 */
16 public class ShortImageBeans {
17 private Vector v = new Vector();
18
19 public void add(ShortImageBean sib) {
20 v.addElement(sib);
21 }
22
23 public ShortImageBean[] getShortImageBeans() {
24 ShortImageBean sba[] = new ShortImageBean[v.size()];
25 v.copyInto(sba);
26 return sba;
27 }
28
29
30 public static ShortImageBeans computeComparisons(Image sourceImage,
31 ImageProcessorFactory ipf,
32 int numberOfImages) {
33 ShortImageBeans sibs = new ShortImageBeans();
34
35 for (int i = 1; i < numberOfImages; i++) {
36 ImageProcessorInterface ip = ipf.getProcessor(i);
37 sibs.add(new ShortImageBean(
38 ip.process(sourceImage)));
39 }
40 return sibs;
41 }
42
43 public ShortImageBeans getImageFlows() {
44 ShortImageBeans imageFlows = new ShortImageBeans();
45 int numberOfImages = v.size();
46 ShortImageBean sbi[] = getShortImageBeans();
47 for (int i = 1; i < numberOfImages; i++)
48 imageFlows.add(sbi[i].getImageFlowBean());
49 return imageFlows;
50 }
51
52 public static void print(double s[]) {
53 System.out.println("histogram of scales:");
54 for (int i = 0; i < s.length; i++)
55 System.out.println(s[i]);
56 }
57
58 private byte sigma[][] = null;
59
60 private double subBandHistogram[] = null;
61
62 public ShortImageBean getMaxBean() {
63 ShortImageBean sib[] = getShortImageBeans();
64 int w = sib[0].getWidth();
65 int h = sib[0].getHeight();
66 ShortImageBean maxBean = new ShortImageBean(w, h);
67 short r[][] = maxBean.getR();
68 short g[][] = maxBean.getG();
69 short b[][] = maxBean.getB();
70
71 sigma = new byte[w][h];
72 subBandHistogram = new double[v.size()];
73 computeSigmaMaxForEachPixel(w, h, maxBean, sib, r, g, b);
74 printTheSubbandHistogramSigmaMinus1();
75 return maxBean;
76 }
77
78 public double[][] getOrientation() {
79 ShortImageBean sib = getMaxBean();
80 short r[][] = sib.getR();
81 double o[][] = new double[r.length][r[0].length];
82 float ns[][] = Kernels.getRobinson1();
83 float ew[][] = Kernels.getRobinson3();
84 Image maxImage = sib.getImage();
85 ShortImageBean nsb =
86 new ShortImageBean(
87 ImageUtils.convolution(maxImage, ns));
88 ShortImageBean ewb =
89 new ShortImageBean(
90 ImageUtils.convolution(maxImage, ew));
91 short imaginaryPart[][] = nsb.getR();
92 short realPart[][] = ewb.getR();
93 for (int x = 0; x < o.length; x++)
94 for (int y = 0; y < o.length; y++) {
95 double dy = imaginaryPart[x][y];
96 double dx = realPart[x][y];
97 o[x][y] = Math.atan2(dy, dx);
98 }
99 return o;
100
101 }
102
103 public void add(ShortImageBeans sibs) {
104 ShortImageBean sib[] = getShortImageBeans();
105 for (int i = 0; i < sib.length; i++)
106 add(sib[i]);
107 }
108
109 public ShortImageBeans getOrientationSubBands() {
110 ShortImageBeans orientation = new ShortImageBeans();
111 ShortImageBean sib[] = getShortImageBeans();
112 float ns[][] = Kernels.getRobinson1();
113 float ew[][] = Kernels.getRobinson3();
114 for (int i = 0; i < sib.length; i++) {
115 ShortImageBean nsb =
116 new ShortImageBean(
117 ImageUtils.convolution(sib[i].getImage(), ns));
118 ShortImageBean ewb =
119 new ShortImageBean(
120 ImageUtils.convolution(sib[i].getImage(), ew));
121 orientation.add(nsb);
122 orientation.add(ewb);
123 }
124 return orientation;
125 }
126
127 private void printTheSubbandHistogramSigmaMinus1() {
128 Mat1.normalize(subBandHistogram);
129 print(subBandHistogram);
130 }
131
132 private void computeSigmaMaxForEachPixel(int w, int h, ShortImageBean maxBean, ShortImageBean[] sib, short[][] r, short[][] g, short[][] b) {
133 for (int x = 0; x < w; x++) {
134 for (int y = 0; y < h; y++) {
135 int subBand =
136 selectSubBand(maxBean, x, y, sib, r, g, b);
137 sigma[x][y] = (byte) (subBand + 1);
138
139 }
140 }
141 }
142
143 private int selectSubBand(ShortImageBean maxBean,
144 int x, int y,
145 ShortImageBean sib[],
146 short r[][],
147 short g[][],
148 short b[][]) {
149 int a = maxBean.getAverage(x, y);
150 int subBand = 0;
151 for (int i = 0; i < sib.length; i++) {
152 if (a < sib[i].getAverage(x, y)) {
153 r[x][y] = sib[i].getR()[x][y];
154 g[x][y] = sib[i].getG()[x][y];
155 b[x][y] = sib[i].getB()[x][y];
156 a = maxBean.getAverage(x, y);
157 subBand = i;
158 subBandHistogram[i]++;
159 }
160 }
161 return subBand;
162 }
163
164 public ShortImageBeans() {
165 }
166
167 }
168