﻿{"id":1638,"date":"2024-03-07T20:21:05","date_gmt":"2024-03-07T12:21:05","guid":{"rendered":"http:\/\/blog.sway.com.cn\/?p=1638"},"modified":"2024-03-12T10:54:27","modified_gmt":"2024-03-12T02:54:27","slug":"springboot%e4%b8%8b%e4%bd%bf%e7%94%a8opencv%e7%9a%84matchtemplate%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"http:\/\/blog.sway.com.cn\/?p=1638","title":{"rendered":"SpringBoot\u4e0b\u4f7f\u7528OpenCV\u7684MatchTemplate\u529f\u80fd"},"content":{"rendered":"\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u5728Windows\u4e0b\u7f16\u8bd1OpenCV\uff08\u5982\u5728\u5176\u4ed6\u7cfb\u7edf\u4e0b\u7f16\u8bd1\uff0c\u8bf7\u81ea\u884c\u641c\u7d22\uff09<\/h2>\n\n\n\n<p>CMake\uff1a<\/p>\n\n\n\n<p><a href=\"https:\/\/cmake.org\/download\/\">https:\/\/cmake.org\/download\/<\/a><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u4e0b\u8f7dzip\u7248\u672c\u201ccmake-3.xx-win64-x64.zip\u201d<\/li>\n\n\n\n<li>\u89e3\u538b\u5230\u6307\u5b9a\u76ee\u5f55\u5e76\u6dfb\u52a0bin\u6240\u5728\u76ee\u5f55\u5230\u73af\u5883\u53d8\u91cf\u3002<\/li>\n\n\n\n<li>\u5728\u7ec8\u7aef\u8f93\u5165\u201ccmake &#8211;help\u201d\u6216 \u201ccmake &#8211;version\u201d\u67e5\u770b\u662f\u5426\u914d\u7f6e\u6210\u529f\u3002<\/li>\n<\/ol>\n\n\n\n<p>MinGW\uff1a<\/p>\n\n\n\n<p><a href=\"https:\/\/sourceforge.net\/projects\/mingw-w64\/files\/\">https:\/\/sourceforge.net\/projects\/mingw-w64\/files\/<\/a><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u63a8\u8350\u4e0b\u8f7d \u79bb\u7ebf\u7248\u672c\u201cx86_64-posix-seh\u201d<\/li>\n\n\n\n<li>\u89e3\u538b\u5230\u6307\u5b9a\u76ee\u5f55\u5e76\u5c06bin\u76ee\u5f55\u52a0\u5165\u5230\u73af\u5883\u53d8\u91cf<\/li>\n\n\n\n<li>\u5728\u7ec8\u7aef\u8f93\u5165 \u201cgcc &#8211;version\u201d\u3002\u67e5\u770b\u662f\u5426\u5b89\u88c5\u6210\u529f\u3002<\/li>\n<\/ol>\n\n\n\n<p>OpenCV<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-opencv wp-block-embed-opencv\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"o3lkEVgWBH\"><a href=\"https:\/\/opencv.org\/releases\/\">Releases<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; visibility: hidden;\" title=\"&#8220;Releases&#8221; &#8212; OpenCV\" src=\"https:\/\/opencv.org\/releases\/embed\/#?secret=aqJWPO821w#?secret=o3lkEVgWBH\" data-secret=\"o3lkEVgWBH\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>\u672c\u6b21 Windows \u73af\u5883\u7684\u5b89\u88c5\uff0c\u56e0\u6b64\u4e0b\u8f7dOpenCV-4.6.0 Windows \u7248\u3002<\/p>\n\n\n\n<p>\u53cc\u51fb\u4e0b\u8f7d\u597d\u7684exe\u6587\u4ef6\uff0c\u7a0b\u5e8f\u4f1a\u751f\u6210 opencv \u6587\u4ef6\u5939\u3002<\/p>\n\n\n\n<p>\u8fd9\u4e2a opencv \u6587\u4ef6\u5939\u5c31\u662f\u6211\u4eec\u540e\u9762\u7684\u7f16\u8bd1\u6e90\u6587\u4ef6\u3002<\/p>\n\n\n\n<p>\u65b0\u5efa\u4e00\u4e2a\u6587\u4ef6\u5939 build_for_vscode \uff0c\u7528\u4e8e\u5b58\u653e\u6211\u4eec\u540e\u9762\u7684\u7f16\u8bd1\u7ed3\u679c\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e0b\u8f7dOpenCV\u8fd0\u884c\u5e93<\/h2>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-opencv wp-block-embed-opencv\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"o3lkEVgWBH\"><a href=\"https:\/\/opencv.org\/releases\/\">Releases<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; visibility: hidden;\" title=\"&#8220;Releases&#8221; &#8212; OpenCV\" src=\"https:\/\/opencv.org\/releases\/embed\/#?secret=aqJWPO821w#?secret=o3lkEVgWBH\" data-secret=\"o3lkEVgWBH\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<p>\u4e0b\u8f7dOpenCV \u2013 4.7.0\u7684Windows\u7248\u672c\uff0c\u8fd0\u884c\u5e76\u5236\u5b9a\u89e3\u538b\u76ee\u5f55\u3002<\/p>\n\n\n\n<p>\uff08\u6ce8\u610f\uff0c\u5982\u679c\u8fd9\u91cc\u9009\u62e9\u5176\u4ed6\u7248\u672c\uff0c\u8bf7\u52a1\u5fc5\u5728\u540e\u9762\u7684\u9879\u76ee\u4f9d\u8d56\u7528\u4e5f\u914d\u7f6e\u5bf9\u5e94\u76844.7.0\u7248\u672c\u3002\uff09<\/p>\n\n\n\n<p class=\"has-vivid-red-color has-text-color\">\u7136\u540e\u914d\u7f6e\u7cfb\u7edf\u7684\u73af\u5883\u53d8\u91cfPath\uff0c\u6dfb\u52a0\u201copencv\/build\/java\/x86\/opencv_java470.dll\u201d\u3002<\/p>\n\n\n\n<p>\u5426\u5219\u4f1a\u62a5\u7c7b\u4f3c\u5982\u4e0b\u7684\u9519\u8bef\uff1a<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">Exception in thread \"JavaFX Application Thread\" java.lang.UnsatisfiedLinkError: no opencv_java470 in java.library.path: D:\\Java\\jdk-17.0.8\\bin;C:\\Windows\\Sun\\Java\\bin;C:\\Windows\\system32;C:\\Windows;C:\\Program Files (x86)\\VMware\\VMware Workstation\\bin\\;C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath;C:\\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;C:\\Program Files (x86)\\NVIDIA Corporation\\PhysX\\Common;C:\\Program Files\\NVIDIA Corporation\\NVIDIA NvDLISR;C:\\Program Files\\Git\\cmd;C:\\Program Files\\TortoiseGit\\bin;C:\\Program Files (x86)\\Microsoft SQL Server\\160\\Tools\\Binn\\;C:\\Program Files\\Microsoft SQL Server\\160\\Tools\\Binn\\;C:\\Program Files\\Microsoft SQL Server\\Client SDK\\ODBC\\170\\Tools\\Binn\\;C:\\Program Files\\Microsoft SQL Server\\160\\DTS\\Binn\\;C:\\Program Files\\Docker\\Docker\\resources\\bin;D:\\Java\\jdk-1.8\\bin;C:\\Program Files\\JetBrains\\IntelliJ IDEA 2021.2\\plugins\\maven\\lib\\maven3\\bin;C:\\Program Files\\nodejs\\;C:\\Windows\\System32\\Wbem;D:\\Redis-x64-3.2.100;C:\\Program Files\\Go\\bin;C:\\Program Files\\Go\\bin;C:\\Program Files\\OpenSSL-Win64\\bin;D:\\MinGW-12.2.0\\bin;D:\\gradle-8.5\\bin;D:\\hkbea\\20231201.PBWMS Enhancement\\RmTool\\opencv\\build\\java\\x64;C:\\Program Files\\Tesseract-OCR;C:\\Users\\cp0612\\AppData\\Local\\Microsoft\\WindowsApps;C:\\Users\\cp0612\\AppData\\Roaming\\npm;C:\\Windows\\System32\\Wbem;C:\\Users\\cp0612\\AppData\\Local\\Programs\\Microsoft VS Code\\bin;C:\\Users\\cp0612\\go\\bin;C:\\Program Files\\CMake\\bin;.\n\tat java.base\/java.lang.ClassLoader.loadLibrary(ClassLoader.java:2429)\n\tat java.base\/java.lang.Runtime.loadLibrary0(Runtime.java:818)\n\tat java.base\/java.lang.System.loadLibrary(System.java:1989)\n\tat com.hkbea.pbwms.rmtool.MainApp$1.run(MainApp.java:151)\n\tat com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)\n\tat java.base\/java.security.AccessController.doPrivileged(AccessController.java:399)\n\tat com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)\n\tat com.sun.glass.ui.InvokeLaterDispatcher$Future.run$$$capture(InvokeLaterDispatcher.java:96)\n\tat com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java)\n\tat com.sun.glass.ui.win.WinApplication._runLoop(Native Method)\n\tat com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)\n\tat java.base\/java.lang.Thread.run(Thread.java:833)<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u5728\u9879\u76ee\u4e2d\u5f15\u5165\u4f9d\u8d56<\/h2>\n\n\n\n<p>\u4f9d\u8d56\u7248\u672c\u53ef\u4ee5\u67e5\u770b\u8fd9\u91cc\uff1ahttps:\/\/mvnrepository.com\/artifact\/org.bytedeco\/opencv<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\t\/\/ https:\/\/mvnrepository.com\/artifact\/org.bytedeco\/opencv\n\timplementation group: 'org.bytedeco', name: 'opencv', version: '4.7.0-1.5.9'<\/pre>\n\n\n\n<p>\u8bf7\u52a1\u5fc5\u6ce8\u610fversion\u9700\u8981\u548c\u6240\u4e0b\u8f7d\u7684OpenCV\u8fd0\u884c\u7248\u672c\u53f7\u4e00\u81f4\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u793a\u4f8b\u4ee3\u7801<\/h2>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/\/ \u52a0\u8f7dOpenCV\u5e93\nSystem.loadLibrary(Core.NATIVE_LIBRARY_NAME);\n\n\/\/ \u8bfb\u53d6\u6e90\u56fe\u50cf\u548c\u6a21\u677f\u56fe\u50cf\nMat sourceImage = Imgcodecs.imread(\"screenshot.png\");\nMat templateImage = Imgcodecs.imread(\"template.png\");\n\n\/\/ \u521b\u5efa\u7ed3\u679c\u77e9\u9635\nint resultCols = sourceImage.cols() - templateImage.cols() + 1;\nint resultRows = sourceImage.rows() - templateImage.rows() + 1;\n\/\/Mat resultImage = new Mat(resultRows, resultCols, CvType.CV_32F);\nMat resultImage = new Mat(resultRows, resultCols, CvType.CV_32FC1);\n\n\/\/ \u6267\u884c\u6a21\u677f\u5339\u914d\n\/\/ \u6ce8\u610f\uff1a\u8fd9\u91cc\u53ea\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5b9e\u9645\u7684\u6a21\u677f\u5339\u914d\u9700\u8981\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u53c2\u6570\u8c03\u6574\u548c\u5904\u7406\n\/**\n * result \u53c2\u6570\uff1a\u7528\u4e8e\u5b58\u50a8\u5339\u914d\u7ed3\u679c\u7684\u77e9\u9635\uff0c\u60a8\u53ef\u4ee5\u68c0\u67e5\u8fd9\u4e2a\u77e9\u9635\u6765\u4e86\u89e3\u5339\u914d\u7684\u60c5\u51b5\u3002\n * method \u53c2\u6570\uff1a\u6307\u5b9a\u5339\u914d\u65b9\u6cd5\u3002\u5e38\u89c1\u7684\u5339\u914d\u65b9\u6cd5\u5305\u62ec Imgproc.TM_SQDIFF\u3001 Imgproc.TM_SQDIFF_NORMED\u3001Imgproc.TM_CCORR\u3001Imgproc.TM_CCORR_NORMED\u3001Imgproc.TM_CCOEFF \u548c Imgproc.TM_CCOEFF_NORMED\uff0c\u5b83\u4eec\u4f1a\u5f71\u54cd\u5230\u5339\u914d\u7ed3\u679c\u7684\u7cbe\u786e\u5ea6\u548c\u51c6\u786e\u5ea6\u3002\n * mask \u53c2\u6570\uff1a\u6307\u5b9a\u6a21\u677f\u7684\u63a9\u7801\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b83\u6765\u9650\u5236\u5339\u914d\u533a\u57df\uff0c\u63d0\u9ad8\u5339\u914d\u7684\u51c6\u786e\u5ea6\u3002\n * scale \u548c scaleFactor \u53c2\u6570\uff1a\u6307\u5b9a\u641c\u7d22\u56fe\u50cf\u548c\u6a21\u677f\u56fe\u50cf\u7684\u7f29\u653e\u6bd4\u4f8b\uff0c\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u8fd9\u4e9b\u53c2\u6570\u6765\u9002\u5e94\u4e0d\u540c\u5c3a\u5bf8\u7684\u56fe\u50cf\u3002\n *\/\nImgproc.matchTemplate(sourceImage, templateImage, resultImage, Imgproc.TM_CCOEFF_NORMED);\n\n\/\/ --------- \u5904\u7406\u5339\u914d\u7ed3\u679c ----------\n\n\/\/\u5339\u914d\u9608\u503c\u5904\u7406\n\/\/double thresholdValue = 0.8;\n\/\/Core.compare(resultImage, new Scalar(thresholdValue), resultImage, Core.CMP_GE);\n\n\/\/ \u5bfb\u627e\u6700\u5927\u548c\u6700\u5c0f\u5339\u914d\u503c\u53ca\u5176\u4f4d\u7f6e\nCore.MinMaxLocResult mmr = Core.minMaxLoc(resultImage);\n\n\/\/\u6bd4\u5bf9\u5339\u914d\u5230\u7684\u56fe\u50cf\u7ed3\u679c\ndouble threshold = 0.2; \/\/ \u8bbe\u7f6e\u5339\u914d\u9608\u503c\nif (mmr.maxVal >= threshold) {\n    \/\/log.info(\"\u5339\u914d\u6210\u529f\");\n    System.out.println(\"\u5339\u914d\u6210\u529f\");\n    Point maxLoc = mmr.maxLoc;\n    \/\/ \u5728\u539f\u59cb\u56fe\u50cf\u4e2d\u753b\u51fa\u5339\u914d\u7ed3\u679c\u7684\u77e9\u5f62\u6846\n    Rect matchRect = new Rect((int)maxLoc.x, (int)maxLoc.y, templateImage.cols(), templateImage.rows());\n    Imgproc.rectangle(sourceImage, matchRect.tl(), matchRect.br(), new Scalar(0, 255, 0), 2);\n\n    \/\/ \u5c06 Mat \u5bf9\u8c61\u8f6c\u6362\u4e3a BufferedImage\n    \/\/imageView.setImage(SwingFXUtils.toFXImage(matToBufferedImage(resultImage),null));\n    BufferedImage resultBuffImage = Mat2BufImg(sourceImage,\".png\");\n    WritableImage rersultWritableImage = SwingFXUtils.toFXImage(resultBuffImage,null);\n    File resultImageFile = new File(\"outputImage.png\");\n    try {\n        ImageIO.write(SwingFXUtils.fromFXImage(rersultWritableImage, null), \"png\", resultImageFile);\n        System.out.println(\"\u5c4f\u5e55\u622a\u56fe\u5df2\u4fdd\u5b58\u5230: \" + resultImageFile.getAbsolutePath()+\"; \u5927\u5c0f=\"+resultImageFile.length());\n    } catch (IOException e) {\n        System.out.println(\"\u65e0\u6cd5\u4fdd\u5b58\u5c4f\u5e55\u622a\u56fe: \" + e.getMessage());\n    }\n    imageView.setImage(rersultWritableImage);\n} else {\n    \/\/ \u672a\u627e\u5230\u5339\u914d\n    \/\/log.info(\"\u5339\u914d\u5931\u8d25\");\n    System.out.println(\"\u5339\u914d\u5931\u8d25\");\n}\n\n\/\/ \u91ca\u653e\u8d44\u6e90\nsourceImage.release();\ntemplateImage.release();\nresultImage.release();<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u5728Windows\u4e0b\u7f16\u8bd1OpenCV\uff08\u5982\u5728\u5176\u4ed6\u7cfb\u7edf\u4e0b\u7f16\u8bd1\uff0c\u8bf7\u81ea\u884c\u641c\u7d22\uff09 CMake &hellip; <a href=\"http:\/\/blog.sway.com.cn\/?p=1638\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1638","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/1638","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1638"}],"version-history":[{"count":5,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/1638\/revisions"}],"predecessor-version":[{"id":1653,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/1638\/revisions\/1653"}],"wp:attachment":[{"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1638"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.sway.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}