基于Java实现图片相似度对比的示例代码
前言
很多时候我们需要将两个图片进行对比,确定两个图片的相似度。一般常用的就是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图片相似度对比内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
相关文章