使用OpenCV ORB进行图像匹配

时间:2014-07-15 11:40:20

标签: android opencv image-recognition

我正在编写一个应用程序,用户在其中拍摄了代码'然后我尝试识别哪些代码'他们使用OpenCV中的特征检测进行拍摄。

用户可以拍照,然后我遍历所有存储的代码'在设备上,匹配每个并选择具有最高匹配数的代码。

特征检测的代码如下所示:

public Integer detect(Mat img1, Mat img2) {
    Size sz = new Size(200, 200);

    FeatureDetector detector = FeatureDetector.create(FeatureDetector.ORB);
    DescriptorExtractor descriptor = DescriptorExtractor
            .create(DescriptorExtractor.ORB);

    DescriptorMatcher matcher = DescriptorMatcher
            .create(DescriptorMatcher.BRUTEFORCE_HAMMING);

    Mat resizeimage = new Mat();
    Imgproc.resize(img1, resizeimage, sz);
    img1 = resizeimage;

    Mat descriptors1 = new Mat();
    MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
    detector.detect(img1, keypoints1);
    descriptor.compute(img1, keypoints1, descriptors1);

    Mat resizeimage2 = new Mat();
    Imgproc.resize(img2, resizeimage2, sz);
    img2 = resizeimage2;

    Mat descriptors2 = new Mat();
    MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
    detector.detect(img2, keypoints2);
    descriptor.compute(img2, keypoints2, descriptors2);

    MatOfDMatch matches = new MatOfDMatch();
    matcher.match(descriptors1, descriptors2, matches);
    int DIST_LIMIT = 80;
    List<DMatch> matchesList = matches.toList();
    List<DMatch> matches_final = new ArrayList<DMatch>();

    for (int i = 0; i < matchesList.size(); i++)
        if (matchesList.get(i).distance <= DIST_LIMIT) {
            matches_final.add(matches.toList().get(i));
        }// end if

    MatOfDMatch matches_final_mat = new MatOfDMatch();
    matches_final_mat.fromList(matches_final);

    mTotalMatches = matches_final.size();

    Scalar RED = new Scalar(255, 0, 0);
    Scalar GREEN = new Scalar(0, 255, 0);

    Mat outputImg = new Mat();
    MatOfByte drawnMatches = new MatOfByte();

    Features2d.drawMatches(img1, keypoints1, img2, keypoints2,
            matches_final_mat, outputImg, GREEN, RED, drawnMatches,
            Features2d.NOT_DRAW_SINGLE_POINTS);

    Bitmap imageMatched = Bitmap.createBitmap(outputImg.cols(),
            outputImg.rows(), Bitmap.Config.RGB_565);
    Utils.matToBitmap(outputImg, imageMatched);

    return mTotalMatches;
}// end detect

我识别的图像看起来像这样,六边形叠加在不同的位置:

hexagonal coe

我遇到的问题是检测到错误的图像,通常仅通过大约5-10的良好匹配计数。我尝试过很好的比赛距离限制,但我似乎无法让它发挥作用。这可能是由于拍摄的照片的图像质量造成的吗?或者我的代码中有错误吗?

0 个答案:

没有答案