ImageHistogram.java
2.24 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
/*
* $Id: ImageHistogram.java,v 1.2 2009/04/21 13:31:17 abrighto Exp $
*/
package jsky.image;
import javax.media.jai.Histogram;
import javax.media.jai.PlanarImage;
import javax.media.jai.ROI;
import jsky.image.operator.ImageOps;
/**
* Utility class used to avoid creating the same Histogram twice
* (Once for auto-setting cut levels and once for histogram equalization, if selected).
* The previous arguments and resulting histogram are cached and compared the
* next time getHistogram is called. If the arguments have not changed, the cached
* Histogram is returned.
*/
public class ImageHistogram {
private Histogram _histogram;
private PlanarImage _im;
private int _size;
private double _lowCut;
private double _highCut;
private ROI _roi;
private int _xPeriod;
private int _yPeriod;
/** Default Constructor */
public ImageHistogram() {
}
/**
* Return a histogram for the given image with the given size (number of bins).
* The arguments are the same as those for the JAI histogram image operation.
*/
public Histogram getHistogram(PlanarImage im, int size, double lowCut,
double highCut, ROI roi, int xPeriod, int yPeriod) {
// check if we can use the cached histogram again
if (_histogram != null
&& im == _im
&& size == _size
&& lowCut == _lowCut
&& highCut == _highCut
&& roi.equals(_roi)
&& xPeriod == _xPeriod
&& yPeriod == _yPeriod) {
return _histogram;
}
_im = im;
_size = size;
_lowCut = lowCut;
_highCut = highCut;
_roi = roi;
_xPeriod = xPeriod;
_yPeriod = yPeriod;
int numBands = im.getSampleModel().getNumBands();
int[] numBins = new int[numBands];
double[] lowValue = new double[numBands];
double[] highValue = new double[numBands];
for (int i = 0; i < numBands; i++) {
numBins[i] = size;
lowValue[i] = lowCut;
highValue[i] = highCut;
}
_histogram = ImageOps.histogram(im, roi, xPeriod, yPeriod, numBins, lowValue, highValue);
return _histogram;
}
}