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下载

相关推荐
萧鼎1 小时前
Python并发编程库:Asyncio的异步编程实战
开发语言·数据库·python·异步
学地理的小胖砸1 小时前
【一些关于Python的信息和帮助】
开发语言·python
疯一样的码农1 小时前
Python 继承、多态、封装、抽象
开发语言·python
^velpro^1 小时前
数据库连接池的创建
java·开发语言·数据库
秋の花1 小时前
【JAVA基础】Java集合基础
java·开发语言·windows
小松学前端1 小时前
第六章 7.0 LinkList
java·开发语言·网络
可峰科技1 小时前
斗破QT编程入门系列之二:认识Qt:编写一个HelloWorld程序(四星斗师)
开发语言·qt
全栈开发圈1 小时前
新书速览|Java网络爬虫精解与实践
java·开发语言·爬虫
面试鸭1 小时前
离谱!买个人信息买到网安公司头上???
java·开发语言·职场和发展
小白学大数据1 小时前
JavaScript重定向对网络爬虫的影响及处理
开发语言·javascript·数据库·爬虫