文章详情

短信预约-IT技能 免费直播动态提醒

请输入下面的图形验证码

提交验证

短信预约提醒成功

基于Java实现图片相似度对比的示例代码

2024-04-02 19:55

关注

前言

很多时候我们需要将两个图片进行对比,确定两个图片的相似度。一般常用的就是openCv库,这里就是使用openCv进行图片相似度对比。

依赖

<dependency>
          <groupId>org.bytedeco</groupId>
          <artifactId>javacv</artifactId>
          <version>1.3.3</version>
      </dependency>
<dependency>
          <groupId>org.bytedeco</groupId>
          <artifactId>javacv-platform</artifactId>
          <version>1.3.3</version>
</dependency>

基本算法

基本算法

1、判断高度是否一致,如果不一致,需要截取到高度一致

2、截取算法

a、因为图片有通用的顶部bar和底部bar,需要先找到底部bar。

b、截取长图片的部分,然后和底部bar拼接,就完成了图片截取。

c、这里设置一个默认的宽度,然后对比,找到相同部分,就是底部bar。

相关代码

package com.test.image;
 
import org.bytedeco.javacpp.BytePointer;
import org.bytedeco.javacpp.opencv_core;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import static org.bytedeco.javacpp.opencv_core.*;
import static org.bytedeco.javacpp.opencv_imgcodecs.imread;
import static org.bytedeco.javacpp.opencv_imgcodecs.imwrite;
import static org.bytedeco.javacpp.opencv_imgproc.*;
import static org.bytedeco.javacpp.opencv_imgproc.THRESH_BINARY;
 
public class ImageService {
    private static Logger Log = LoggerFactory.getLogger(ImageService.class);
 
 
    public static void compareImage( String targetImageUrl, String baseImageUrl ){
 
 
        
        opencv_core.Mat targetImage = imread(targetImageUrl);
        opencv_core.Mat baseImage = imread(baseImageUrl);
        Log.info("read image success");
 
 
        
        if(targetImage.size().width()==baseImage.size().width()){
 
 
            
 
            if(targetImage.size().height()!=baseImage.size().height()){
 
                if(targetImage.size().height()>baseImage.size().height()){
                    targetImage = dealLongImage(targetImage.clone(),baseImage.clone());
                } else {
                    baseImage = dealLongImage(baseImage.clone(),targetImage.clone());
                }
            }
 
            
            Mat imageDiff = compareImage(targetImage,baseImage);
 
            double nonZeroPercent = 100 * (double) countNonZero(imageDiff) / (imageDiff.size().height() * imageDiff.size().width());
 
            
            set3ImageTo1("", targetImage, baseImage, showDiff(imageDiff, baseImage), "xxxx.jpg" );
 
 
            imageDiff.release();
            baseImage.release();
            targetImage.release();
 
        } else {
 
        }
    }
 
 
    
    public static int interceptBarHeight( Mat longImage, Mat shortImage ){
 
        
        int imageSearchMaxHeight = 400;
        Mat subImageLong = new Mat(longImage, new Rect(0, longImage.size().height() - imageSearchMaxHeight, longImage.size().width(), imageSearchMaxHeight));
        Mat subImageShort = new Mat(shortImage, new Rect(0, shortImage.size().height() - imageSearchMaxHeight, shortImage.size().width(), imageSearchMaxHeight));
 
        opencv_core.Mat imageDiff = compareImage(subImageLong,subImageShort);
 
        for (int row = imageDiff.size().height() - 1; row > -1; row--) {
            for (int col = 0; col < imageDiff.size().width(); col++) {
                BytePointer bytePointer = imageDiff.ptr(row, col);
                if (bytePointer.get(0) != 0) {
                    imageDiff.release();
                    return imageSearchMaxHeight-row;
                }
            }
        }
        return imageSearchMaxHeight;
    }
 
    
    public static opencv_core.Mat dealLongImage( Mat longImage, Mat shortImage ){
 
        int diffHeight = longImage.size().height()-shortImage.size().height();
        int barHeight = interceptBarHeight(longImage,shortImage);
 
        opencv_core.Mat dealedLongImage = new Mat(longImage,new Rect(0,0,longImage.size().width(),shortImage.size().height()-barHeight) );
 
        opencv_core.Mat imageBar = new Mat(longImage,new Rect(0,longImage.size().height()-barHeight,longImage.size().width(),barHeight) );
 
        opencv_core.Mat dealedLongImageNew = dealedLongImage.clone();
 
        
        vconcat(dealedLongImage, imageBar, dealedLongImageNew);
        imageBar.release();
        dealedLongImage.release();
        return dealedLongImageNew;
    }
 
 
    public static opencv_core.Mat compareImage( opencv_core.Mat targetImage, opencv_core.Mat baseImage ){
 
        opencv_core.Mat targetImageClone = targetImage.clone();
        opencv_core.Mat baseImageColne = baseImage.clone();
        opencv_core.Mat imgDiff1 = targetImage.clone();
        opencv_core.Mat imgDiff = targetImage.clone();
 
        
        cvtColor(targetImage, targetImageClone, COLOR_BGR2GRAY);
        cvtColor(baseImage, baseImageColne, COLOR_BGR2GRAY);
 
        
        subtract(targetImageClone, baseImageColne, imgDiff1);
        subtract(baseImageColne, targetImageClone, imgDiff);
 
        
        addWeighted(imgDiff, 1, imgDiff1, 1, 0, imgDiff);
 
        
        threshold(imgDiff, imgDiff, 24, 255, THRESH_BINARY);
        erode(imgDiff, imgDiff, new opencv_core.Mat());
        dilate(imgDiff, imgDiff, new opencv_core.Mat());
        return imgDiff;
    }
 
 
    private static void set3ImageTo1(String logTag, Mat imageSrc, Mat imageBaseSrc, Mat imageDest, String mergePicResult ) {
 
        if (imageSrc.size().width() == imageDest.size().width() && imageBaseSrc.size().height() == imageDest.size().height()) {
            Mat img = imageSrc.clone();
            Mat imgBase = imageBaseSrc.clone();
            Mat imgDest = imageDest.clone();
            Mat imgLine = new Mat(imgBase.size().height(), 1, CV_8UC3, new Scalar(0, 0, 0, 255));
            Mat largeImg2 = new Mat();
            Mat largeImg3 = new Mat();
            Mat largeImg4 = new Mat();
            Mat largeImg5 = new Mat();
            
            hconcat(img, imgLine, largeImg2);
            hconcat(largeImg2, imgBase, largeImg3);
            hconcat(largeImg3, imgLine, largeImg4);
            hconcat(largeImg4, imgDest, largeImg5);
 
            imwrite( mergePicResult, largeImg5);
 
            img.release();
            imgBase.release();
            imgDest.release();
            imgLine.release();
            largeImg2.release();
            largeImg3.release();
            largeImg4.release();
            largeImg5.release();
        } else {
            Log.info(logTag+" pictures merge failed");
            imwrite( mergePicResult, imageDest);
        }
 
    }
 
 
    private static Mat showDiff(Mat imgDiff, Mat imgBase) {
 
        MatVector rgbFrame = new MatVector();
        Mat imgDest = imgBase.clone();
        split(imgBase, rgbFrame);
        subtract(rgbFrame.get(2), imgDiff, rgbFrame.get(2));
        subtract(rgbFrame.get(0), imgDiff, rgbFrame.get(0));
        addWeighted(rgbFrame.get(1), 1, imgDiff, 1, 0, rgbFrame.get(1));
        merge(rgbFrame, imgDest);
        return imgDest;
    }
 
 
    public static void main( String[] args ){
 
        String targetImageUrl = "2022-03-15-11-37-35-2ouA9yi9gjsGWHDAoaZTaNe4awr0xSlohFq0gF0m.png";
        String baseImageUrl = "2022-03-15-11-37-38-njH2kVzd3boX1i8q8bLCfnnIj8xTLyHhHufgs9rp.png";
 
        compareImage(targetImageUrl,baseImageUrl);
    }
 
}

到此这篇关于基于Java实现图片相似度对比的示例代码的文章就介绍到这了,更多相关Java图片相似度对比内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

阅读原文内容投诉

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

软考中级精品资料免费领

  • 历年真题答案解析
  • 备考技巧名师总结
  • 高频考点精准押题
  • 2024年上半年信息系统项目管理师第二批次真题及答案解析(完整版)

    难度     813人已做
    查看
  • 【考后总结】2024年5月26日信息系统项目管理师第2批次考情分析

    难度     354人已做
    查看
  • 【考后总结】2024年5月25日信息系统项目管理师第1批次考情分析

    难度     318人已做
    查看
  • 2024年上半年软考高项第一、二批次真题考点汇总(完整版)

    难度     435人已做
    查看
  • 2024年上半年系统架构设计师考试综合知识真题

    难度     224人已做
    查看

相关文章

发现更多好内容

猜你喜欢

AI推送时光机
位置:首页-资讯-后端开发
咦!没有更多了?去看看其它编程学习网 内容吧
首页课程
资料下载
问答资讯