Java實(shí)現(xiàn)圖片比對(duì)算法
采用直方圖原理算法比對(duì)圖片的細(xì)微差別效果比較好,以下兩張區(qū)別很小的圖片識(shí)別效果如下:


識(shí)別結(jié)果:

主要代碼如下:
import javax.imageio.*;
import java.awt.image.*;
import java.awt.*;
import java.io.*;
?
public class PhotoDigest {
? ? public static void main(String[] args) throws Exception {
? ? ? ? float percent = compare(getData("/Users/sun/Downloads/1.jpg"),
? ? ? ? ? ? ? ? getData("/Users/sun/Downloads/3.jpg"));
? ? ? ? if (percent == 0) {
? ? ? ? ? ? System.out.println("無法比較");
? ? ? ? } else {
? ? ? ? ? ? System.out.println("兩張圖片的相似度為:" + percent + "%");
? ? ? ? }
? ? }
?
? ? public static int[] getData(String name) {
? ? ? ? try {
? ? ? ? ? ? BufferedImage img = ImageIO.read(new File(name));
? ? ? ? ? ? BufferedImage slt = new BufferedImage(100, 100,
? ? ? ? ? ? ? ? ? ? BufferedImage.TYPE_INT_RGB);
? ? ? ? ? ? slt.getGraphics().drawImage(img, 0, 0, 100, 100, null);
? ? ? ? ? ? // ImageIO.write(slt,"jpeg",new File("slt.jpg"));
? ? ? ? ? ? int[] data = new int[256];
? ? ? ? ? ? for (int x = 0; x < slt.getWidth(); x++) {
? ? ? ? ? ? ? ? for (int y = 0; y < slt.getHeight(); y++) {
? ? ? ? ? ? ? ? ? ? int rgb = slt.getRGB(x, y);
? ? ? ? ? ? ? ? ? ? Color myColor = new Color(rgb);
? ? ? ? ? ? ? ? ? ? int r = myColor.getRed();
? ? ? ? ? ? ? ? ? ? int g = myColor.getGreen();
? ? ? ? ? ? ? ? ? ? int b = myColor.getBlue();
? ? ? ? ? ? ? ? ? ? data[(r + g + b) / 3]++;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? // data 就是所謂圖形學(xué)當(dāng)中的直方圖的概念
? ? ? ? ? ? return data;
? ? ? ? } catch (Exception exception) {
? ? ? ? ? ? System.out.println("有文件沒有找到,請(qǐng)檢查文件是否存在或路徑是否正確");
? ? ? ? ? ? return null;
? ? ? ? }
? ? }
?
? ? public static float compare(int[] s, int[] t) {
? ? ? ? try {
? ? ? ? ? ? float result = 0F;
? ? ? ? ? ? for (int i = 0; i < 256; i++) {
? ? ? ? ? ? ? ? int abs = Math.abs(s[i] - t[i]);
? ? ? ? ? ? ? ? int max = Math.max(s[i], t[i]);
? ? ? ? ? ? ? ? result += (1 - ((float) abs / (max == 0 ? 1 : max)));
? ? ? ? ? ? }
? ? ? ? ? ? return (result / 256) * 100;
? ? ? ? } catch (Exception exception) {
? ? ? ? ? ? return 0;
? ? ? ? }
? ? }
}以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Springboot如何獲取配置文件application.yml中自定義的變量并使用
這篇文章主要介紹了Springboot中獲取配置文件(application.yml)中自定義的變量并使用,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-09-09
Java?Git?Commit?Message使用規(guī)范
這篇文章主要介紹了Java?Git?Commit?Message使用規(guī)范,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,感興趣的小伙伴可以參考一下,希望對(duì)你的學(xué)習(xí)有所幫助2022-08-08
Springboot Autowried及Resouce使用對(duì)比解析
這篇文章主要介紹了Springboot Autowried及Resouce使用對(duì)比解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
如何在spring boot中進(jìn)行參數(shù)校驗(yàn)示例詳解
這篇文章主要介紹了如何在spring-boot中進(jìn)行參數(shù)校驗(yàn)及l(fā)ombok的使用詳解,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-05-05
Java實(shí)現(xiàn)中序表達(dá)式的實(shí)例代碼
這篇文章主要介紹了Java實(shí)現(xiàn)中序表達(dá)式的實(shí)例代碼,需要的朋友可以參考下2018-08-08
SpringBoot引入Redis報(bào)Redis?command?timed?out兩種異常情況
這篇文章主要給大家介紹了關(guān)于SpringBoot引入Redis報(bào)Redis?command?timed?out兩種異常情況的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08

