資源簡介
ImageComparerUI——基于Java語言實(shí)現(xiàn)的相似圖像識別,基于直方圖比較算法。
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class ImageComparerUI extends JComponent implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private JButton browseBtn;
private JButton histogramBtn;
private JButton compareBtn;
private Dimension mySize;
// image operator
private MediaTracker tracker;
private BufferedImage sourceImage;
private BufferedImage candidateImage;
private double simility;
// command constants
public final static String BROWSE_CMD = "Browse...";
public final static String HISTOGRAM_CMD = "Histogram Bins";
public final static String COMPARE_CMD = "Compare Result";
public ImageComparerUI() {
JPanel btnPanel = new JPanel();
btnPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
browseBtn = new JButton("Browse...");
histogramBtn = new JButton("Histogram Bins");
compareBtn = new JButton("Compare Result");
// buttons
btnPanel.add(browseBtn);
btnPanel.add(histogramBtn);
btnPanel.add(compareBtn);
// setup listener...
browseBtn.addActionListener(this);
histogramBtn.addActionListener(this);
compareBtn.addActionListener(this);
mySize = new Dimension(620, 500);
JFrame demoUI = new JFrame("Similiar Image Finder");
demoUI.getContentPane().setLayout(new BorderLayout());
demoUI.getContentPane().add(this, BorderLayout.CENTER);
demoUI.getContentPane().add(btnPanel, BorderLayout.SOUTH);
de

代碼片段和文件信息
package?com.gloomyfish.image.compare;
//Download?by?http://www.codefans.net
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 ???????? int?ta?=?0?tr?=?0?tg?=?0?tb?=?0;
???????? for(int?col=0;?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[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?);
????}
}
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
-----------?---------??----------?-----??----
?????????????????8145????????????????????5
評論
共有 條評論