openCV4.4.0 基于SIFT特征值的图像匹配【java】。。。。。搞到吐

2023-11-02 19:30

本文主要是介绍openCV4.4.0 基于SIFT特征值的图像匹配【java】。。。。。搞到吐,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

首先,java使用opengCV4.4.0成功搞出来基于SIFT的图像匹配是很开心的,但是还是要吐槽几句下面这块都是吐槽,干货在下面,代码自取哈。
java是要凉了还是咋地,真的没人搞openCV了么,在网上找了快一个星期真就找不到能用的代码。要么是C++要么是Python,,,我吐了。
部分提供出来的代码也是旧的版本的,旧的我也忍了,代码拿过来还是用不了!说是因为版权原因不能直接用,需要去官网拿opencv_contrib 源码自己去编译!但是opencv的模板匹配图像又不能满足我的需求!就只能去度娘看能不能白嫖到了。
在这里插入图片描述
网上能找到编译好的,但是版本又低,我又不确定是不是编译的jar,又要收费!我这么抠的人会去搞知识付费么?照着攻略自己干!结果看到要准备的一大堆软件,我又又吐了,,,。然后转身发现4.4.0自带SIFT算法,不需要编译!可是网上找不到能用的代码!于是根据折腾了大半个星期的积累尝试自己动手!
openCV的安装与使用应该不用说吧,都走到这一步了,en,应该是不用说了【主要是懒】

先看效果

原图在这里插入图片描述

模板图 模板图有做缩放和旋转处理
在这里插入图片描述

匹配过程
在这里插入图片描述

匹配结果
在这里插入图片描述

再看代码

 public void matchImage(BufferedImage templateImageB, BufferedImage originalImageB) {Mat resT = new Mat();Mat resO = new Mat();//即当detector 又当DetectorSIFT sift = SIFT.create();Mat templateImage = getMatify(templateImageB);Mat originalImage = getMatify(originalImageB);MatOfKeyPoint templateKeyPoints = new MatOfKeyPoint();MatOfKeyPoint originalKeyPoints = new MatOfKeyPoint();//获取模板图的特征点sift.detect(templateImage, templateKeyPoints);sift.detect(originalImage, originalKeyPoints);sift.compute(templateImage, templateKeyPoints, resT);sift.compute(originalImage, originalKeyPoints, resO);List<MatOfDMatch> matches = new LinkedList();DescriptorMatcher descriptorMatcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);System.out.println("寻找最佳匹配");printPic("ptest", templateImage);printPic("ptesO", originalImage);printPic("test", resT);printPic("tesO", resO);/*** knnMatch方法的作用就是在给定特征描述集合中寻找最佳匹配* 使用KNN-matching算法,令K=2,则每个match得到两个最接近的descriptor,然后计算最接近距离和次接近距离之间的比值,当比值大于既定值时,才作为最终match。*/descriptorMatcher.knnMatch(resT, resO, matches, 2);System.out.println("计算匹配结果");LinkedList<DMatch> goodMatchesList = new LinkedList();//对匹配结果进行筛选,依据distance进行筛选matches.forEach(match -> {DMatch[] dmatcharray = match.toArray();DMatch m1 = dmatcharray[0];DMatch m2 = dmatcharray[1];if (m1.distance <= m2.distance * nndrRatio) {goodMatchesList.addLast(m1);}});matchesPointCount = goodMatchesList.size();//当匹配后的特征点大于等于 4 个,则认为模板图在原图中,该值可以自行调整if (matchesPointCount >= 4) {System.out.println("模板图在原图匹配成功!");List<KeyPoint> templateKeyPointList = templateKeyPoints.toList();List<KeyPoint> originalKeyPointList = originalKeyPoints.toList();LinkedList<Point> objectPoints = new LinkedList();LinkedList<Point> scenePoints = new LinkedList();goodMatchesList.forEach(goodMatch -> {objectPoints.addLast(templateKeyPointList.get(goodMatch.queryIdx).pt);scenePoints.addLast(originalKeyPointList.get(goodMatch.trainIdx).pt);});MatOfPoint2f objMatOfPoint2f = new MatOfPoint2f();objMatOfPoint2f.fromList(objectPoints);MatOfPoint2f scnMatOfPoint2f = new MatOfPoint2f();scnMatOfPoint2f.fromList(scenePoints);//使用 findHomography 寻找匹配上的关键点的变换Mat homography = Calib3d.findHomography(objMatOfPoint2f, scnMatOfPoint2f, Calib3d.RANSAC, 3);/*** 透视变换(Perspective Transformation)是将图片投影到一个新的视平面(Viewing Plane),也称作投影映射(Projective Mapping)。*/Mat templateCorners = new Mat(4, 1, CvType.CV_32FC2);Mat templateTransformResult = new Mat(4, 1, CvType.CV_32FC2);templateCorners.put(0, 0, new double[]{0, 0});templateCorners.put(1, 0, new double[]{templateImage.cols(), 0});templateCorners.put(2, 0, new double[]{templateImage.cols(), templateImage.rows()});templateCorners.put(3, 0, new double[]{0, templateImage.rows()});//使用 perspectiveTransform 将模板图进行透视变以矫正图象得到标准图片Core.perspectiveTransform(templateCorners, templateTransformResult, homography);//矩形四个顶点  匹配的图片经过旋转之后就这个矩形的四个点的位置就不是正常的abcd了double[] pointA = templateTransformResult.get(0, 0);double[] pointB = templateTransformResult.get(1, 0);double[] pointC = templateTransformResult.get(2, 0);double[] pointD = templateTransformResult.get(3, 0);//指定取得数组子集的范围
//            int rowStart = (int) pointA[1];
//            int rowEnd = (int) pointC[1];
//            int colStart = (int) pointD[0];
//            int colEnd = (int) pointB[0];//rowStart, rowEnd, colStart, colEnd 好像必须左上右下  没必要从原图扣下来模板图了
//            Mat subMat = originalImage.submat(rowStart, rowEnd, colStart, colEnd);
//            printPic("yppt", subMat);//将匹配的图像用用四条线框出来Imgproc.rectangle(originalImage, new Point(pointA), new Point(pointC), new Scalar(0, 255, 0));/* Core.line(originalImage, new Point(pointA), new Point(pointB), new Scalar(0, 255, 0), 4);//上 A->BCore.line(originalImage, new Point(pointB), new Point(pointC), new Scalar(0, 255, 0), 4);//右 B->CCore.line(originalImage, new Point(pointC), new Point(pointD), new Scalar(0, 255, 0), 4);//下 C->DCore.line(originalImage, new Point(pointD), new Point(pointA), new Scalar(0, 255, 0), 4);//左 D->A*/MatOfDMatch goodMatches = new MatOfDMatch();goodMatches.fromList(goodMatchesList);Mat matchOutput = new Mat(originalImage.rows() * 2, originalImage.cols() * 2, Imgcodecs.IMREAD_COLOR);Features2d.drawMatches(templateImage, templateKeyPoints, originalImage, originalKeyPoints, goodMatches, matchOutput, new Scalar(0, 255, 0), new Scalar(255, 0, 0), new MatOfByte(), 2);Features2d.drawMatches(templateImage, templateKeyPoints, originalImage, originalKeyPoints, goodMatches, matchOutput, new Scalar(0, 255, 0), new Scalar(255, 0, 0), new MatOfByte(), 2);printPic("ppgc", matchOutput);printPic("ytwz", originalImage);} else {System.out.println("模板图不在原图中!");}printPic("模板特征点", resT);}public void printPic(String name, Mat pre) {Imgcodecs.imwrite(name + ".jpg", pre);}/*** 尝试把BufferedImage转换为Mat** @param im* @return*/public Mat getMatify(BufferedImage im) {BufferedImage bufferedImage = toBufferedImageOfType(im, BufferedImage.TYPE_3BYTE_BGR);//将bufferedimage转换为字节数组byte[] pixels = ((DataBufferByte) bufferedImage.getRaster().getDataBuffer()).getData();
//        byte[] pixels = ((DataBufferByte) im.getRaster().getDataBuffer()).getData();Mat image = new Mat(bufferedImage.getHeight(), bufferedImage.getWidth(), CvType.CV_8UC3);image.put(0, 0, pixels);return image;}

当前这个方法可以使用SIFT实现特征值匹配,顺带再附上模板匹配的代码,聊胜于无记录一番

/*** 暂定返回一个点坐标吧,但是这个坐标是相对源图片的,不是最终坐标** @param sourceB* @param templateB*/public PicPoint matchTemplate(BufferedImage sourceB, BufferedImage templateB) {Mat source = getMatify(sourceB);Mat template = getMatify(templateB);//创建于原图相同的大小,储存匹配度Mat result = Mat.zeros(source.rows() - template.rows() + 1, source.cols() - template.cols() + 1, CvType.CV_32FC1);//调用模板匹配方法Imgproc.matchTemplate(source, template, result, Imgproc.TM_SQDIFF_NORMED);//规格化Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1);//获得最可能点,MinMaxLocResult是其数据格式,包括了最大、最小点的位置x、yCore.MinMaxLocResult mlr = Core.minMaxLoc(result);Point matchLoc = mlr.minLoc;//在原图上的对应模板可能位置画一个绿色矩形Imgproc.rectangle(source, matchLoc, new Point(matchLoc.x + template.width(), matchLoc.y + template.height()), new Scalar(0, 255, 0));//将结果输出到对应位置printPic("E:\\study\\CV\\result3.png", source);return new PicPoint(matchLoc);}

里面缺的类只是一个普通的实体,耗时一个星期实现出来,记录学习一下,openCV涉及java的东西实在太少了,如果有需要的代码自取。

/**********************************************补充一下图片转化的代码====================================================

/*** 转换图片类型** @param original* @param type* @return*/public static BufferedImage toBufferedImageOfType(BufferedImage original, int type) {if (original == null) {throw new IllegalArgumentException("original == null");}// Don't convert if it already has correct typeif (original.getType() == type) {return original;}// Create a buffered imageBufferedImage image = new BufferedImage(original.getWidth(), original.getHeight(), type);// Draw the image onto the new bufferGraphics2D g = image.createGraphics();try {g.setComposite(AlphaComposite.Src);g.drawImage(original, 0, 0, null);} finally {g.dispose();}return image;}

ps:这玩意是真的烦,有需要的xdm不明白的可以私聊我。看到都会回复qaq

这篇关于openCV4.4.0 基于SIFT特征值的图像匹配【java】。。。。。搞到吐的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/332814

相关文章

Java图片压缩三种高效压缩方案详细解析

《Java图片压缩三种高效压缩方案详细解析》图片压缩通常涉及减少图片的尺寸缩放、调整图片的质量(针对JPEG、PNG等)、使用特定的算法来减少图片的数据量等,:本文主要介绍Java图片压缩三种高效... 目录一、基于OpenCV的智能尺寸压缩技术亮点:适用场景:二、JPEG质量参数压缩关键技术:压缩效果对比

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

springboot+dubbo实现时间轮算法

《springboot+dubbo实现时间轮算法》时间轮是一种高效利用线程资源进行批量化调度的算法,本文主要介绍了springboot+dubbo实现时间轮算法,文中通过示例代码介绍的非常详细,对大家... 目录前言一、参数说明二、具体实现1、HashedwheelTimer2、createWheel3、n

Java利用docx4j+Freemarker生成word文档

《Java利用docx4j+Freemarker生成word文档》这篇文章主要为大家详细介绍了Java如何利用docx4j+Freemarker生成word文档,文中的示例代码讲解详细,感兴趣的小伙伴... 目录技术方案maven依赖创建模板文件实现代码技术方案Java 1.8 + docx4j + Fr

SpringBoot首笔交易慢问题排查与优化方案

《SpringBoot首笔交易慢问题排查与优化方案》在我们的微服务项目中,遇到这样的问题:应用启动后,第一笔交易响应耗时高达4、5秒,而后续请求均能在毫秒级完成,这不仅触发监控告警,也极大影响了用户体... 目录问题背景排查步骤1. 日志分析2. 性能工具定位优化方案:提前预热各种资源1. Flowable

基于SpringBoot+Mybatis实现Mysql分表

《基于SpringBoot+Mybatis实现Mysql分表》这篇文章主要为大家详细介绍了基于SpringBoot+Mybatis实现Mysql分表的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可... 目录基本思路定义注解创建ThreadLocal创建拦截器业务处理基本思路1.根据创建时间字段按年进

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Springboot @Autowired和@Resource的区别解析

《Springboot@Autowired和@Resource的区别解析》@Resource是JDK提供的注解,只是Spring在实现上提供了这个注解的功能支持,本文给大家介绍Springboot@... 目录【一】定义【1】@Autowired【2】@Resource【二】区别【1】包含的属性不同【2】@

springboot循环依赖问题案例代码及解决办法

《springboot循环依赖问题案例代码及解决办法》在SpringBoot中,如果两个或多个Bean之间存在循环依赖(即BeanA依赖BeanB,而BeanB又依赖BeanA),会导致Spring的... 目录1. 什么是循环依赖?2. 循环依赖的场景案例3. 解决循环依赖的常见方法方法 1:使用 @La