OpenCV Template Matching example in Android
I was facing the same problem you did. No source in Java available. Some search in the JavaDoc and some hints for const values later, I wrote this, which is almost the sample code above written in Java:
package opencv;
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;
class MatchingDemo {
public void run(String inFile, String templateFile, String outFile, int match_method) {
System.out.println("\nRunning Template Matching");
Mat img = Highgui.imread(inFile);
Mat templ = Highgui.imread(templateFile);
// / 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 and Normalize
Imgproc.matchTemplate(img, templ, result, match_method);
Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
// / Localizing the best match with minMaxLoc
MinMaxLocResult mmr = Core.minMaxLoc(result);
Point matchLoc;
if (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) {
matchLoc = mmr.minLoc;
} else {
matchLoc = mmr.maxLoc;
}
// / Show me what you got
Core.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(),
matchLoc.y + templ.rows()), new Scalar(0, 255, 0));
// Save the visualized detection.
System.out.println("Writing "+ outFile);
Highgui.imwrite(outFile, img);
}
}
public class TemplateMatching {
public static void main(String[] args) {
System.loadLibrary("opencv_java246");
new MatchingDemo().run(args[0], args[1], args[2], Imgproc.TM_CCOEFF);
}
}
Now, run the program with the following options: lena.png template.png templatematch.png
and you should receive the same result I did. Make sure the files are accessible by your runtime and, of course, opencv 2.4.6 library is registered to your classpath.
Android - OpenCV Template Matching with threshold
Core.MinMaxLocResult
contains maxLoc
, maxVal
, minLoc
, and minVal
values. Just assign match=mmr.maxVal
or match=mmr.minVal
depending on context.
Alternatively, you could index in the result
Mat to get your value and then test if it's over 0.8 as so:
double[] resultVal = result.get(matchLoc.y, matchLoc.x);
if (resultVal[0] >= 0.8) ...
Android - OpenCV Template Matching
googling a little bit , you will found that you've called the template matching before loading the OpenCV library see the same error in openCV forum :
you can only call opencv functions after the BaseLoaderCallback
finished(the opencv dlls were loaded). so, if you put that code into
your onCreate function, you'll get that error.
you could find a complete BaseLoaderCallback documentation in openCV doc
Hope this could help you ^^
Template matching using OpenCV
I might be far off here, but I believe the submat method returns a pointer to the submatrix of mGray. So your template changes from frame to frame since mRgba is always copied to mGray, but mGray is never reallocated. In this case, the solution would be to make a copy of the mGray submatrix. In C++, that would be something like:
mGray.submat(Y-H/2, Y+H/2, X-W/2, X+W/2).copyTo(template);
Related Topics
Passing a JavaScript Object Using Addjavascriptinterface() on Android
Android: How to Disable List Items on List Creation
Issue When Using a Custom Font - "Native Typeface Cannot Be Made"
Can't Get Download Url from Firebase Storge in Android
Why Is Accessing Textview of a Fragment Inside Activity Throwing an Error
Android Recyclerview Scrolling Performance
Android Layout with Listview and Buttons
Which Programming Languages How to Use on Android Dalvik
Deprecated Managedquery() Issue
Creating Hashmap from a JSON String
What Is the Easiest Way to Get the Current Day of the Week in Android
Android HTML.Fromhtml() with Images
How to Start Activity in Adapter
How to Draw a Filled Triangle in Android Canvas
How to Check Wifi or 3G Network Is Available on Android Device