How to identify two similar images?

We can use Opencv to find the similar images,below is a simple java code


import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;

import java.util.Arrays;

public class FaceCompareMain {

    static CascadeClassifier faceDetector;

    static {
        faceDetector = new CascadeClassifier(

    public static void main(String[] args) {
        String basePicPath = "D:\\ib\\face-detact\\src\\pics\\";
        double compareHist = compare_image(basePicPath + "image_1.png", basePicPath + "image_2.png");
        if (compareHist > 0.72) {
        } else {
            System.out.println("not match!");

    public static double compare_image(String img_1, String img_2) {
        Mat mat_1 = conv_Mat(img_1);
        Mat mat_2 = conv_Mat(img_2);

        Mat hist_1 = new Mat();
        Mat hist_2 = new Mat();

        MatOfFloat ranges = new MatOfFloat(0f, 256f);

        MatOfInt histSize = new MatOfInt(1000);

        Imgproc.calcHist(Arrays.asList(mat_1), new MatOfInt(0), new Mat(), hist_1, histSize, ranges);
        Imgproc.calcHist(Arrays.asList(mat_2), new MatOfInt(0), new Mat(), hist_2, histSize, ranges);

        double res = Imgproc.compareHist(hist_1, hist_2, Imgproc.CV_COMP_CORREL);
        return res;

    // "D:\\ib\\face-detact\\src\\com\\company\\a1.jpg"
    private static Mat conv_Mat(String img_1) {
        Mat image0 = Imgcodecs.imread(img_1);

        Mat image = new Mat();
        Imgproc.cvtColor(image0, image, Imgproc.COLOR_BGR2GRAY);

        MatOfRect faceDetections = new MatOfRect();
        faceDetector.detectMultiScale(image, faceDetections);

        for (Rect rect : faceDetections.toArray()) {
            Mat mat = new Mat(image, rect);
            return mat;
        return null;


The similar images´╝Ü