OpenCV Learning notes: Templates match Java version
First of all, I want to correct a wrong learning habits, such as OpenCV large libraries, follow the official tutorial step-by-Step debugging learning efficiency is too low, opencv like a dictionary, when we need to do some visual simulation of the computer, the specific problem to retrieve the corresponding API in the library.
Although the official tutorial is very detailed, in addition to the face recognition of the demo and a set of doc, there are no other Java instances, the detailed examples in the tutorial are written in C, I will be in the process of testing the corresponding part in the form of OOP as a Java module, and in the study notes posted.
The official tutorials can be found under the Build/doc directory of the OpenCV library, of course, in English.
[Introduction]
Template matching, popular speaking, provide the original picture with a part of it, find out the position of the part in the original picture, there are many limitations, the transpose and scaling of the template will seriously affect the matching results, but allow some distortion.
[Template Matching]
Templatemaching.java:
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;public class Templatemaching {private String sourcepath,dstpath;private Mat source, dst;//original picture public void SetSource (String picpath) {this.sourcepath = Picpath;} Part of public void Setdst (String picpath) {This.dstpath = Picpath required to be matched;} Processing, generating a result diagram public void process () {//reads the file into the OpenCV's mat format Source = Highgui.imread (sourcepath);d st = Highgui.imread (Dstpath );//Create the same size as the original, store the matching degree Mat result = Mat.zeros (Source.rows (), Source.cols (), CVTYPE.CV_32FC1);// Call template Matching method imgproc.matchtemplate (source, DST, Result,imgproc.tm_sqdiff);//normalized core.normalize (result, result, 0, 1, Core.norm_minmax,-1);//Get the most likely point, Minmaxlocresult is its data format, including the maximum, minimum point position x, Yminmaxlocresult MLR = Core.minmaxloc (result); Point matchloc = mlr.minloc;//the corresponding template on the original image may draw a green rectangle core.rectangle (source, Matchloc, new PoiNT (matchloc.x + dst.width (), Matchloc.y + dst.height ()), New Scalar (0,255,0)),//outputs the result to the corresponding location Highgui.imwrite ("./result/ Tmoutput.png ", source);} public static void Main (string[] args) {system.loadlibrary ("opencv_java249"); templatemaching macher = new templatemaching ();//Set the original Macher.setsource ("./data/lession4/bk.jpg");// Set the figure Macher.setdst ("./data/lession4/bk_dst_des.jpg") to match; Macher.process ();}}
[Test legend]
Original:
Match graph (original, transpose, zoom, distortion):
[test results][Original Test]
↑ Match
[transpose Test]
↑ Generate offsets
[Zoom Test]
↑ Generate offset, size error
[Distortion test]
↑ Match
[summary]
For the image recognition, the template mapping has a large limitation, in the future study will focus on looking for features matching content.
OPENCV learning notes [4] template matching Java version