使用相机opencv4android模板匹配 [英] opencv4android template matching using camera

查看:899
本文介绍了使用相机opencv4android模板匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经下载并成功运行的<一个提供的示例href="http://docs.opencv.org/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.html#dev-with-ocv-on-android"相对=nofollow> opencv4android人员。

I have downloaded and successfully run the example provided in opencv4android sdk.

我的可以简单地显示相机帧未经过任何处理,

 public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
     return inputFrame.rgba();
 }

我要处理现场框与一些predefined图像模板以识别模板。我已经从参考这个帖子和相应的执行。但我只得到黑色的屏幕。

I want to process live frame with some predefined image template to recognize that template. I have taken reference from this post and implemented accordingly. But I get black screen only.

private Mat mCameraMat = new Mat();
private Mat mTemplateMat;

 public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
    mCameraMat = inputFrame.rgba();
    initialize();

     int match_method = Imgproc.TM_SQDIFF;

        // Create the result matrix
        int result_cols = mCameraMat.cols() - mTemplateMat.cols() + 1;
        int result_rows = mCameraMat.rows() - mTemplateMat.rows() + 1;
        Log.d(TAG, " mCameraMat cols "+mCameraMat.cols());
        Log.d(TAG, " mCameraMat rows "+mCameraMat.rows());
        Log.d(TAG, " mTemplateMat cols "+mTemplateMat.cols());
        Log.d(TAG, " mTemplateMat rows "+mTemplateMat.rows());

       Mat result = new Mat(result_rows, result_cols, CvType.CV_32F);

        // Do the Matching and Normalize
        Imgproc.matchTemplate(mCameraMat, mTemplateMat, result, match_method);



        Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());

        // Localizing the best match with minMaxLoc
        MinMaxLocResult mmr = Core.minMaxLoc(result);

        Point matchLoc;
        if (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) {
            matchLoc = mmr.minLoc;
        } else {
            matchLoc = mmr.maxLoc;
        }

        Rect roi = new Rect((int) matchLoc.x, (int) matchLoc.y, mTemplateMat.cols(), mTemplateMat.rows());
        Core.rectangle(mCameraMat, new Point(roi.x, roi.y), new Point(roi.width - 2, roi.height - 2), new Scalar(255, 0, 0, 255), 2);           
 return result;
}

public void initialize(){

    try {
        if (mCameraMat.empty())
            return;
        if(mTemplateMat == null){
            Mat temp = Utils.loadResource(Tutorial1Activity.this, R.drawable.icon);
            mTemplateMat = new Mat(temp.size(), CvType.CV_32F);
            Imgproc.cvtColor(temp, mTemplateMat, Imgproc.COLOR_BGR2RGBA);
            Log.d(TAG, "initialize mTemplateMat cols "+mTemplateMat.cols());
            Log.d(TAG, "initialize mTemplateMat rows "+mTemplateMat.rows());
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

注意:

我的最终目标是为识别的扑克牌从现场摄像机。请建议最好的办法。我应该使用的图像模板或任何其他东西,使事情更快?

My ultimate goal is to recognize the playing cards from live camera. Kindly suggest best approach. Should I use image templates or any other thing to make things faster?

这是我要如何识别多张卡从现场摄像机:

This is how I want to recognize multiple cards from live camera:

结果应该是:♠一个♠ķ♠Q♠Ĵ♠10时相机preVIEW好像下面

Result should be: ♠A ♠K ♠Q ♠J ♠10 when camera preview seems like below

推荐答案

模板匹配不大可能是这里最好的办法。

Template matching is unlikely to be the best approach here.

  1. 尝试 ASIFT 做仿射不变SIFT匹配或正常SIFT(OpenCV的执行情况存在)。然而, 因为这些都是在C ++中,您可能需要使用JNI来在Android设备上调用它,从Java。这可能是检测从4个符号的牌的花色的最佳方式。
  2. 另一种选择,以检测和识别号码/卡上的字母是使用文本检测器一样的 MSER ,然后感兴趣的MSER过滤器指示的区域的文本识别。
  1. Try aSIFT to do an affine invariant SIFT matching or a normal SIFT (OpenCV implementation exists). However, since these are in C++, you may want to use JNI to make calls to it from Java on an Android device. This is probably the best way to detect the suit of the card from the 4 symbols.
  2. Another option to detect and recognize the numbers/alphabets on the cards is to use a text detector like MSER and then a text recognizer on the regions of interest indicated by the MSER filter.

在任何情况下,你是不太可能能够产生最好的结果,从你的图像中所表现出来的那种形象。您可能能够获得完全的正面,直立的图像与第一种方法可接受的性能。

In any case, you are unlikely to be able produce the best of results from the kind of image you've shown in the image. You may be able to get acceptable performance for full frontal, upright images with the first method.

这篇关于使用相机opencv4android模板匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆