java直方图文件相似度比较 指纹提取存储 不用javacv
时间: 2023-12-03 15:05:08 浏览: 144
Compic.zip_Compic_JAVACV
要实现指纹提取并进行存储,可以使用Java自带的图像处理库ImageIO来读取图像文件,并使用Java自带的数据分析库Apache Commons Math来计算直方图和指纹,并将指纹存储在文件中。
以下是一个示例代码:
```java
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import javax.imageio.ImageIO;
import org.apache.commons.math3.stat.descriptive.moment.Mean;
import org.apache.commons.math3.stat.descriptive.summary.Sum;
public class FingerprintExtract {
public static void main(String[] args) throws IOException {
// 读取图像文件
BufferedImage img = ImageIO.read(new File("image.jpg"));
// 计算直方图
int[] hist = calcHistogram(img);
// 计算指纹
int[] fingerprint = calcFingerprint(hist);
// 存储指纹
saveFingerprint(fingerprint, "fingerprint.txt");
}
private static int[] calcHistogram(BufferedImage img) {
int[] hist = new int[256];
for (int y = 0; y < img.getHeight(); y++) {
for (int x = 0; x < img.getWidth(); x++) {
int pixel = img.getRGB(x, y);
int r = (pixel >> 16) & 0xff;
int g = (pixel >> 8) & 0xff;
int b = pixel & 0xff;
int gray = (r + g + b) / 3;
hist[gray]++;
}
}
return hist;
}
private static int[] calcFingerprint(int[] hist) {
int[] fingerprint = new int[64];
double[] histNorm = normalize(hist);
for (int i = 0; i < fingerprint.length; i++) {
int start = i * 4;
double max = Double.NEGATIVE_INFINITY;
int maxIndex = -1;
for (int j = start; j < start + 4; j++) {
if (histNorm[j] > max) {
max = histNorm[j];
maxIndex = j;
}
}
fingerprint[i] = maxIndex;
}
return fingerprint;
}
private static double[] normalize(int[] hist) {
double[] histNorm = new double[hist.length];
int numPixels = hist.length;
Mean mean = new Mean();
double histMean = mean.evaluate(hist);
for (int i = 0; i < hist.length; i++) {
histNorm[i] = hist[i] / (double) numPixels - histMean;
}
return histNorm;
}
private static void saveFingerprint(int[] fingerprint, String filename) throws IOException {
PrintWriter writer = new PrintWriter(filename);
for (int i = 0; i < fingerprint.length; i++) {
writer.print(fingerprint[i]);
if (i < fingerprint.length - 1) {
writer.print(",");
}
}
writer.close();
}
}
```
该代码读取一个图像文件,计算它的直方图和指纹,并将指纹存储在文件中。其中,`calcHistogram`方法用于计算直方图,`calcFingerprint`方法用于计算指纹,`normalize`方法用于将直方图归一化,以便计算指纹。`saveFingerprint`方法用于将指纹存储在文件中。指纹用逗号分隔,并存储在一个文本文件中。
阅读全文