断言失败与Android中的OpenCV进行模板匹配时出错

时间:2019-01-10 19:13:52

标签: java android opencv template-matching

我在OpenCV中编写了一个Netbeans模板匹配类,并且在JVM中运行良好。

我只想将其转到android应用程序。但是我之前对Android编程不感兴趣。因此,我阅读了教程,并确定IntentService对于我的目标而言是不错的选择。因为我不希望任何UI只处理图像并获取结果图像。

我终于将OpenCV导入我的简单Android项目。 Template MatchingJVM中运作良好,但在Android中出现错误。只是我更改了Android的图像文件路径形式。并在JVM中使用相同的图像文件。

-编辑-

我将图像文件复制到Android虚拟设备下载文件夹。我用虚拟设备对其进行了测试。

让我分享我的代码和结果;

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


}

结果

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)

2 个答案:

答案 0 :(得分:0)

确保matchTemplate()的所有参数的大小和类型都正确。 从OpenCV文档中:

  

image –运行搜索的图像。它必须是8位或32位浮点。

     

templ –搜索的模板。它必须不大于源图像并且具有相同的数据类型。

因此,请确保您的投资回报率是类型(8位或32位浮点数)。 还要检查图像是否正确打开,因为如果模板太小会出现该错误。

答案 1 :(得分:0)

-已解决-

问题1

AndroidManifest.xml需要这样的权限;

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

问题2

文件路径错误并已更改;

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

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";

感谢您的关注和努力。