How to match multiple objects - java

How do i match multiple objects using a single template?
i want to match multiple objects by threshold value.
When i matched a single object, i used this code.
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
Mat img = Highgui.imread("/test/test_img.jpg");//input image
if(img.empty())
throw new Exception("no image");
Mat tpl = Highgui.imread("/test/test_tpl.jpg");//template image
if(tpl.empty())
throw new Exception("no template");
Mat result = new Mat();
Imgproc.matchTemplate(img, tpl,result,Imgproc.TM_CCOEFF_NORMED);//Template Matching
Core.MinMaxLocResult maxr = Core.minMaxLoc(result);
Point maxp = maxr.maxLoc;
Point maxop = new Point(maxp.x + tpl.width(), maxp.y + tpl.height());
Mat dst = img.clone();
Core.rectangle(dst, maxp, maxop, new Scalar(255,0,0), 2);//draw a rectangle
Highgui.imwrite("/test/test.jpg", dst);//save image

This one is working for me:
Mat img = Highgui.imread("/test/test_img.jpg");//input image
if(img.empty())
throw new Exception("no image");
Mat tpl = Highgui.imread("/test/test_tpl.jpg");//template image
if(tpl.empty())
throw new Exception("no template");
Mat result = new Mat();
Imgproc.matchTemplate(img, tpl,result,Imgproc.TM_CCOEFF_NORMED);//Template Matching
Imgproc.threshold(result, result, 0.1, 1, Imgproc.THRESH_TOZERO);
double threshold = 0.95;
double maxval;
Mat dst;
while(true)
{
Core.MinMaxLocResult maxr = Core.minMaxLoc(result);
Point maxp = maxr.maxLoc;
maxval = maxr.maxVal;
Point maxop = new Point(maxp.x + tpl.width(), maxp.y + tpl.height());
dst = img.clone();
if(maxval >= threshold)
{
System.out.println("Template Matches with input image");
Core.rectangle(img, maxp, new Point(maxp.x + tpl.cols(),
maxp.y + tpl.rows()), new Scalar(0, 255, 0),5);
Core.rectangle(result, maxp, new Point(maxp.x + tpl.cols(),
maxp.y + tpl.rows()), new Scalar(0, 255, 0),-1);
}else{
break;
}
}
Highgui.imwrite("test.jpg", dst);//save image
for example
the template:
coin
and the result:
marioWorld

Related

How to adjust the threshold for template matching in openCV (java)?

I am running template matching using openCV 3.4.7 Android SDK (java).
The code work almost perfectly; when the template is match, it draws a rectangle on the matching area. The problem is that even when there is no match, it draws a random rectangle. I think that happens because the threshold is not set correctly. If so, can someone please help me out?
Here's the code:
public static void run(String inFile, String templateFile, String outFile,
int match_method) {
Mat img = Imgcodecs.imread(inFile);
Mat templ = Imgcodecs.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
Core.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
Imgproc.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(),
matchLoc.y + templ.rows()), new Scalar(0, 0, 128));
// Save the visualized detection.
System.out.println("Writing " + outFile);
Imgcodecs.imwrite(outFile, img);
}
You can use Imgproc.TM_CCOEFF_NORMED or Imgproc.TM_CCORR_NORMED and mmr.maxVal >= 0.8. It should take care of most of your false positives.
Sample Code:
import org.opencv.core.*;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;
import java.io.File;
import java.nio.file.Files;
public class templateMatchingTester {
private static String str = null;
static {
if (str == null) {
str = "initialised";
nu.pattern.OpenCV.loadShared();
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
}
private static Mat createMatrixFromImage(String imagePath) {
Mat imageMatrix = Imgcodecs.imread(imagePath);
Mat greyImage = new Mat();
Imgproc.cvtColor(imageMatrix, greyImage, Imgproc.COLOR_BGR2GRAY);
return greyImage;
}
private static boolean matchTemplate(String pathToInputImage,String pathToTemplate){
Mat inputImage = createMatrixFromImage(pathToInputImage);
Mat templateImage = createMatrixFromImage(pathToTemplate);
// Create the result matrix
int result_cols = inputImage.cols() - templateImage.cols() + 1;
int result_rows = inputImage.rows() - templateImage.rows() + 1;
Mat result = new Mat(result_rows, result_cols, CvType.CV_8UC1);
int match_method;
match_method = Imgproc.TM_CCOEFF_NORMED;//Imgproc.TM_CCORR_NORMED;
Imgproc.matchTemplate(inputImage, templateImage, result, match_method);
Core.MinMaxLocResult mmr = Core.minMaxLoc(result);
double minMatchQuality = 0.85;
System.out.println(mmr.maxVal);
if (mmr.maxVal >= minMatchQuality){
return true;
} else
return false;
}
public static void main(String args[]) {
String template = "path/to/your/templateImage";
final File folder = new File("path/to/your/testImagesFolder/");
int matchCount = 0;
for (final File fileEntry : folder.listFiles()){
if (matchTemplate(fileEntry.getPath(),template)){
matchCount+=1;
}else
System.out.println(fileEntry.getPath());
}
System.out.println(matchCount);
}
}
Use a normed match method to ensure your match value is [0..1].
Replace this line
Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
with a thresholding operation. Otherwise a best match of 0.9 would become 1 by the second normalization and you would lose the actual match "quality" information.
Normalizing the result of the template matching will always result in your best match being 1 making it impossible to discard a bad match.
i wrote an app that would take a screenshot of the game overwatch and attempt to tell who is on each team. using template matching and open cv. project need to iterate over the result image and check values.
OpenCVUtils.getPointsFromMatAboveThreshold(result,
0.90f)
public static void scaleAndCheckAll(String guid){
Mat source = imread(IMG_PROC_PATH + guid); //load the source image
Mat scaledSrc = new Mat(defaultScreenshotSize, source.type());
resize(source, scaledSrc, defaultScreenshotSize);
Mat sourceGrey = new Mat(scaledSrc.size(), CV_8UC1);
cvtColor(scaledSrc, sourceGrey, COLOR_BGR2GRAY);
for (String hero : getCharacters()) {
Mat template = OpenCVUtils.matFromJar(TEMPLATES_FOLDER + hero + ".png", 0); //load a template
Size size = new Size(sourceGrey.cols()-template.cols()+1, sourceGrey.rows()-template.rows()+1);
Mat result = new Mat(size, CV_32FC1);
matchTemplate(sourceGrey, template, result, TM_CCORR_NORMED);// get results
Scalar color = OpenCVUtils.randColor();
List<Point> points = OpenCVUtils.getPointsFromMatAboveThreshold(result,
0.90f);
for (Point point : points) {
//rectangle(scaledSrc, new Rect(point.x(),point.y(),template.cols(),template.rows()), color, -2, 0, 0);
putText(scaledSrc, hero, point, FONT_HERSHEY_PLAIN, 2, color);
}
}
String withExt = IMG_PROC_PATH + guid +".png";
imwrite(withExt, scaledSrc);
File noExt = new File(IMG_PROC_PATH + guid);
File ext = new File(withExt);
noExt.delete();
ext.renameTo(noExt);
}
the other method.
public static List<Point> getPointsFromMatAboveThreshold(Mat m, float t){
List<Point> matches = new ArrayList<Point>();
FloatIndexer indexer = m.createIndexer();
for (int y = 0; y < m.rows(); y++) {
for (int x = 0; x < m.cols(); x++) {
if (indexer.get(y,x)>t) {
System.out.println("(" + x + "," + y +") = "+ indexer.get(y,x));
matches.add(new Point(x, y));
}
}
}
return matches;
}
you can just get the first from the list or see how close they are if you expect multiple matches.

OpenCV - Android : java.lang.IllegalArgumentException: bmp == null

I'm trying to capture an image from the JavaCameraView and load the captured image into another activity and is supposed to be processed (Hough Circles).
private void takePhoto(final Mat rgba) {
// Determine the path and metadata for the photo.
final long currentTimeMillis = System.currentTimeMillis();
final String appName = getString(R.string.app_name);
final String galleryPath =
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).toString();
final String albumPath = galleryPath + File.separator +
appName;
final String photoPath = albumPath + File.separator +
currentTimeMillis + LabActivity.PHOTO_FILE_EXTENSION;
final ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, photoPath);
values.put(Images.Media.MIME_TYPE,
LabActivity.PHOTO_MIME_TYPE);
values.put(Images.Media.TITLE, appName);
values.put(Images.Media.DESCRIPTION, appName);
values.put(Images.Media.DATE_TAKEN, currentTimeMillis);
// Ensure that the album directory exists.
File album = new File(albumPath);
if (!album.isDirectory() && !album.mkdirs()) {
Log.e(TAG, "Failed to create album directory at " +
albumPath);
onTakePhotoFailed();
return;
}
/*
// Try to create the photo.
Imgproc.cvtColor(rgba, mBgr, Imgproc.COLOR_RGBA2BGR, 3);
if (!Imgcodecs.imwrite(photoPath, mBgr)) {
Log.e(TAG, "Failed to save photo to " + photoPath);
onTakePhotoFailed();
}
Log.d(TAG, "Photo saved successfully to " + photoPath);
*/
Mat grayMat = new Mat();
Mat cannyEdges = new Mat();
Mat lines = new Mat();
Imgproc.cvtColor(rgba, mBgr, Imgproc.COLOR_RGBA2BGR, 3);
//Converting the image to grayscale
Imgproc.cvtColor(mBgr, grayMat, Imgproc.COLOR_BGR2GRAY);
Imgproc.Canny(grayMat, cannyEdges, 10, 100);
Imgproc.HoughLinesP(cannyEdges, lines, 1, Math.PI / 180, 50, 20, 20);
Mat houghLines = new Mat();
houghLines.create(cannyEdges.rows(), cannyEdges.cols(), CvType.CV_8UC1);
//Drawing lines on the image
for (int i = 0; i < lines.cols(); i++) {
double[] points = lines.get(0, i);
double x1, y1, x2, y2;
x1 = points[0];
y1 = points[1];
x2 = points[2];
y2 = points[3];
Point pt1 = new Point(x1, y1);
Point pt2 = new Point(x2, y2);
//Drawing lines on an image
Imgproc.line(houghLines, pt1, pt2, new Scalar(255, 0, 0), 1);
}
//Converting Mat back to Bitmap
Utils.matToBitmap(houghLines, currentBitmap);
Log.d(TAG, "Photo saved successfully to " + photoPath);
// Try to insert the photo into the MediaStore.
Uri uri;
try {
uri = getContentResolver().insert(
Images.Media.EXTERNAL_CONTENT_URI, values);
} catch (final Exception e) {
Log.e(TAG, "Failed to insert photo into MediaStore");
e.printStackTrace();
// Since the insertion failed, delete the photo.
File photo = new File(photoPath);
if (!photo.delete()) {
Log.e(TAG, "Failed to delete non-inserted photo");
}
onTakePhotoFailed();
return;
}
// Open the photo in LabActivity.
final Intent intent = new Intent(this, LabActivity.class);
intent.putExtra(LabActivity.EXTRA_PHOTO_URI, uri);
intent.putExtra(LabActivity.EXTRA_PHOTO_DATA_PATH,
photoPath);
runOnUiThread(new Runnable() {
#Override
public void run() {
startActivity(intent);
}
});
}
The error occurs after i click the capture option.
12-07 00:15:45.420 9205-9933/? E/AndroidRuntime﹕ FATAL EXCEPTION: Thread-8672
Process: com.example.alexies.cameratesting, PID: 9205
java.lang.IllegalArgumentException: bmp == null
at org.opencv.android.Utils.matToBitmap(Utils.java:122)
at org.opencv.android.Utils.matToBitmap(Utils.java:132)
at com.example.alexies.cameratesting.MainActivity.takePhoto(MainActivity.java:380)
currentBitmap is null in your code.
Either you didn't copy the part where it's assigned a bitmap value or it's never assigned. If there's some part of your code missing please add it in your question, if not, your problem is that you never get the bitmap.
EDIT
You never initiate currentBitmap. The docs state that the provided bitmap must be the same size as the Mat object (your houghLines) and the type of your bitmap should be ARGB_8888 or RGB_565.

Mouth detection using openCv returns a lot of circles in only one face

I'm using OpenCv with android studio to detect faces in an image along with the eyes and the mouth in each face. But, the problem is whenever I try to detect the mouth it returns multiple circles in a face which is wrong.
Here is the code I added for mouth detection:
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
#Override
public void onManagerConnected(int status) {
switch (status) {
...
InputStream iserM = getResources().openRawResource(
R.raw.haarcascade_mcs_mouth);
File cascadeDirERM = getDir("cascadeERM",
Context.MODE_PRIVATE);
File cascadeFileERM = new File(cascadeDirERM,
"haarcascade_mcs_mouth.xml");
FileOutputStream oserM = new FileOutputStream(cascadeFileERM);
byte[] bufferERM = new byte[4096];
int bytesReadERM;
while ((bytesReadERM = iserM.read(bufferERM)) != -1) {
oserM.write(bufferERM, 0, bytesReadERM);
}
iserM.close();
oserM.close();
...
//here begins
mJavaDetectorMouth = new CascadeClassifier(
cascadeFileERM.getAbsolutePath());
if (mJavaDetectorMouth.empty()) {
Log.e(TAG, "Failed to load cascade classifier");
mJavaDetectorMouth = null;
} else
Log.i(TAG, "Loaded cascade classifier from "
+ mCascadeFile.getAbsolutePath());
//here ends
...
}
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
...
Rect r = facesArray[i];
MatOfRect mouths = new MatOfRect();
Mat faceROI = mRgba.submat(facesArray[i]);
mJavaDetectorMouth.detectMultiScale(faceROI, mouths,1.1,1,1, new org.opencv.core.Size(30, 30), new org.opencv.core.Size());
Rect[] mouthArray = mouths.toArray();
for (int j = 0; j < mouthArray.length; j++){
Point center1 = new Point(facesArray[i].x + mouthArray[j].x + mouthArray[j].width * 0.5,
facesArray[i].y + mouthArray[j].y + mouthArray[j].height * 0.5);
int radius = (int) Math.round(mouthArray[j].width / 2);
Imgproc.circle(mRgba, center1, radius, new Scalar(255, 0, 0), 4, 8, 0);
}
...
}
I've been looking around since I posted the question and tried a lot of things. But finally I found the solution:
I changed:
mJavaDetectorMouth.detectMultiScale(faceROI, mouths,1.1,1,1, new org.opencv.core.Size(30, 30), new org.opencv.core.Size());
To:
mJavaDetectorMouth.detectMultiScale(faceROI, mouths,1.1, 2,
Objdetect.CASCADE_FIND_BIGGEST_OBJECT
| Objdetect.CASCADE_SCALE_IMAGE, new org.opencv.core.Size(30, 30), new org.opencv.core.Size());
and I solved the issue.

template matching with multiple templates in android

I'm a beginner to OpenCV.
I'm trying to do template matching with multiple tempates.
for single template i get the source code from here
OpenCV Template Matching example in Android
I searched in the internet and I couldn't find a proper android or java code which satisfy my requirement.
I have C++ code but I dont know how to translate it.
Can you please help me to find a proper java or android code. Or else please help me with translate this C++ code into java, which I can use inside android application.
Thank you in advance.
C++ code
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
bool FindTemplate(Mat Img_Scene_Bgr,Mat Img_Template_Bgr,Point &Point_TemplateLocation)
{
// `Img_Scene_Bgr` and `Img_Template_Bgr` are the reference and template image
cv::Mat Img_Result_Float(Img_Scene_Bgr.rows-Img_Template_Bgr.rows+1, Img_Scene_Bgr.cols-Img_Template_Bgr.cols+1, CV_32FC1);
cv::matchTemplate(Img_Template_Bgr, Img_Scene_Bgr, Img_Result_Float, CV_TM_CCOEFF_NORMED);
normalisasi normalize( Img_Result_Float, Img_Result_Float, 0, 1, NORM_MINMAX, -1, Mat() );
double minval, maxval, threshold = 0.7;
cv::Point minloc, maxloc;
cv::minMaxLoc(Img_Result_Float, &minval, &maxval, &minloc, &maxloc);
if (maxval >= threshold)
{
Point_TemplateLocation= maxloc;
return true;
}
else
{
return false;
}
}
int main( int argc, char** argv )
{
Mat Img_Scene;
Mat Img_Template_1;
Mat Img_Template_2;
Mat Img_Result;
char* image_window = "Source Image";
char* result_window = "Result window";
/// Load image and template
Img_Scene = imread("SceneImage.png", 1 );
Img_Template_1 = imread( "Templ1.png", 1 );
Img_Template_2 = imread( "Templ2.png", 1 );
if(Img_Scene.data== NULL||Img_Template_1.data==NULL||Img_Template_2.data==NULL)
{
cout<<"Image Not Found";
return 0;
}
Img_Result= Img_Scene.clone();
Vector<Mat> List_Template_Img;
List_Template_Img.push_back(Img_Template_1);//Otherwise Get some folder & add the Files in it
List_Template_Img.push_back(Img_Template_2);
Point Point_TemplateLocation;
for (int i = 0; i < List_Template_Img.size(); i++)
{
if(!FindTemplate(Img_Scene,List_Template_Img[i],Point_TemplateLocation))
{
cout<<"No Match Found";
}
/// Show me what you got
rectangle( Img_Result, Point_TemplateLocation, Point( Point_TemplateLocation.x + Img_Template_1.cols , Point_TemplateLocation.y + Img_Template_1.rows ), Scalar(0,0,255), 2, 8, 0 );
putText( Img_Result, format("Object %d ",i),Point( Point_TemplateLocation.x + Img_Template_1.cols/4 , Point_TemplateLocation.y + Img_Template_1.rows/2 ),1,1,Scalar(255,0,0),1,-1);
}
/// Create windows
namedWindow( image_window, CV_WINDOW_AUTOSIZE );
namedWindow( result_window, CV_WINDOW_AUTOSIZE );
imshow( image_window, Img_Template_1);
imshow( image_window, Img_Template_2);
imshow( result_window, Img_Result );
waitKey(0);
return 0;
}
I tried to write a script like this, but I am confused in the calculation process and bring the results of calculations
public void matching() {
String img_eq = Environment.getExternalStorageDirectory().getAbsolutePath() + "/img_eq/img_eq.jpg";
String template = Environment.getExternalStorageDirectory().getAbsolutePath() + "/img_template/img_template.jpg";
String template2 = Environment.getExternalStorageDirectory().getAbsolutePath() + "/img_template/img_template2.jpg";
Mat img = Highgui.imread(img_eq, CvType.CV_8SC3);
Mat templ = Highgui.imread(template, CvType.CV_8SC3);
Mat templ_2 = Highgui.imread(template2, CvType.CV_8SC3);
int match_method = Imgproc.TM_SQDIFF_NORMED;
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_8SC3);
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 = new Point();
//for (int i = 0; i < List_template_image.size(); i++)
{
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.
Highgui.imwrite("/mnt/sdcard/img_result/img_result.jpg", img);
Mat image = Highgui.imread("/mnt/sdcard/img_result/img_result.jpg");
Mat android_image = Mat.zeros(image.cols(), image.rows(), CvType.CV_8SC3);
Imgproc.cvtColor(image, android_image, Imgproc.COLOR_BGR2RGB);
Bitmap bm = Bitmap.createBitmap(android_image.cols(),android_image.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(android_image, bm);
ImageView iv = (ImageView) findViewById(R.id.image);
iv.setImageBitmap(bm);
}

opencv - matchTemplate

I am new to opencv.
I have a piece of code that finds a match to my template.
public static void findTemplete(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);
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);
Imgproc.matchTemplate(img, templ, result, match_method);
Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
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;
}
Core.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(),
matchLoc.y + templ.rows()), new Scalar(0, 255, 0));
System.out.println("Writing "+ outFile);
Highgui.imwrite(outFile, img);
}
My problem is that matchTemplate finds "The best match". So if my templete doesnt exists at all in the picture it finds something anyhow.
So how can i set the "Match strength", so that only strong matches will be found.
Remove the Normalize after your match Template. This will not allow the minmaxloc to give you proper numbers.
Here is an example:
double minVal; double maxVal=0; Point minLoc; Point maxLoc;
Point matchLoc;
matchTemplate ( frame, objectToFind, result, CV_TM_CCORR_NORMED );
minMaxLoc ( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat ( ) );
matchLoc = maxLoc;
if ( maxVal > .995 )
{
//we have a good match so do something
}
the .995 is what you change to a different number to help remove the bad matches.
Since we arbitrarily set this number, have it output the current value in your console using something like:
printf("My current maxVal: %f \n", maxVal);
This will help you gauge what you should set your value to.
Hope this helps :)

Categories