templateMatch Android FATAL EXCEPTION

asked 2013-07-11 16:19:48 -0600

jasper gravatar image

updated 2013-07-11 16:46:35 -0600

I'm trying to do template matching using OpenCV libs in Java for Android. I'm trying to utilize a code that I found here to do the job.

When I execute the app I get FATAL EXCEPTION

07-10 20:24:28.456: E/cv::error()(8608): OpenCV Error: Assertion failed (corrsize.height <= img.rows + templ.rows - 1 && corrsize.width <= img.cols + templ.cols - 1) in void cv::crossCorr(const cv::Mat&, const cv::Mat&, cv::Mat&, cv::Size, int, cv::Point, double, int), file /home/reports/ci/slave_desktop/50-SDK/opencv/modules/imgproc/src/templmatch.cpp, line 70


07-10 20:55:15.706: E/AndroidRuntime(9622): FATAL EXCEPTION: main 07-10 20:55:15.706: E/AndroidRuntime(9622): CvException [org.opencv.core.CvException: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/imgproc/src/templmatch.cpp:70: error: (-215) corrsize.height <= img.rows + templ.rows - 1 && corrsize.width <= img.cols + templ.cols - 1 in function void cv::crossCorr(const cv::Mat&, const cv::Mat&, cv::Mat&, cv::Size, int, cv::Point, double, int)

I presume that it must be something to do with the size of the files, but there are executed as should be (main image, small image, image to write to).

I use .bmp files as input:

bmp1.bmp - size 1280x960 - main image
bmp2.bmp - size 168x63   - template image
bmp3.bmp - size 1280x960 - (blank .bmp file to write the result) size 1280x960

My images are 8 bit as instructed in OpenCV docs. I have also tried converting my images to .png, but still no joy.

My Start.java code is:

package com.example.matchtemplate;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Core;
import org.opencv.core.Core.MinMaxLocResult;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class Start extends Activity {

    Button button;
    ImageView imageview;

    protected static final String TAG = null;
    private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
        public void onManagerConnected(int status) {
            switch (status) {
                case LoaderCallbackInterface.SUCCESS:
                    Log.i(TAG, "OpenCV loaded successfully");

                } break;
                } break;

    public void onResume()
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_6, this, mLoaderCallback);


    protected void onCreate(Bundle savedInstanceState) {

    public void addListenerOnButton() {

        imageview = (ImageView) findViewById(R.id.imageView1);

        button = (Button) findViewById(R.id.button1);
        button.setOnClickListener(new OnClickListener() {

            public void onClick(View arg0) {

                matchTemplate("wall.png", "template.png", "result.png", Imgproc.TM_CCOEFF);



    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.start, menu);
        return true;

    public void matchTemplate(String inFile, String templateFile, String outFile, int ...
edit retag flag offensive close merge delete