-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeedsGrower.java
More file actions
189 lines (157 loc) · 4.49 KB
/
Copy pathSeedsGrower.java
File metadata and controls
189 lines (157 loc) · 4.49 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import ij.IJ;
import ij.ImagePlus;
import ij.gui.NewImage;
import ij.process.Blitter;
import ij.process.ColorProcessor;
import ij.process.FloatProcessor;
import ij.process.ImageProcessor;
import ij.process.ImageStatistics;
import java.util.LinkedList;
import java.util.ListIterator;
/**
* @author Alfonso Ridolfo
* @version 0.1
* This class provides methods to plant and grow a collection (a {@code LinkedList} region on the specified {@code ImageProcessor}
*/
public class SeedsGrower {
private LinkedList<Coord2D> seeds;
private ImageProcessor ip;
private LinkedList<Region> regions;
private int w, h;
private byte[] hue;
private byte[] sat;
private byte[] bri;
private float[] gradMap;
private ImagePlus gradIMP;
private ImageStatistics gradStats;
private int[] gradHist;
private float gradThreshold;
private float edgePixMean;
public SeedsGrower(LinkedList<Coord2D> seeds, ImageProcessor ip) {
this.seeds = seeds;
this.regions = new LinkedList<Region>();
this.ip = ip;
w = ip.getWidth();
h = ip.getHeight();
hue = new byte[w*h];
sat = new byte[w*h];
bri = new byte[w*h];
this.gradCalc();
}
/**
* method to compute gradient map and gradient threshold of the input {@code ImageProcessor}
*/
private void gradCalc() {
// copying the input Image
ImagePlus impLP;
ImageProcessor ipLP;
byte[] pixels;
if (ip instanceof ColorProcessor) {
impLP = NewImage.createRGBImage("LowPassCopy", w, h, 1, NewImage.FILL_BLACK);
ipLP = (ColorProcessor) impLP.getProcessor();
}
else {
impLP = NewImage.createFloatImage("LowPassCopy", w, h, 1, NewImage.FILL_BLACK);
ipLP = (FloatProcessor) impLP.getProcessor();
}
ipLP.copyBits(ip, 0, 0, Blitter.COPY);
ipLP.medianFilter();
GradientCalculator gc = new GradientCalculator(ipLP);
gc.calc();
// obtaining gradient data
this.gradMap = gc.getGradFloat();
this.gradIMP = gc.getImage();
this.gradStats = gradIMP.getStatistics();
this.gradHist = gradStats.histogram;
// gc.showGradImage();
int nPixels = w*h;
float sum = 0.0f;
int i = 0;
float counter = 0;
// calculating gradient threshold
while (counter<0.98) {
sum += gradHist[i];
counter = sum/nPixels;
i++;
}
this.gradThreshold = (float) (((i-1)*gradStats.binSize)+this.gradStats.histMin);
// IJ.write("soglia del gradiente ="+this.gradThreshold);
pixels = (byte[]) ip.convertToByte(false).getPixelsCopy();
int offset, l;
sum = 0;
int countEdgePix = 0;
for (int y=0; y<h; y++) {
for (int x=0; x<w; x++){
offset = y*w;
l = x+offset;
if (this.gradMap[l]>this.gradThreshold) {
sum += (pixels[l] & 0xff);
countEdgePix++;
}
}
}
edgePixMean = sum/countEdgePix;
// IJ.write("media dei bordi: "+edgePixMean);
return;
}
/**
* shows segmented output image in a new window
*/
public void showOutput() {
// IJ.write("show output...");
ImagePlus segOutput = NewImage.createRGBImage("Segmented Output", w, h, 1, NewImage.RGB);
ColorProcessor segOutIP = (ColorProcessor) segOutput.getProcessor();
segOutIP.copyBits(ip, 0, 0, Blitter.COPY);
Region current;
ListIterator regLi = regions.listIterator();
int h = 0;
int step = (int) Math.round(200.0f/(regions.size()-1));
while (regLi.hasNext()) {
current = (Region) regLi.next();
this.draw(current, segOutIP, h);
h += step;
}
segOutIP.setHSB(hue, sat, bri);
segOutput.updateAndDraw();
segOutput.show();
}
/**
* draw segmented regions on the output {@code ImagePlus}
* @param current region to draw
* @param segOutIP segmented output {@code ImageProcessor}
* @param color the hue value used to draw region
*/
private void draw(Region current, ImageProcessor segOutIP, int color) {
LinkedList<Coord2D> pixels = current.getPoints();
ListIterator pixLI = pixels.listIterator();
int x, y, offset, l;
Coord2D point;
while (pixLI.hasNext()) {
point = (Coord2D) pixLI.next();
x = (int) point.X();
y = (int) point.Y();
offset = y*w;
l = x+offset;
hue[l] = (byte) color;
sat[l] = (byte) 255;
bri[l] = (byte) 255;
}
}
/**
* method that initializes the {@code Region}s and grows the seeds
*/
public void growSeeds() {
// IJ.write("growing seeds...");
ListIterator sLI = seeds.listIterator();
Coord2D curSeed;
Region rgn;
while (sLI.hasNext()) {
rgn = new Region(ip, this.gradMap, this.gradIMP, this.gradThreshold, this.edgePixMean);
curSeed = (Coord2D) sLI.next();
rgn.grow(curSeed);
regions.add(rgn);
// rgn.showStdVar();
// rgn.showMean();
}
}
}