简体   繁体   English

使用相机进行opencv4android模板匹配

[英]opencv4android template matching using camera

I have downloaded and successfully run the example provided in opencv4android sdk . 我已经下载并成功运行了opencv4android sdk中提供的示例。

I am able to simply display the camera frames without any processing, 可以简单地显示相机框架而无需进行任何处理,

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

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

Note: 注意:

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: 这就是我要从实时摄像机识别多张卡的方式:

Result should be: ♠A ♠K ♠Q ♠J ♠10 when camera preview seems like below 结果应为:当相机预览如下所示时,♠A♠K♠Q♠J♠10

在此处输入图片说明

Template matching is unlikely to be the best approach here. 模板匹配不太可能是此处的最佳方法。

  1. Try aSIFT to do an affine invariant SIFT matching or a normal SIFT (OpenCV implementation exists). 尝试使用aSIFT进行仿射不变的SIFT匹配或正常的SIFT(存在OpenCV实现)。 However, since these are in C++, you may want to use JNI to make calls to it from Java on an Android device. 但是,由于这些都是C ++,因此您可能要使用JNI在Android设备上从Java对其进行调用。 This is probably the best way to detect the suit of the card from the 4 symbols. 这可能是从4个符号中检测卡的花色的最佳方法。
  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. 检测和识别卡上数字/字母的另一种方法是使用文本检测器(例如MSER) ,然后使用MSER过滤器指示的感兴趣区域上的文本识别器。

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. 使用第一种方法,您可能可以获得完整的正面,直立图像的可接受的性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM