[英]Assertion failed Error during template matching with OpenCV in Android
我在Netbeans
編寫了一個OpenCV
模板匹配類,並且在JVM
運行良好。
我只想把它轉到android應用程序。 但是我之前對Android編程不感興趣。 因此,我閱讀了教程,並確定IntentService
對我的目標很IntentService
。 因為我不希望任何UI僅處理圖像並采用結果圖像。
最后,我將OpenCV
導入到我的簡單Android項目中。 Template Matching
在JVM
運行良好,但在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)
確保matchTemplate()的所有參數的大小和類型都正確。 從OpenCV文檔中:
圖像–運行搜索的圖像。 它必須是8位或32位浮點。
templ –搜索的模板。 它必須不大於源圖像並且具有相同的數據類型。
因此,請確保您的投資回報率為類型(8位或32位浮點數)。 還要檢查圖像是否正確打開,因為如果模板太小會出現該錯誤。
- 解決了 -
第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";
感謝您的關注和努力。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.