模板与android中的多个模板匹配

时间:2014-06-25 12:32:13

标签: java android opencv template-matching

我是OpenCV的初学者。 我正在尝试使用多个模板进行模板匹配。

对于单个模板,我从这里获取源代码 OpenCV Template Matching example in Android

我在互联网上搜索,找不到符合我要求的合适的android或java代码。 我有C ++代码,但我不知道如何翻译它。 你能帮我找一个合适的java或android代码吗?或者请帮我把这个C ++代码翻译成java,我可以在android应用程序中使用。

提前谢谢。

C ++代码

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

1 个答案:

答案 0 :(得分:0)

我试着编写这样的脚本,但我在计算过程中感到困惑并带来了计算结果

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