package Demo;
import java.awt.image.BufferedImage;
public class HistogramFilter {
private int redBins;
private int greenBins;
private int blueBins;
public HistogramFilter() {
redBins = greenBins = blueBins = 4;
}
public void setRedBinCount(int redBinCount) {
this.redBins = redBinCount;
}
public void setGreenBinCount(int greenBinCount) {
this.greenBins = greenBinCount;
}
public void setBlueBinCount(int blueBinCount) {
this.blueBins = blueBinCount;
}
public float[] filter(BufferedImage src, BufferedImage dest) {
int width = src.getWidth();
int height = src.getHeight();
int[] inPixels = new int[width*height];
float[] histogramData = new float[redBins * greenBins * blueBins];
getRGB( src, 0, 0, width, height, inPixels );
int index = 0;
int redIdx = 0, greenIdx = 0, blueIdx = 0;
int singleIndex = 0;
float total = 0;
for(int row=0; row<height; row++) {
int ta = 0, tr = 0, tg = 0, tb = 0;
for(int col=0; col<width; col++) {
index = row * width + col;
ta = (inPixels[index] >> 24) & 0xff;
tr = (inPixels[index] >> 16) & 0xff;
tg = (inPixels[index] >> 8) & 0xff;
tb = inPixels[index] & 0xff;
redIdx = (int)getBinIndex(redBins, tr, 255);
greenIdx = (int)getBinIndex(greenBins, tg, 255);
blueIdx = (int)getBinIndex(blueBins, tb, 255);
singleIndex = redIdx + greenIdx * redBins + blueIdx * redBins * greenBins;
histogramData[singleIndex] += 1;
total += 1;
}
}
// start to normalize the histogram data
for (int i = 0; i < histogramData.length; i++)
{
histogramData[i] = histogramData[i] / total;
}
return histogramData;
}
private float getBinIndex(int binCount, int color, int colorMaxValue) {
float binIndex = (((float)color)/((float)colorMaxValue)) * ((float)binCount);
if(binIndex >= binCount)
binIndex = binCount - 1;
return binIndex;
}
public int[] getRGB( BufferedImage image, int x, int y, int width, int height, int[] pixels ) {
int type = image.getType();
if ( type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB )
return (int [])image.getRaster().getDataElements( x, y, width, height, pixels );
return image.getRGB( x, y, width, height, pixels, 0, width );
}
}
最近下载更多
long123_356 LV8
2024年6月22日
genyuan2014 LV6
2024年4月30日
xingxing1234 LV10
2023年3月22日
公共分类 LV1
2022年10月18日
kgq_kong LV1
2022年8月31日
zinshao LV12
2022年7月20日
微信网友_5992582549164032 LV6
2022年6月29日
wdm0408 LV1
2022年2月22日
.rabbit LV1
2021年12月2日
xierongsong LV1
2021年10月25日
最近浏览更多
微信网友_7074062297419776
2024年7月10日
暂无贡献等级
long123_356 LV8
2024年6月22日
微笑刺客 LV21
2024年5月30日
3334004690 LV11
2024年5月28日
genyuan2014 LV6
2024年4月30日
晨爽明宇 LV1
2024年3月4日
骆红亮
2024年2月29日
暂无贡献等级
xingxing1234 LV10
2023年3月22日
hadesxxx
2022年12月10日
暂无贡献等级
arkic1
2022年12月1日
暂无贡献等级

