package com.image.compare;
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);
}
}
最近下载更多
微笑刺客 LV21
2024年5月30日
一头土猪 LV7
2021年12月22日
okliu123456 LV24
2021年12月14日
与你同行2019 LV11
2019年11月26日
LYXNAME LV1
2018年7月19日
马昌平 LV2
2018年3月16日
dagf113225 LV68
2018年3月12日
andinker LV10
2018年2月21日
Black_杰ck LV1
2017年12月25日
weizhi LV5
2017年12月4日
最近浏览更多
lipanlong LV10
2024年12月3日
sswert LV2
2024年9月12日
微笑刺客 LV21
2024年5月30日
liyonggang LV2
2024年1月19日
lvdong2023 LV10
2023年5月17日
13655961071 LV5
2023年5月4日
xuaniw LV4
2022年10月17日
微信网友_5992582549164032 LV6
2022年6月29日
ttppbb LV4
2022年6月29日
eddie2260 LV2
2022年6月17日

