`
yaerfeng1989
  • 浏览: 226428 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

分享一个网上的图片识别程序

    博客分类:
  • java
阅读更多

基于Java语言实现的相似图像识别,基于直方图比较算法。

原创不易,转载请注明出处:分享一个网上的图片识别程序

HistogramFilter.java

package com.zuidaima.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);
	}

}

	    			


 下载地址:http://www.zuidaima.com/share/1550463398382592.htm

 

 

 

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics