i want match template (small image) in given picture using opencv.
i found following code port: opencv template matching example in android
the problem starting openvc 3.0.0, highgui broken down new videoio , imgcodecs , code below using highgui.
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 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); // / matching , normalize imgproc.matchtemplate(img, templ, result, match_method); core.normalize(result, result, 0, 1, core.norm_minmax, -1, new mat()); // / localizing best match 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 got core.rectangle(img, matchloc, new point(matchloc.x + templ.cols(), matchloc.y + templ.rows()), new scalar(0, 255, 0)); // save 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); } }
read-write functions moved org.opencv.highgui.highgui org.opencv.imgcodecs.imgcodecs. drawing functions moved org.opencv.core.core org.opencv.imgproc.imgproc. source becomes:
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 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); // / matching , normalize imgproc.matchtemplate(img, templ, result, match_method); core.normalize(result, result, 0, 1, core.norm_minmax, -1, new mat()); // / localizing best match 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 got imgproc.rectangle(img, matchloc, new point(matchloc.x + templ.cols(), matchloc.y + templ.rows()), new scalar(0, 255, 0)); // save 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); } }
Comments
Post a Comment