OpenCV的模板匹配例如

OpenCV的模板匹配例如

本文介绍了OpenCV的模板匹配例如,在安卓的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个初学者的OpenCV。我试图做一个样本Android应用程序,以匹配模板图像使用OpenCV的模板匹配给定的图像。我搜索在互联网上,我无法找到一个合适的Andr​​oid或Java的code满足我的要求。但我有C ++ code。我不知道如何翻译它。http://docs.opencv.org/doc/tutorials/imgproc/histograms/template_matching/template_matching.html

能否请你帮我找到合适的Java或者Android code。否则,请帮我翻译这个C ++ code到Java,我可以使用里面的Andr​​oid应用程序。

感谢你在前进。

C ++ code

 的#includeopencv2 /一下HighGUI / highgui.hpp
#包括opencv2 / imgproc / imgproc.hpp
#包括<的iostream>
#包括< stdio.h中>

使用名字空间std;
使用命名空间的简历;

///全局变量
垫IMG;垫TEMPL;垫的结果;
char *之image_window =源图像;
char *之result_window =结果窗口;

INT match_method;
INT max_Trackbar = 5;

///功能标题
无效MatchingMethod(INT,无效*);

/ ** @function主* /
INT主(INT ARGC,字符** argv的)
{
  ///加载图像和模板
  的img = imread(的argv [1],1);
  TEMPL = imread(的argv [2],1);

  ///创建Windows
  namedWindow(image_window,CV_WINDOW_AUTOSIZE);
  namedWindow(result_window,CV_WINDOW_AUTOSIZE);

  ///创建的TrackBar
  字符* trackbar_label =方法:\ñ0:SQDIFF \ N + 1:SQDIFF赋范\ N 2:TM CCORR \ñ3:TM CCORR赋范\ñ4:TM COEFF \ N 5:TM COEFF赋范;
  createTrackbar(trackbar_label,image_window,和放大器; match_method,max_Trackbar,MatchingMethod);

  MatchingMethod(0,0);

  waitKey(0);
  返回0;
}

/ **
 * @function MatchingMethod
 * @brief的TrackBar回调
 * /
无效MatchingMethod(INT,无效*)
{
  ///源图像显示
  太img_display;
  img.copyTo(img_display);

  ///创建结果矩阵
  INT result_cols = img.cols  -  templ.cols + 1;
  INT result_rows = img.rows  -  templ.rows + 1;

  result.create(result_cols,result_rows,CV_32FC1);

  ///做匹配和规范
  matchTemplate(IMG,TEMPL,结果,match_method);
  正常化(结果,结果是,0,1,NORM_MINMAX,-1,垫());

  ///本地化与minMaxLoc最佳匹配
  双MINVAL;双MAXVAL;点minLoc;点MAXLOC;
  点matchLoc;

  minMaxLoc(结果,与放大器; MINVAL,和放大器; MAXVAL,和放大器; minLoc,和放大器; MAXLOC,垫());

  ///对于SQDIFF和SQDIFF_NORMED,最佳匹配较低值。对于所有的其他的方法,越高越好
  如果(match_method == CV_TM_SQDIFF || match_method == CV_TM_SQDIFF_NORMED)
    {matchLoc = minLoc; }
  其他
    {matchLoc = MAXLOC; }

  /// 告诉我你得到了什么
  矩形(img_display,matchLoc,点(matchLoc.x + templ.cols,matchLoc.y + templ.rows),标量::所有(0),2,8,0);
  矩形(结果,matchLoc,点(matchLoc.x + templ.cols,matchLoc.y + templ.rows),标量::所有(0),2,8,0);

  imshow(image_window,img_display);
  imshow(result_window,结果);

  返回;
}
 

解决方案

我面临你做了同样的问题。没有源在Java中可用。在JavaDoc一些搜索和一些提示常量值后,我写了这个,这几乎是样品code以上用Java编写的:

 包OpenCV的;

进口org.opencv.core.Core;
进口org.opencv.core.Core.MinMaxLocResult;
进口org.opencv.core.CvType;
进口org.opencv.core.Mat;
进口org.opencv.core.Point;
进口org.opencv.core.Scalar;
进口org.opencv.highgui.Highgui;
进口org.opencv.imgproc.Imgproc;

类MatchingDemo {
    公共无效的run(字符串INFILE,字符串templateFile,字符串不过outFile,INT match_method){
        的System.out.println(\ N运行模板匹配);

        垫IMG = Highgui.imread(INFILE);
        垫TEMPL = Highgui.imread(templateFile);

        // /创建结果矩阵
        INT result_cols = img.cols() -  templ.cols()+ 1;
        INT result_rows = img.rows() -  templ.rows()+ 1;
        垫结果=新材料(result_rows,result_cols,CvType.CV_32FC1);

        // /做匹配和规范
        Imgproc.matchTemplate(IMG,TEMPL,结果,match_method);
        Core.normalize(结果,结果是,0,1,Core.NORM_MINMAX,-1,新垫());

        // /本地化与minMaxLoc最佳匹配
        MinMaxLocResult MMR = Core.minMaxLoc(结果);

        点matchLoc;
        如果(match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED){
            matchLoc = mmr.minLoc;
        } 其他 {
            matchLoc = mmr.maxLoc;
        }

        // / 告诉我你得到了什么
        Core.rectangle(IMG,matchLoc,新点(matchLoc.x + templ.cols()
                matchLoc.y + templ.rows()),新的标量(0,255,0));

        //保存的可视化检测。
        的System.out.println(写+不过outFile);
        Highgui.imwrite(不过outFile,IMG);

    }
}

公共类TemplateMatching {
    公共静态无效的主要(字串[] args){
        的System.loadLibrary(opencv_java246);
        新MatchingDemo()跑(参数[0],的args [1]的args [2],Imgproc.TM_CCOEFF)。
    }
}
 

现在,用下面的选项运行程序: lena.png template.png templatematch.png ,你应该得到同样的结果我做到了。请当然,确保文件被运行时到达,并,OpenCV的2.4.6库注册到类路径中。

I'm a beginner to OpenCV. I'm trying to do a sample android application to match a template image in a given image using OpenCV Template matching. I searched in the internet and I couldn't find a proper android or java code which satisfy my requirement. But I have C++ code. I dont know how to translate it.http://docs.opencv.org/doc/tutorials/imgproc/histograms/template_matching/template_matching.html

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;

/// Global Variables
Mat img; Mat templ; Mat result;
char* image_window = "Source Image";
char* result_window = "Result window";

int match_method;
int max_Trackbar = 5;

/// Function Headers
void MatchingMethod( int, void* );

/** @function main */
int main( int argc, char** argv )
{
  /// Load image and template
  img = imread( argv[1], 1 );
  templ = imread( argv[2], 1 );

  /// Create windows
  namedWindow( image_window, CV_WINDOW_AUTOSIZE );
  namedWindow( result_window, CV_WINDOW_AUTOSIZE );

  /// Create Trackbar
  char* trackbar_label = "Method: \n 0: SQDIFF \n 1: SQDIFF NORMED \n 2: TM CCORR \n 3: TM CCORR NORMED \n 4: TM COEFF \n 5: TM COEFF NORMED";
  createTrackbar( trackbar_label, image_window, &match_method, max_Trackbar, MatchingMethod );

  MatchingMethod( 0, 0 );

  waitKey(0);
  return 0;
}

/**
 * @function MatchingMethod
 * @brief Trackbar callback
 */
void MatchingMethod( int, void* )
{
  /// Source image to display
  Mat img_display;
  img.copyTo( img_display );

  /// Create the result matrix
  int result_cols =  img.cols - templ.cols + 1;
  int result_rows = img.rows - templ.rows + 1;

  result.create( result_cols, result_rows, CV_32FC1 );

  /// Do the Matching and Normalize
  matchTemplate( img, templ, result, match_method );
  normalize( result, result, 0, 1, NORM_MINMAX, -1, Mat() );

  /// Localizing the best match with minMaxLoc
  double minVal; double maxVal; Point minLoc; Point maxLoc;
  Point matchLoc;

  minMaxLoc( result, &minVal, &maxVal, &minLoc, &maxLoc, Mat() );

  /// For SQDIFF and SQDIFF_NORMED, the best matches are lower values. For all the other methods, the higher the better
  if( match_method  == CV_TM_SQDIFF || match_method == CV_TM_SQDIFF_NORMED )
    { matchLoc = minLoc; }
  else
    { matchLoc = maxLoc; }

  /// Show me what you got
  rectangle( img_display, matchLoc, Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), Scalar::all(0), 2, 8, 0 );
  rectangle( result, matchLoc, Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), Scalar::all(0), 2, 8, 0 );

  imshow( image_window, img_display );
  imshow( result_window, result );

  return;
}
解决方案

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.

这篇关于OpenCV的模板匹配例如,在安卓的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 16:11