Android 图片相似度比较-SIFT&直方图

因为项目需要,最近研究了一下在Android App内实现两张图片的相似度比较。查了一圈之后发现OpenCV有提供SIFT算法,所以最终决定用SIFT算法实现两张图片的相似度比较。

在正式开始之前,对与OpenCV中提供的SIFT算法先简单说明一下,以免大家像我一样走很多弯路。因为SIFT算法版权的原因,在3.0版本开始,SIFT算法被移出了官方提供的SDK。需要用的人要自己下载Opencv_contrib源码包进行编译。这个过程可以在网上查到,比较繁琐。但比较巧的是这个SIFT算法的版权刚好最近到期,所以官方最近又把SIFT算法加进了SDK中。所以想用的朋友可以直接下载OpenCV 4.4.0版本就可以了。具体OpenCV的接入方法可以参考我的另外一篇文章(https://www.jianshu.com/p/6fdefcdb86de),在这就不再赘述了。

接下来,我们就直接进入正题,如何用SIFT算法实现两张图片的相似度比较。话不多说,直接上代码,看了就自然会了。

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import com.wildma.idcardcamera.R;
import org.opencv.android.Utils;
import org.opencv.core.DMatch;
import org.opencv.core.Mat;
import org.opencv.core.MatOfDMatch;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.features2d.DescriptorMatcher;
import org.opencv.features2d.SIFT;
import java.util.ArrayList;
import java.util.List;

public class SIFTTest {

  public static boolean isTemplateMatch(Context mContext, Bitmap srcBitmap) {

    float nndrRatio =0.7f;//邻近距离阀值,这里设置既定值为0.7,该值可自行调整

    BitmapFactory.Options options =new BitmapFactory.Options();

    options.inPreferredConfig = Bitmap.Config.ARGB_8888;

    Bitmap bmp1 = BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.template1, options);

    Mat tempMat =new Mat();

    Utils.bitmapToMat(bmp1, tempMat);

    Mat srcMat =new Mat();

    Utils.bitmapToMat(srcBitmap, srcMat);

    MatOfKeyPoint templateKeyPoints =new MatOfKeyPoint();

    MatOfKeyPoint srcKeyPoints =new MatOfKeyPoint();

    //初始化SIFT
    SIFT siftDetector = SIFT.create();

    MatOfKeyPoint templateDescriptors =new MatOfKeyPoint();

    MatOfKeyPoint srcDescriptors =new MatOfKeyPoint();

    DescriptorMatcher descriptorMatcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);

    //获取模板图的特征点
    siftDetector.detect(tempMat, templateKeyPoints);

    siftDetector.detect(srcMat, srcKeyPoints);

    siftDetector.compute(tempMat, templateKeyPoints, templateDescriptors);

    siftDetector.compute(srcMat, srcKeyPoints, srcDescriptors);

    List matches =new ArrayList<>();

    //获取最佳匹配点列表
    descriptorMatcher.knnMatch(templateDescriptors, srcDescriptors, matches,2);

    int matchCount =0;

    for (int i =0; i < matches.size(); i++) {

        MatOfDMatch match = matches.get(i);

        DMatch[] array = match.toArray();

        DMatch m1 = array[0];

        DMatch m2 = array[1];

        //用邻近距离比值法(NDDR)计算匹配点数
        if (m1.distance <= m2.distance * nndrRatio) {

            ++matchCount;

        }

    }

    Log.i("###","======matchCount========" + matchCount);

    if (matchCount >=4) {

        //当匹配后的特征点大于等于 4 个,则认为模板图在原图中(这边匹配特征点个数可以根据实际情况自己设置)
        return true;

    }

    return false;

  }

}

OK,有轮子就是这么简单,这样就实现了用SIFT算法比较两张图片的相似度。因为这是我第一次使用,有可能会存在一些错误,如有发现问题请不吝赐教。

实际运行下来发现这个算法的精度的确挺高,果然名不虚传!但对我来说有一点不理想的地方就是运行速度偏慢,执行一次比较需要1~2s。如果单纯只是想比较两张静态图片(比如缩略图、原图的切片之类),大可不必用这么复杂的算法,比如下面这种直方图比较法,运行起来就快很多。

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import com.wildma.idcardcamera.R;
import org.opencv.android.Utils;
import org.opencv.core.Mat;
import org.opencv.core.MatOfFloat;
import org.opencv.core.MatOfInt;
import org.opencv.imgproc.Imgproc;
import java.util.Arrays;

public class HistCompareTest {

public static boolean isTemplateMatch(Context mContext, Bitmap bitmap) {

    //读取模版图片
    BitmapFactory.Options options =new BitmapFactory.Options();

    options.inPreferredConfig = Bitmap.Config.ARGB_8888;

    Bitmap bmp1 = BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.tag_circle, options);

    Mat mat1 =new Mat();

    Utils.bitmapToMat(bitmap, mat1);

    Mat mat2 =new Mat();

    Utils.bitmapToMat(bmp1, mat2);

    Mat mat_1 =new Mat();

    Mat mat_2 =new Mat();

    //颜色转换
    Imgproc.cvtColor(mat1, mat_1, Imgproc.COLOR_BGR2HSV);

    Imgproc.cvtColor(mat2, mat_2, Imgproc.COLOR_BGR2HSV);

    Mat hist_1 =new Mat();

    Mat hist_2 =new Mat();

    //颜色范围
    MatOfFloat ranges =new MatOfFloat(0f,256f);

    //直方图大小, 越大匹配越精确 (越慢)
    MatOfInt histSize =new MatOfInt(100);

    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);

    // 相似度系数,该值越接近1表示越相似
    double histVal = Imgproc.compareHist(hist_1, hist_2, Imgproc.CV_COMP_CORREL);

    Log.i("###","=====histVal======" + histVal);
    //相似度值大于0.6,则认为两张图片相似(该值可以根据你实际情况自由设置)

    if (histVal >0.6){
        return true;
    }

    return false;

  }

}

好了,这次的分享就到这里啦!希望对你会有所帮助·~

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。