Any ideas on template matching in OpenCV for android?

1,459 views
Skip to first unread message

adiel ashrov

unread,
Feb 20, 2012, 2:27:22 PM2/20/12
to android-opencv
Hey,

Is there a way to translate this code to openCV android code?
I couldn't find template match function in the .jar file.

If you have any better ideas on how to match a picture i've taken of a
card to a saved crad, I'll be more than happy to hear it.

As a matter of fact I need to find a {shape and a number} in a given
picture of a card.

please recommend.

Thanks

Adiel

/*
* TemplateMatching 1.0
*
* @author Nashruddin Amin <m...@nashruddin.com>
* @version 1.0
* @website http://www.nashruddin.com
*/

#include <stdio.h>
#include "cv.h"
#include "highgui.h"

int main( int argc, char** argv )
{
IplImage *img;
IplImage *tpl;
IplImage *res;
CvPoint minloc, maxloc;
double minval, maxval;
int img_width, img_height;
int tpl_width, tpl_height;
int res_width, res_height;

/* check for arguments */
if( argc < 3 ) {
fprintf( stderr, "Usage: template_match <reference> <template>\n" );
return 1;
}

/* load reference image */
img = cvLoadImage( argv[1], CV_LOAD_IMAGE_COLOR );

/* always check */
if( img == 0 ) {
fprintf( stderr, "Cannot load file %s!\n", argv[1] );
return 1;
}

/* load template image */
tpl = cvLoadImage( argv[2], CV_LOAD_IMAGE_COLOR );

/* always check */
if( tpl == 0 ) {
fprintf( stderr, "Cannot load file %s!\n", argv[2] );
return 1;
}

/* get image's properties */
img_width = img->width;
img_height = img->height;
tpl_width = tpl->width;
tpl_height = tpl->height;
res_width = img_width - tpl_width + 1;
res_height = img_height - tpl_height + 1;

/* create new image for template matching computation */
res = cvCreateImage( cvSize( res_width, res_height ), IPL_DEPTH_32F,
1 );

/* choose template matching method to be used */
cvMatchTemplate( img, tpl, res, CV_TM_SQDIFF );
/*cvMatchTemplate( img, tpl, res, CV_TM_SQDIFF_NORMED );
cvMatchTemplate( img, tpl, res, CV_TM_CCORR );
cvMatchTemplate( img, tpl, res, CV_TM_CCORR_NORMED );
cvMatchTemplate( img, tpl, res, CV_TM_CCOEFF );
cvMatchTemplate( img, tpl, res, CV_TM_CCOEFF_NORMED );*/

cvMinMaxLoc( res, &minval, &maxval, &minloc, &maxloc, 0 );

/* draw red rectangle */
cvRectangle( img,
cvPoint( minloc.x, minloc.y ),
cvPoint( minloc.x + tpl_width, minloc.y + tpl_height ),
cvScalar( 0, 0, 255, 0 ), 1, 0, 0 );

/* display images */
cvNamedWindow( "reference", CV_WINDOW_AUTOSIZE );
cvNamedWindow( "template", CV_WINDOW_AUTOSIZE );
cvShowImage( "reference", img );
cvShowImage( "template", tpl );

/* wait until user press a key to exit */
cvWaitKey( 0 );

/* free memory */
cvDestroyWindow( "reference" );
cvDestroyWindow( "template" );
cvReleaseImage( &img );
cvReleaseImage( &tpl );
cvReleaseImage( &res );

return 0;
}

n@wonder

unread,
Feb 22, 2012, 4:35:14 PM2/22/12
to android-opencv
This seems to be a simple code in C..use the following link to convert
it for android.
Don't download the pre-compiled folder..try following the steps.
http://marakana.com/forums/android/examples/49.html

Roman

unread,
Mar 11, 2012, 10:09:32 AM3/11/12
to android-opencv
Im doing template matching in Java side. Look at this code:
Point matchLoc;
mROI = mGray.submat(area);
int result_cols = mROI.cols() - mTemplate.cols() + 1;
int result_rows = mROI.rows() - mTemplate.rows() + 1;
mResult = new Mat(result_cols, result_rows, CvType.CV_8U);
Imgproc.matchTemplate(mROI, mTemplate, mResult, Imgproc.TM_SQDIFF) ;
Core.MinMaxLocResult mmres = Core.minMaxLoc(mResult);

Roman

On 22 ún, 22:35, "n@wonder" <sahildhawanthe...@gmail.com> wrote:
> This seems to be a simple code in C..use the following link to convert
> it for android.
> Don't download the pre-compiled folder..try following the steps.http://marakana.com/forums/android/examples/49.html

He Jibo

unread,
Oct 16, 2012, 1:12:16 PM10/16/12
to android...@googlegroups.com
Thanks. I want to get the cross-correlation of two images in android with opencv.  I used your code snippet, and want to display the mmres . It only shows org.opencv.core.Core$$MinMaxLocResu....
How can I get the cross-correlation of two images in android? Thanks!

    @Override
    protected Bitmap processFrame(byte[] data) {
        mYuv.put(0, 0, data);

//        switch (Sample1Java.viewMode) {
//        case Sample1Java.VIEW_MODE_GRAY:
//            Imgproc.cvtColor(mGraySubmat, mRgba, Imgproc.COLOR_GRAY2RGBA, 4);
//            break;
//        case Sample1Java.VIEW_MODE_RGBA:
//            Imgproc.cvtColor(mYuv, mRgba, Imgproc.COLOR_YUV420sp2RGB, 4);
//            Core.putText(mRgba, "OpenCV + Android", new Point(10, 100), 3/* CV_FONT_HERSHEY_COMPLEX */, 2, new Scalar(255, 0, 0, 255), 3);
//            break;
//        case Sample1Java.VIEW_MODE_CANNY:
//            Imgproc.Canny(mGraySubmat, mIntermediateMat, 80, 100);
//            Imgproc.cvtColor(mIntermediateMat, mRgba, Imgproc.COLOR_GRAY2BGRA, 4);
//            break;
//        }
        Imgproc.cvtColor(mYuv, mRgba, Imgproc.COLOR_YUV420sp2RGB, 4);
        mTemplate=mRgba;
        int result_cols =  mRgba.cols() - mTemplate.cols() + 1; 
        int result_rows = mRgba.rows() - mTemplate.rows() + 1; 
        mResult = new Mat(result_cols, result_rows, CvType.CV_8U); 
        Imgproc.matchTemplate(mRgba, mTemplate, mResult, Imgproc.TM_SQDIFF) ; 
        Core.MinMaxLocResult mmres =  Core.minMaxLoc(mResult); 
      Core.putText(mRgba, String.valueOf(mmres), new Point(10, 100), 3/* CV_FONT_HERSHEY_COMPLEX */, 2, new Scalar(255, 0, 0, 255), 3);

        
        Bitmap bmp = Bitmap.createBitmap(getFrameWidth(), getFrameHeight(), Bitmap.Config.ARGB_8888);

        if (Utils.matToBitmap(mRgba, bmp))
            return bmp;

        bmp.recycle();
        return null;
Reply all
Reply to author
Forward
0 new messages