Android中的OpenCV模板匹配示例

我是OpenCV的初学者。 我试图做一个示例Android应用程序来匹配使用OpenCV模板匹配给定图像中的模板图像。 我在互联网search,我无法find一个合适的Android或Java代码,满足我的要求。 但是我有C ++代码。 我不知道如何翻译它。 http://docs.opencv.org/doc/tutorials/imgproc/histograms/template_matching/template_matching.html

你能帮我find一个合适的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; /// 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; } 

我正面临着同样的问题。 没有可用的Java源代码。 一些在JavaDoc中search和一些关于const值的提示后来写到了,这几乎是上面用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); } } 

现在,使用以下选项运行程序: lena.png template.png templatematch.png ,你应该得到和我一样的结果。 确保文件可以被运行时访问,当然,opencv 2.4.6库已经注册到你的classpath中。

lena.pngtemplate.pngtemplatematch.png

如果你想使用OpenCV 3以上,你应该使用这个代码

因为在OpenCV 3中没有Highgui,而应该使用imgcodecs。

 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.imgcodecs.Imgcodecs; 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 = 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 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, 255, 0)); // Save the visualized detection. System.out.println("Writing " + outFile); Imgcodecs.imwrite(outFile, img); } } public class TemplateMatching { public static void main(String[] args) { System.loadLibrary("opencv_java300"); new MatchingDemo().run(args[0], args[1], args[2], Imgproc.TM_CCOEFF); } } 
Interesting Posts