Android中的OpenCV模板匹配示例

Android中的OpenCV模板匹配示例

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

问题描述

我是 OpenCV 的初学者.我正在尝试做一个示例 android 应用程序,以使用 OpenCV 模板匹配来匹配给定图像中的模板图像.我在互联网上搜索,但找不到满足我要求的合适的 android 或 java 代码.但我有 C++ 代码.我不知道怎么翻译.http://docs.opencv.org/doc/tutorials/imgproc/histograms/template_matching/template_matching.html

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

你能帮我找到一个合适的java或android代码吗?或者请帮我把这个C++代码翻译成java,我可以在android应用程序中使用.

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.

提前致谢.

C++ 代码

#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:
 0: SQDIFF
 1: SQDIFF NORMED
 2: TM CCORR
 3: TM CCORR NORMED
 4: TM COEFF
 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;
}

推荐答案

我遇到了和你一样的问题.没有可用的 Java 源代码.后来在JavaDoc中搜索了一些const值的提示,我写了这个,几乎就是上面用Java写的示例代码:

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("
Running 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);
    }
}

现在,使用以下选项运行程序:lena.png template.png templatematch.png,您应该会收到与我相同的结果.确保您的运行时可以访问这些文件,当然,opencv 2.4.6 库已注册到您的类路径.

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模板匹配示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 16:11