C# OpenCvSharp MatchTemplate 多目标匹配

目录

效果

项目

代码

下载


效果

项目

代码

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using OpenCvSharp;

using OpenCvSharp.Extensions;

namespace OpenCvSharp_MatchTemplate_多目标匹配

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

pictureBox1.Image = new Bitmap("test.png");

String tempImg_path = ("t1.png");

String srcImg_path = ("test.png");

Bitmap bitmap = Recoganize(srcImg_path, tempImg_path, 0.95, 1, "target", 10);

pictureBox2.Image = bitmap;

}

Bitmap Recoganize(String srcImg_path, String tempImg_path, double threshold = 0.5, double compressed = 0.5, string name = "target", int space = 10)

{

DateTime beginTime = DateTime.Now; //获取开始时间

// 新建图变量并分配内存

Mat srcImg = new Mat();

// 读取要被匹配的图像

srcImg = Cv2.ImRead(srcImg_path);

// 更改尺寸

Cv2.Resize(srcImg, srcImg, new OpenCvSharp.Size((int)srcImg.Cols * compressed, (int)srcImg.Rows * compressed));

// 初始化保存保存匹配结果的横纵坐标列表

List<int> Xlist = new List<int> { };

List<int> Ylist = new List<int> { };

int order = 0;

Mat tempImg = Cv2.ImRead(tempImg_path);

Cv2.Resize(tempImg, tempImg, new OpenCvSharp.Size((int)tempImg.Cols * compressed, (int)tempImg.Rows * compressed));

Mat result = srcImg.Clone();

int dstImg_rows = srcImg.Rows - tempImg.Rows + 1;

int dstImg_cols = srcImg.Cols - tempImg.Cols + 1;

Mat dstImg = new Mat(dstImg_rows, dstImg_cols, MatType.CV_32F, 1);

Cv2.MatchTemplate(srcImg, tempImg, dstImg, TemplateMatchModes.CCoeffNormed);

int count = 0;

for (int i = 0; i < dstImg_rows; i++)

{

for (int j = 0; j < dstImg_cols; j++)

{

float matchValue = dstImg.At<float>(i, j);

if (matchValue >= threshold && Xlist.Count == 0)

{

count++;

Cv2.Rectangle(result, new Rect(j, i, tempImg.Width, tempImg.Height), new Scalar(0, 255, 0), 2);

Cv2.PutText(result, name, new OpenCvSharp.Point(j, i - (int)20 * compressed), HersheyFonts.HersheySimplex, 0.5, new Scalar(0, 0, 0), 1);

Xlist.Add(j);

Ylist.Add(i);

}

if (matchValue >= threshold && Xlist.Count != 0)

{

for (int q = 0; q < Xlist.Count; q++)

{

if (Math.Abs(j - Xlist[q]) + Math.Abs(i - Ylist[q]) < space)

{

order = 1;

break;

}

}

if (order != 1)

{

count++;

Cv2.Rectangle(result, new Rect(j, i, tempImg.Width, tempImg.Height), new Scalar(0, 255, 0), 2);

Cv2.PutText(result, name, new OpenCvSharp.Point(j, i - (int)20 * compressed), HersheyFonts.HersheySimplex, 0.5, new Scalar(0, 0, 0), 1);

Xlist.Add(j);

Ylist.Add(i);

}

order = 0;

}

}

}

Console.WriteLine("目标数量:{0}", count);

DateTime endTime = DateTime.Now; //获取结束时间

TimeSpan oTime = endTime.Subtract(beginTime); //求时间差的函数

Console.WriteLine("程序的运行时间:{0} 毫秒", oTime.TotalMilliseconds);

return result.ToBitmap();

}

}

}

cs 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using OpenCvSharp;
using OpenCvSharp.Extensions;

namespace OpenCvSharp_MatchTemplate_多目标匹配
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = new Bitmap("test.png");
            String tempImg_path = ("t1.png");
            String srcImg_path = ("test.png");
            Bitmap bitmap = Recoganize(srcImg_path, tempImg_path, 0.95, 1, "target", 10);
            pictureBox2.Image = bitmap;
        }

        Bitmap Recoganize(String srcImg_path, String tempImg_path, double threshold = 0.5, double compressed = 0.5, string name = "target", int space = 10)
        {
            DateTime beginTime = DateTime.Now;            //获取开始时间  
            // 新建图变量并分配内存
            Mat srcImg = new Mat();
            // 读取要被匹配的图像
            srcImg = Cv2.ImRead(srcImg_path);
            // 更改尺寸
            Cv2.Resize(srcImg, srcImg, new OpenCvSharp.Size((int)srcImg.Cols * compressed, (int)srcImg.Rows * compressed));
            // 初始化保存保存匹配结果的横纵坐标列表
            List<int> Xlist = new List<int> { };
            List<int> Ylist = new List<int> { };

            int order = 0;
            Mat tempImg = Cv2.ImRead(tempImg_path);
            Cv2.Resize(tempImg, tempImg, new OpenCvSharp.Size((int)tempImg.Cols * compressed, (int)tempImg.Rows * compressed));
            Mat result = srcImg.Clone();

            int dstImg_rows = srcImg.Rows - tempImg.Rows + 1;
            int dstImg_cols = srcImg.Cols - tempImg.Cols + 1;
            Mat dstImg = new Mat(dstImg_rows, dstImg_cols, MatType.CV_32F, 1);
            Cv2.MatchTemplate(srcImg, tempImg, dstImg, TemplateMatchModes.CCoeffNormed);

            int count = 0;
            for (int i = 0; i < dstImg_rows; i++)
            {
                for (int j = 0; j < dstImg_cols; j++)
                {
                    float matchValue = dstImg.At<float>(i, j);
                    if (matchValue >= threshold && Xlist.Count == 0)
                    {
                        count++;
                        Cv2.Rectangle(result, new Rect(j, i, tempImg.Width, tempImg.Height), new Scalar(0, 255, 0), 2);
                        Cv2.PutText(result, name, new OpenCvSharp.Point(j, i - (int)20 * compressed), HersheyFonts.HersheySimplex, 0.5, new Scalar(0, 0, 0), 1);
                        Xlist.Add(j);
                        Ylist.Add(i);
                    }

                    if (matchValue >= threshold && Xlist.Count != 0)
                    {
                        for (int q = 0; q < Xlist.Count; q++)
                        {
                            if (Math.Abs(j - Xlist[q]) + Math.Abs(i - Ylist[q]) < space)
                            {
                                order = 1;
                                break;
                            }
                        }
                        if (order != 1)
                        {
                            count++;
                            Cv2.Rectangle(result, new Rect(j, i, tempImg.Width, tempImg.Height), new Scalar(0, 255, 0), 2);
                            Cv2.PutText(result, name, new OpenCvSharp.Point(j, i - (int)20 * compressed), HersheyFonts.HersheySimplex, 0.5, new Scalar(0, 0, 0), 1);
                            Xlist.Add(j);
                            Ylist.Add(i);
                        }
                        order = 0;
                    }
                }
            }
            Console.WriteLine("目标数量:{0}", count);
            DateTime endTime = DateTime.Now;              //获取结束时间  
            TimeSpan oTime = endTime.Subtract(beginTime); //求时间差的函数  
            Console.WriteLine("程序的运行时间:{0} 毫秒", oTime.TotalMilliseconds);
            return result.ToBitmap();
        }

    }
}

下载

Demo下载

相关推荐
lly2024069 分钟前
ASP Folder:深入解析其功能与使用技巧
开发语言
SmoothSailingT38 分钟前
C#窗体—子窗体获取父窗体TextBox框的值
c#·窗体
雪域迷影40 分钟前
Go语言中通过get请求获取api.open-meteo.com网站的天气数据
开发语言·后端·http·golang·get
ysdysyn3 小时前
C# 进程管理实战:检查与启动EXE程序的完整指南
开发语言·c#
IDOlaoluo3 小时前
PHP-5.2.1.tar.gz 离线安装教程:从源码编译到配置的详细步骤(附安装包)
开发语言·php
云缘若仙3 小时前
Godot游戏开发——C# (一)
c#·godot
wangjialelele3 小时前
Qt中的常用组件:QWidget篇
开发语言·前端·c++·qt
爱上妖精的尾巴4 小时前
5-26 WPS JS宏数组元素添加删除应用
开发语言·前端·javascript·wps·js宏
_OP_CHEN4 小时前
C++进阶:(三)深度解析二叉搜索树原理及实现
开发语言·数据结构·c++·二叉树·二叉搜索树·键值对
wxxka4 小时前
git使用
开发语言·git