-2

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;
}
Community
  • 1
  • 1
beny_21
  • 1
  • 2
  • can you write java code for template matching for a single template? – Micka Jun 25 '14 at 12:53
  • i write a link to single template above – beny_21 Jun 26 '14 at 11:56
  • if single template matching works for you, just extend it by a second template (execute the same code for a second template). That would be the same that is done in the code you want to translate. – Micka Jun 26 '14 at 12:02
  • yes, it works. I also have the same thoughts like that. but I am very new in OpenCV and also in android. I wonder how the calculation in determining which one is more suitable template and display it in ImageView – beny_21 Jun 26 '14 at 13:13
  • well you extract the minMaxLoc. The values at that position give you the "quality" of the matching. So the one with the better quality is the better template. If one template is much bigger (in image size) you might need to care about that. Depending on the DIFF type good "quality" means to be big or small. – Micka Jun 26 '14 at 14:06
  • I wrote a script that I try below. what do you think?. I wonder how can I make the script can perform template matching process to the second template. and bring the results. I am still very new in android language – beny_21 Jun 26 '14 at 21:39

1 Answers1

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);
            }
beny_21
  • 1
  • 2