📄 histogram.java
字号:
* Gives number of histogram bands.
* @return number of bands, as an integer (1 if the source of the
* histogram is a greyscale image, 3 if the source is a colour image).
*/
public int getNumBands() {
return bands;
}
/**
* Gives number of samples taken from source image.
* @return number of samples, as an integer.
*/
public int getNumSamples() {
return samples;
}
/**
* Retrieves the frequency of occurrence of a specified grey level.
* @param value pixel value for which a frequency is required
* @return frequency as an integer (0 or greater).
* @exception HistogramException if source is not a greyscale image.
*/
public int getFrequency(int value) throws HistogramException {
if (sourceIsGrey())
return freq[0][value];
else
throw new HistogramException(BAND_ERROR);
}
/**
* Retrieves the frequency of occurrence of a particular value
* in a given band.
* @param band band for which a frequency is required (0, 1 or 2)
* @param value pixel value for which a frequency is required
* @return frequency as an integer (0 or greater).
*/
public int getFrequency(int band, int value) {
return freq[band][value];
}
/**
* Retrieves the frequency of occurrence of a grey level less than
* or equal to the specified value.
* @param value pixel value for which a cumulative frequency is required
* @return cumulative frequency as an integer (0 or greater).
* @exception HistogramException if source is not a greyscale image.
*/
public int getCumulativeFrequency(int value) throws HistogramException {
if (sourceIsGrey())
return cumFreq[0][value];
else
throw new HistogramException(BAND_ERROR);
}
/**
* Retrieves the frequency of occurrence of values less than or
* equal to the specified value in a given band.
* @param band band for which a cumulative frequency is required
* @param value pixel value for which a cumulative frequency is required
* @return cumulative frequency as an integer (0 or greater).
*/
public int getCumulativeFrequency(int band, int value) {
return cumFreq[band][value];
}
/**
* Gives the smallest frequency recorded in the histogram.
* @return minimum frequency as an integer (0 or greater).
* @exception HistogramException if source is not a greyscale image.
*/
public int getMinFrequency() throws HistogramException {
if (sourceIsGrey())
return minFreq[0];
else
throw new HistogramException(BAND_ERROR);
}
/**
* Gives the smallest frequency recorded in the histogram.
* @param band band for which a minimum frequency is required (0, 1 or 2)
* @return minimum frequency in that band, as an integer (0 or greater).
*/
public int getMinFrequency(int band) {
return minFreq[band];
}
/**
* Gives the largest frequency recorded in the histogram.
* @return maximum frequency as an integer (0 or greater).
* @exception HistogramException if source is not a greyscale image.
*/
public int getMaxFrequency() throws HistogramException {
if (sourceIsGrey())
return maxFreq[0];
else
throw new HistogramException(BAND_ERROR);
}
/**
* Gives the largest frequency recorded in the histogram.
* @param band band from which maximum frequency is required (0, 1 or 2)
* @return maximum frequency in that band, as an integer (0 or greater).
*/
public int getMaxFrequency(int band) {
return maxFreq[band];
}
/**
* Gives minimum value for which counts have been recorded.
* @return minimum value as an integer (0 or greater).
* @exception HistogramException if source is not a greyscale image.
*/
public int getMinValue() throws HistogramException {
if (sourceIsGrey())
return minValue[0];
else
throw new HistogramException(BAND_ERROR);
}
/**
* Gives minimum value for which counts have been recorded
* in the specified band.
* @param band band for which a minimum value is required (0, 1 or 2)
* @return minimum value in that band, as an integer (0 or greater).
*/
public int getMinValue(int band) {
return minValue[band];
}
/**
* Gives maximum value for which counts have been recorded.
* @return maximum value, as an integer (0 or greater).
* @exception HistogramException if source is not a greyscale image.
*/
public int getMaxValue() throws HistogramException {
if (sourceIsGrey())
return maxValue[0];
else
throw new HistogramException(BAND_ERROR);
}
/**
* Gives maximum value for which counts have been recorded
* in the specified band.
* @param band the band for which maximum value is required (0, 1 or 2)
* @return maximum value in that band, as an integer (0 or greater).
*/
public int getMaxValue(int band) {
return maxValue[band];
}
/**
* Gives mean value of a greyscale histogram.
* @return mean value, as a double-precision real number.
* @exception HistogramException if source is not a greyscale image.
*/
public double getMeanValue() throws HistogramException {
if (sourceIsGrey())
return meanValue[0];
else
throw new HistogramException(BAND_ERROR);
}
/**
* Gives mean value in one band of a colour histogram.
* @param band the band for which mean value is required (0, 1 or 2)
* @return mean value in the specified band, as a double-precision
* real number.
*/
public double getMeanValue(int band) {
return meanValue[band];
}
/////////////////////////// NON-PUBLIC METHODS ////////////////////////////
/**
* Allocates storage for arrays.
*/
private void allocateStorage() {
freq = new int[3][256];
cumFreq = new int[3][256];
minValue = new int[3];
maxValue = new int[3];
minFreq = new int[3];
maxFreq = new int[3];
meanValue = new double[3];
}
/**
* Initializes arrays and other instance variables properly.
*/
private void initialize() {
bands = samples = 0;
for (int i = 0; i < 3; ++i)
for (int j = 0; j < 256; ++j)
freq[i][j] = cumFreq[i][j] = 0;
for (int i = 0; i < 3; ++i) {
minValue[i] = maxValue[i] = 0;
minFreq[i] = maxFreq[i] = 0;
meanValue[i] = 0.0;
}
}
/**
* Accumulates histogram data by retrieving pixel values
* from the specified image.
* @param image BufferedImage for which a histogram must be calculated
* @exception HistogramException if the image type is not supported.
*/
private void accumulateFrequencies(BufferedImage image)
throws HistogramException {
if (image.getType() == BufferedImage.TYPE_BYTE_BINARY
|| image.getType() == BufferedImage.TYPE_USHORT_GRAY)
throw new HistogramException("invalid image type");
Raster raster = image.getRaster();
if (image.getType() == BufferedImage.TYPE_BYTE_GRAY) {
for (int y = 0; y < image.getHeight(); ++y)
for (int x = 0; x < image.getWidth(); ++x)
++freq[0][raster.getSample(x, y, 0)];
}
else {
int[] value = new int[3];
for (int y = 0; y < image.getHeight(); ++y)
for (int x = 0; x < image.getWidth(); ++x) {
raster.getPixel(x, y, value);
++freq[0][value[0]];
++freq[1][value[1]];
++freq[2][value[2]];
}
}
}
/**
* Computes statistics from histogram data.
*/
private void computeStatistics() {
int i, j;
for (i = 0; i < bands; ++i) {
// Compute cumulative histogram
cumFreq[i][0] = freq[i][0];
for (j = 1; j < 256; ++j)
cumFreq[i][j] = cumFreq[i][j-1] + freq[i][j];
// Find minimum value
for (j = 0; j < 256; ++j) {
if (freq[i][j] > 0) {
minValue[i] = j;
break;
}
}
// Find maximum value
for (j = 255; j >= 0; --j) {
if (freq[i][j] > 0) {
maxValue[i] = j;
break;
}
}
// Find lowest and highest frequencies, and determine mean
minFreq[i] = Integer.MAX_VALUE;
for (j = 0; j < 256; ++j) {
if (freq[i][j] < minFreq[i])
minFreq[i] = freq[i][j];
else if (freq[i][j] > maxFreq[i])
maxFreq[i] = freq[i][j];
meanValue[i] += (double) (j*freq[i][j]);
}
meanValue[i] /= (double) samples;
}
}
/**
* Outputs an integer in right-justified form.
* @param value integer value to be formatted
* @param out destination for the formatted value
*/
private void writeValue(int value, PrintWriter out) {
if (value > 99999)
out.print(" " + value);
else if (value > 9999)
out.print(" " + value);
else if (value > 999)
out.print(" " + value);
else if (value > 99)
out.print(" " + value);
else if (value > 9)
out.print(" " + value);
else
out.print(" " + value);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -