简体   繁体   中英

Assertion failed Error during template matching with OpenCV in Android

I write a OpenCV template matching class in Netbeans and worked well in JVM .

Just i want turn it to android application. But i am not interested Android Programming before. So i read tutorials and decide IntentService is looking good for my goal. Because i dont want any UI just process image and taking result image.

I finally import OpenCV to my simple Android project. Template Matching working well in JVM but turns error in Android. Just i changed image filepath form for Android. And using same image files in JVM .

--EDIT--

I copy image files to Android Virtual Device Download folder. And i test it with Virtual Device.

Let me share my code and result;

MyService.java (Android Studio)

import android.content.Intent;
import android.app.IntentService;
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.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;


public class MyService extends IntentService {

    public MyService() {
        super("MyService");
    }

    @Override
    protected void onHandleIntent(Intent workIntent) {

        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        Mat img = Imgcodecs.imread("/sdcard/Download/bigpicture.png");
        Mat templ = Imgcodecs.imread("/sdcard/Download/template.png");
        String outFile = "/sdcard/Download/result.png";

        // Create the result matrix
        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_32FC1);

        // Do the Matching Normalize and Perform the template matching operation
        Imgproc.matchTemplate(img, templ, result, 3);
        // Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
        Imgproc.threshold(result, result,0.98,1,Imgproc.THRESH_TOZERO);

        // Localizing the best match with minMaxLoc. We localize the minimum and maximum values in the result matrix R by using minMaxLoc.
        Point matchLoc;
        Point maxLoc;
        Point minLoc;

        MinMaxLocResult mmr;


        while(true)
        {

            mmr = Core.minMaxLoc(result);
            matchLoc = mmr.maxLoc;

            if(mmr.maxVal >= 0.997)
            {
                Imgproc.rectangle(img, matchLoc,
                        new Point(matchLoc.x + templ.cols(),matchLoc.y + templ.rows()),
                        new    Scalar(0,255,0));

                Imgproc.rectangle(result, matchLoc,
                        new Point(matchLoc.x + templ.cols(),matchLoc.y + templ.rows()),
                        new    Scalar(0,255,0),-1);

                System.out.println(matchLoc.x + "---" + matchLoc.y);
                //break;
            }
            else
            {
                break; //No more results within tolerance, break search

            }

        }

        Imgcodecs.imwrite(outFile, img);

    }

}

MainActivity.java (Android Studio)

import android.content.Intent;
import android.app.Activity;
import android.os.Bundle;

public class MainActivity extends Activity {


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = new Intent(MainActivity.this, MyService.class);
        startService(intent);
    }


}

Result

E/cv::error(): 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 /builds/master_pack-android/opencv/modules/imgproc/src/templmatch.cpp, line 658
E/org.opencv.imgproc: imgproc::matchTemplate_11() caught cv::Exception: /builds/master_pack-android/opencv/modules/imgproc/src/templmatch.cpp:658: 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)
E/AndroidRuntime: FATAL EXCEPTION: IntentService[MyService]
    Process: com.lacrymae.bapplication, PID: 6565
    CvException [org.opencv.core.CvException: cv::Exception: /builds/master_pack-android/opencv/modules/imgproc/src/templmatch.cpp:658: 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)
    ]
        at org.opencv.imgproc.Imgproc.matchTemplate_1(Native Method)
        at org.opencv.imgproc.Imgproc.matchTemplate(Imgproc.java:2105)
        at com.lacrymae.bapplication.MyService.onHandleIntent(MyService.java:36)
        at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.os.HandlerThread.run(HandlerThread.java:61)

Make sure that all arguments to matchTemplate() are of correct size and type. From OpenCV documentation:

image – Image where the search is running. It must be 8-bit or 32-bit floating-point.

templ – Searched template. It must be not greater than the source image and have the same data type.

So make sur that your roi is of type (8-bit or 32-bit floating-point). Also check that your images are correctly opened because it would give that error if the template was too small.

--SOLVED--

Issue 1

AndroidManifest.xml need a permission like this;

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Issue 2

Filepath was wrong and changed;

Mat img = Imgcodecs.imread("/sdcard/Download/bigpicture.png");
Mat templ = Imgcodecs.imread("/sdcard/Download/template.png");
String outFile = "/sdcard/Download/result.png";

to

String path = Environment.getExternalStorageDirectory().getPath();

Mat img = Imgcodecs.imread(path + "/Download/bigpicture.png");
Mat templ = Imgcodecs.imread(path + "/Download/template.png");
String outFile = path + "/Download/result.png";

Thanks for your interests and efforts.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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