java进行图像相识度比对

/**
 * 图片相似度比对
 * 以下部分为两个文件进行像素比对的算法实现,获取文件的像素个数大小,然后使用循环的方式将两张图片的
 * 所有项目进行一一对比,如有任何一个像素不相同,则退出循环,计算相似度
 * 表示两张图片并不是完全匹配
 * @param expectedPath 预期图片
 * @param actualPath   实际图片
 */
public static void pictureComparison( File fileInput, File fileOutPut) {
    BufferedImage bufileInput = null;
    try {
        bufileInput = ImageIO.read(fileInput);
    } catch (IOException e) {
        e.printStackTrace();
    }
    DataBuffer dafileInput = bufileInput.getData().getDataBuffer();
    int sizefileInput = dafileInput.getSize();
    BufferedImage bufileOutPut = null;
    try {
        bufileOutPut = ImageIO.read(fileOutPut);
    } catch (IOException e) {
        e.printStackTrace();
    }
    DataBuffer dafileOutPut = bufileOutPut.getData().getDataBuffer();
    int sizefileOutPut = dafileOutPut.getSize();
    int flagF=0;
    if (sizefileInput == sizefileOutPut) {//像素
        for (int j = 0; j < sizefileInput; j++) {
            if (dafileInput.getElem(j) != dafileOutPut.getElem(j)) {
                flagF++;
            }
        }
    }else {
        flagF=(sizefileInput+sizefileOutPut)/2;//相识度为0
    }
    double similarity=1-(double)(flagF*2)/(sizefileInput+sizefileOutPut);//相似度
    Assert.assertTrue(similarity==1, "图片对比相似度:"+similarity);

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java 可以使用 OpenCV 库进行图像处理和比对。下面是一个简单的示例代码,用于比较两张图片是否相似: ```java import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.CvType; import org.opencv.core.Scalar; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class ImageCompare { public static void main(String[] args) { // 加载 OpenCV 库 System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // 读取图片 Mat img1 = Imgcodecs.imread("path/to/image1.jpg"); Mat img2 = Imgcodecs.imread("path/to/image2.jpg"); // 转换为灰图像 Imgproc.cvtColor(img1, img1, Imgproc.COLOR_BGR2GRAY); Imgproc.cvtColor(img2, img2, Imgproc.COLOR_BGR2GRAY); // 计算直方图 Mat hist1 = new Mat(); Mat hist2 = new Mat(); Imgproc.calcHist( new Mat[]{img1}, new MatOfInt(0), new Mat(), hist1, new MatOfInt(256), new MatOfFloat(0, 256) ); Imgproc.calcHist( new Mat[]{img2}, new MatOfInt(0), new Mat(), hist2, new MatOfInt(256), new MatOfFloat(0, 256) ); // 比较直方图 double correlation = Imgproc.compareHist(hist1, hist2, Imgproc.HISTCMP_CORREL); // 输出比较结果 System.out.println("Correlation: " + correlation); if (correlation > 0.9) { System.out.println("Images are similar."); } else { System.out.println("Images are not similar."); } } } ``` 这个示例程序使用 OpenCV 计算两张图片的直方图,并使用相关系数(correlation)比较两张图片的相似。如果相关系数大于 0.9,则认为两张图片相似。 需要注意的是,这个方法只能检测两张图片是否相似,而不能检测图片中的物体是否相同。如果需要检测物体是否相同,可以使用 OpenCV 的物体识别功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值