Android camera streaming and matchTemplates, suggestion?

asked 2013-09-19 03:59:09 -0500

Pacobo gravatar image

updated 2013-09-19 08:25:20 -0500


I want to recognize a specific pattern ( This is the pattern ) with Android camera. I took the OpenCV Tutorial1 and developed this:

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

    AssetManager a = getAssets();
    InputStream i = null;
    try {
        i ="pattern.png");
    } catch (IOException e) {
    Drawable d = Drawable.createFromStream(i, "pattern");
    Bitmap bb = ((BitmapDrawable) d).getBitmap();
    Mat templ = new Mat();
    Utils.bitmapToMat(bb, templ);

    int match_method = Imgproc.TM_CCOEFF;

    Mat img = inputFrame.rgba();
    int result_cols = img.cols() - templ.cols() + 1;
    int result_rows = img.rows() - templ.rows() + 1;
    Mat result = new Mat(result_rows, result_cols, CvType.CV_8U);
    Log.i("WWW", "mat: " + img.channels() + " " + templ.channels() + " "
            + result.channels());
    Imgproc.matchTemplate(img, templ, result, match_method);
    Log.i("WWW", "matchTemplate");
    Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
    Log.i("WWW", "normalize");
    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;
    Core.rectangle(mRgba, matchLoc, new Point(matchLoc.x + templ.cols(),
            matchLoc.y + templ.rows()), new Scalar(0, 255, 0));
    Log.i("WWW", "square");

    return mRgba;

As you could see, the image is loaded in each frame, that's because I'm so idiot and cann't load it anywhere... </edit!!>

Thank you!

edit retag flag offensive close merge delete


think again. just knowing that it's a qr code, won't help you much.

you'll probably need a decoder library for this, too.

berak gravatar imageberak ( 2013-09-19 04:02:55 -0500 )edit

Actually your question exists of 3 parts. First the QR code part, then the part where you don't succeed in using openCV and matchTemplate, then the fact that you need help on camera streaming. I suggest you update your question, make it clear, add some example code you tried which failed, supply your system configuration, ... only then people will feel the urge to actually help you out.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-09-19 04:41:44 -0500 )edit

I don't want a QR-decode/encoder, I want to recognize a simple pattern really similar to the QR-code. Sorry if I hadn't explained it correctly.

Also, sorry for not write all the things you said StevenPuttermans, I tried to keep it simple, an example of camera streaming and matchTemplate. I'm trying to organize everything and upload it.

Pacobo gravatar imagePacobo ( 2013-09-19 04:42:37 -0500 )edit

Then again, place your pattern here, add your code and tell us where it goes wrong. Then we will help you out. This is not a ask and get ready made code forum.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-09-19 05:11:53 -0500 )edit

Why dont you create a global variable for the input image and load it for example in your OnCreate() function?

Moster gravatar imageMoster ( 2013-09-19 08:51:00 -0500 )edit

Done, but the problem is the size of the picture, now I take a submatrix and it's better :D

Pacobo gravatar imagePacobo ( 2013-09-23 02:20:46 -0500 )edit

If your question was solved, then feel free to post your solution and accept it as an answer so that it shows solved.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-09-23 03:08:24 -0500 )edit