<h1 style="font-size: 32px; border-bottom: 2px solid rgb(204, 204, 204); padding: 0px 4px 0px 0px; margin: 0px 0px 10px;">多对象模板匹配</h1><p>本章节内容是博主网上收集的,PDF原文下载地址如下:<br/></p><p style="text-wrap: wrap;"><a href="/api/system/download?file=OpenCV-Python-Tutorial-%E4%B8%AD%E6%96%87%E7%89%88.pdf" target="_blank">OpenCV-Python-Tutorial-中文版.pdf</a></p><p style="text-wrap: wrap;"><a href="/upload/file/OpenCV-Python-Tutorial-%E4%B8%AD%E6%96%87%E7%89%88.pdf" target="_blank" title="PDF在线预览">PDF在线预览</a></p><p style="text-wrap: wrap;"><br/><a href="/upload/file/OpenCV-Python-Tutorial-%E4%B8%AD%E6%96%87%E7%89%88.pdf" target="_blank" title="在线预览"></a></p><p style="margin-top: 0px; margin-bottom: 10px; text-wrap: wrap; padding: 0px; list-style: none; border: 0px; overflow-wrap: break-word; word-break: break-all; line-height: 1.5em; font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "WenQuanYi Micro Hei", Arial, sans-serif; color: rgb(51, 51, 51); box-sizing: border-box;">当前系列所有demo下载地址:</p><p style="margin-top: 0px; margin-bottom: 10px; text-wrap: wrap; padding: 0px; list-style: none; border: 0px; overflow-wrap: break-word; word-break: break-all; line-height: 1.5em; font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "WenQuanYi Micro Hei", Arial, sans-serif; color: rgb(51, 51, 51); box-sizing: border-box;"><a href="https://github.com/GaoRenBao/OpenCv4-Demo" target="_blank" style="margin: 0px; padding: 0px; list-style: none; border: 0px; color: rgb(0, 102, 0); transition-duration: 0.2s; transition-property: opacity; outline: none; opacity: 0.8;">https://github.com/GaoRenBao/OpenCv4-Demo</a></p><p style="margin-top: 0px; margin-bottom: 10px; text-wrap: wrap; padding: 0px; list-style: none; border: 0px; overflow-wrap: break-word; word-break: break-all; line-height: 1.5em; font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "WenQuanYi Micro Hei", Arial, sans-serif; box-sizing: border-box; color: rgb(51, 51, 51);">不同编程语言对应的OpenCv版本以及<span style="">开发环境</span>信息如下:<span style="font-family: Calibri; font-size: 14px;"> </span></p><table border="1" style="border-right: none; border-bottom: none; border-image: initial; border-left: 1px solid rgb(102, 102, 102); border-top: 1px solid rgb(102, 102, 102);"><tbody><tr class="firstRow"><td width="81" valign="top" style="border-color: windowtext rgb(102, 102, 102) rgb(102, 102, 102) windowtext; border-bottom-width: 1px; border-bottom-style: solid; border-right-width: 1px; border-right-style: solid; padding: 5px;"><p style="text-align: center;"><strong><span style="font-family: 宋体; font-size: 14px;">语言</span></strong></p></td><td width="223" valign="top" style="border-color: windowtext rgb(102, 102, 102) rgb(102, 102, 102) windowtext; border-bottom-width: 1px; border-bottom-style: solid; border-right-width: 1px; border-right-style: solid; padding: 5px;"><p style="text-align: center;"><strong><span style="font-family: 宋体; font-size: 14px;"><span style="font-family: Calibri;">OpenCv</span>版本</span></strong></p></td><td width="242" valign="top" style="border-color: windowtext rgb(102, 102, 102) rgb(102, 102, 102) windowtext; border-bottom-width: 1px; border-bottom-style: solid; border-right-width: 1px; border-right-style: solid; padding: 5px;"><p style="text-align: center;"><strong><span style="font-family: 宋体; font-size: 14px;"><span style="font-family: Calibri;">IDE</span></span></strong></p></td></tr><tr><td width="81" valign="top" style="border-top: none; border-left-color: windowtext; border-bottom: 1px solid rgb(102, 102, 102); border-right: 1px solid rgb(102, 102, 102); padding: 5px;"><p><span style="font-family: 宋体; font-size: 14px;"><span style="font-family: Calibri;">C#</span></span></p></td><td width="223" valign="top" style="border-top: none; border-left-color: windowtext; border-bottom: 1px solid rgb(102, 102, 102); border-right: 1px solid rgb(102, 102, 102); padding: 5px;"><p><span style="font-family: Calibri; letter-spacing: 0px;">OpenCvSharp4.4.8.0.20230708</span></p></td><td width="242" valign="top" style="border-top: none; border-left-color: windowtext; border-bottom: 1px solid rgb(102, 102, 102); border-right: 1px solid rgb(102, 102, 102); padding: 5px;"><p><span style="font-family: 宋体; font-size: 14px;"><span style="font-family: Calibri;">Visual Studio 2022</span></span></p></td></tr><tr><td width="81" valign="top" style="border-top: none; border-left-color: windowtext; border-bottom: 1px solid rgb(102, 102, 102); border-right: 1px solid rgb(102, 102, 102); padding: 5px;"><p><span style="font-family: 宋体; font-size: 14px;"><span style="font-family: Calibri;">C++</span></span></p></td><td width="223" valign="top" style="border-top: none; border-left-color: windowtext; border-bottom: 1px solid rgb(102, 102, 102); border-right: 1px solid rgb(102, 102, 102); padding: 5px;"><p><span style="font-family: 宋体; letter-spacing: 0px;"><span style="font-family: Calibri;">O</span></span><span style="font-family: Calibri; letter-spacing: 0px;">pen</span><span style="font-family: 宋体; letter-spacing: 0px;"><span style="font-family: Calibri;">C</span></span><span style="font-family: Calibri; letter-spacing: 0px;">v-4.5.5-vc14_vc15</span></p></td><td width="242" valign="top" style="border-top: none; border-left-color: windowtext; border-bottom: 1px solid rgb(102, 102, 102); border-right: 1px solid rgb(102, 102, 102); padding: 5px;"><p><span style="font-family: 宋体; font-size: 14px;"><span style="font-family: Calibri;">Visual Studio 2022</span></span></p></td></tr><tr><td width="81" valign="top" style="border-top: none; border-left-color: windowtext; border-bottom: 1px solid rgb(102, 102, 102); border-right: 1px solid rgb(102, 102, 102); padding: 5px;"><p><span style="font-family: 宋体; font-size: 14px;"><span style="font-family: Calibri;">Python</span></span></p></td><td width="223" valign="top" style="border-top: none; border-left-color: windowtext; border-bottom: 1px solid rgb(102, 102, 102); border-right: 1px solid rgb(102, 102, 102); padding: 5px;"><p><span style="font-family: 宋体; font-size: 14px;"><span style="font-family: Calibri;">OpenCv-Python (4.6.0.66)</span></span></p></td><td width="242" valign="top" style="border-top: none; border-left-color: windowtext; border-bottom: 1px solid rgb(102, 102, 102); border-right: 1px solid rgb(102, 102, 102); padding: 5px;"><p><span style="font-family: 宋体; font-size: 14px;"><span style="font-family: Calibri;">PyCharm Community Edition 2022.1.3</span></span></p></td></tr></tbody></table><p><br/></p><p style="text-wrap: wrap;">测试原图如下</p><p style="text-wrap: wrap;"><img src="/upload/image/6383428245047688317945022.png" title="mario.png" alt="mario.png"/></p><p style="text-wrap: wrap;"><br/></p><p style="text-wrap: wrap;"><img src="/upload/image/6383428245541450659549917.png" title="mario_coin.png" alt="mario_coin.png"/></p><p style="text-wrap: wrap;"><br/></p><p style="text-wrap: wrap;">匹配结果如下:</p><p style="text-wrap: wrap;"><img src="/upload/image/6383428246812005621345166.jpg" title="res.jpg" alt="res.jpg"/></p><p style="text-wrap: wrap;"> </p><p style="text-wrap: wrap;"><strong style="margin: 0px; padding: 0px; list-style: none; border: 0px; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "WenQuanYi Micro Hei", Arial, sans-serif; "><span style="font-size: 16px; font-style: italic; font-weight: bold; color: rgb(51, 153, 204); line-height: 18px;">C#版本运行代码如下:</span></strong></p><pre class="brush:c#;toolbar:false">using OpenCvSharp;
namespace demo
{
internal class Program
{
static void Main(string[] args)
{
Mat img_rgb = Cv2.ImRead("../../../images/mario.png");
Mat img_gray = new Mat();
Cv2.CvtColor(img_rgb, img_gray, ColorConversionCodes.BGR2GRAY);
Mat template = Cv2.ImRead("../../../images/mario_coin.png", 0);
int w = template.Width;
int h = template.Height;
Mat res = new Mat();
Cv2.MatchTemplate(img_gray, template, res, TemplateMatchModes.CCoeffNormed);
double threshold = 0.8;
Cv2.Threshold(res, res, threshold, 1.0, ThresholdTypes.Tozero);
res.FindNonZero().GetArray(out Point[] points);
for (int i = 0; i < points.Length; i++)
{
Point pt = points[i];
Cv2.Rectangle(img_rgb, pt, new Point(pt.X + w, pt.Y + h), new Scalar(0, 255, 0), 2);
}
Cv2.ImShow("result", img_rgb);
Cv2.WaitKey(0);
}
}
}</pre><p style="text-wrap: wrap;"><br/></p><p style="text-wrap: wrap;"><strong style="margin: 0px; padding: 0px; list-style: none; border: 0px; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "WenQuanYi Micro Hei", Arial, sans-serif; "><strong style="margin: 0px; padding: 0px; list-style: none; border: 0px;"><span style="font-size: 16px; font-style: italic; font-weight: bold; color: rgb(51, 153, 204); line-height: 18px;">C++<strong style="margin: 0px; padding: 0px; list-style: none; border: 0px;">版本运行代码如下:</strong></span><strong style="margin: 0px; padding: 0px; list-style: none; border: 0px;"></strong></strong></strong></p><p style="text-wrap: wrap;"><strong style="margin: 0px; padding: 0px; list-style: none; border: 0px; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "WenQuanYi Micro Hei", Arial, sans-serif; "><strong style="margin: 0px; padding: 0px; list-style: none; border: 0px;"><strong style="margin: 0px; padding: 0px; list-style: none; border: 0px;"></strong></strong></strong></p><pre class="brush:cpp;toolbar:false;">#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat img_rgb = cv::imread("../images/mario.png");
Mat img_gray;
cv::cvtColor(img_rgb, img_gray, COLOR_BGR2GRAY);
Mat temp = cv::imread("../images/mario_coin.png", 0);
int w = temp.cols;
int h = temp.rows;
Mat res;
cv::matchTemplate(img_gray, temp, res, cv::TM_CCOEFF_NORMED);
double threshold = 0.8;
cv::threshold(res, res, threshold, 1.0, cv::THRESH_TOZERO);
vector<cv::Point> points;
cv::findNonZero(res, points);
for (int i = 0; i < points.size(); i++)
{
Point pt = points[i];
cv::rectangle(img_rgb, pt, Point(pt.x + w, pt.y + h), Scalar(0, 255, 0), 2);
}
cv::imshow("result", img_rgb);
cv::waitKey(0);
}</pre><p style="text-wrap: wrap;"><br/></p><p style="text-wrap: wrap;"><strong style="margin: 0px; padding: 0px; list-style: none; border: 0px; color: rgb(51, 51, 51); font-family: "Helvetica Neue", Helvetica, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "WenQuanYi Micro Hei", Arial, sans-serif; "><strong style="margin: 0px; padding: 0px; list-style: none; border: 0px;"><span style="font-size: 16px; font-style: italic; font-weight: bold; color: rgb(51, 153, 204); line-height: 18px;">Python<strong style="margin: 0px; padding: 0px; list-style: none; border: 0px;">版本运行代码如下:</strong></span><strong style="margin: 0px; padding: 0px; list-style: none; border: 0px;"></strong></strong></strong></p><pre class="brush:python;toolbar:false">import cv2
import numpy as np
img_rgb = cv2.imread('../images/mario.png')
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread('../images/mario_coin.png', 0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where(res >= threshold)
print(len(loc))
for pt in zip(*loc[::-1]):
cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 255, 0), 2)
print("rectangle 1")
cv2.imwrite('res.jpg',img_rgb)
cv2.imshow("result", img_rgb)
cv2.waitKey(0)</pre><p><br/></p><p><br/></p>
多对象模板匹配 本章节内容是博主网上收集的,PDF原文下载地址如下:
OpenCV-Python-Tutorial-中文版.pdf
PDF在线预览
当前系列所有demo下载地址:
https://github.com/GaoRenBao/OpenCv4-Demo
不同编程语言对应的OpenCv版本以及开发环境 信息如下:
语言
OpenCv 版本
IDE
C#
OpenCvSharp4.4.8.0.20230708
Visual Studio 2022
C++
O pen C v-4.5.5-vc14_vc15
Visual Studio 2022
Python
OpenCv-Python (4.6.0.66)
PyCharm Community Edition 2022.1.3
测试原图如下
匹配结果如下:
C#版本运行代码如下:
using OpenCvSharp;
namespace demo
{
internal class Program
{
static void Main(string[] args)
{
Mat img_rgb = Cv2.ImRead("../../../images/mario.png");
Mat img_gray = new Mat();
Cv2.CvtColor(img_rgb, img_gray, ColorConversionCodes.BGR2GRAY);
Mat template = Cv2.ImRead("../../../images/mario_coin.png", 0);
int w = template.Width;
int h = template.Height;
Mat res = new Mat();
Cv2.MatchTemplate(img_gray, template, res, TemplateMatchModes.CCoeffNormed);
double threshold = 0.8;
Cv2.Threshold(res, res, threshold, 1.0, ThresholdTypes.Tozero);
res.FindNonZero().GetArray(out Point[] points);
for (int i = 0; i < points.Length; i++)
{
Point pt = points[i];
Cv2.Rectangle(img_rgb, pt, new Point(pt.X + w, pt.Y + h), new Scalar(0, 255, 0), 2);
}
Cv2.ImShow("result", img_rgb);
Cv2.WaitKey(0);
}
}
}
C++版本运行代码如下:
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat img_rgb = cv::imread("../images/mario.png");
Mat img_gray;
cv::cvtColor(img_rgb, img_gray, COLOR_BGR2GRAY);
Mat temp = cv::imread("../images/mario_coin.png", 0);
int w = temp.cols;
int h = temp.rows;
Mat res;
cv::matchTemplate(img_gray, temp, res, cv::TM_CCOEFF_NORMED);
double threshold = 0.8;
cv::threshold(res, res, threshold, 1.0, cv::THRESH_TOZERO);
vector<cv::Point> points;
cv::findNonZero(res, points);
for (int i = 0; i < points.size(); i++)
{
Point pt = points[i];
cv::rectangle(img_rgb, pt, Point(pt.x + w, pt.y + h), Scalar(0, 255, 0), 2);
}
cv::imshow("result", img_rgb);
cv::waitKey(0);
}
Python版本运行代码如下:
import cv2
import numpy as np
img_rgb = cv2.imread('../images/mario.png')
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
template = cv2.imread('../images/mario_coin.png', 0)
w, h = template.shape[::-1]
res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where(res >= threshold)
print(len(loc))
for pt in zip(*loc[::-1]):
cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 255, 0), 2)
print("rectangle 1")
cv2.imwrite('res.jpg',img_rgb)
cv2.imshow("result", img_rgb)
cv2.waitKey(0)