Emgu下的HOG行人检测算法示例

时间:2022-05-31 20:48:17

采用opencv自带的svm分类器,演示采用HOG算法的行人检测

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.Structure;

namespace HOGtest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Image<Bgr, Byte> img = new Image<Bgr, Byte>("001.jpg");//测试图片
Emgu.CV.HOGDescriptor hog = new HOGDescriptor();//建立HOG描述子
hog.SetSVMDetector(HOGDescriptor.GetDefaultPeopleDetector());//设置分类器模型数据,这里是opencv默认的模型
MCvObjectDetection[] vec = hog.DetectMultiScale(img);//获得检测结果
Image<Bgr, Byte> ss = new Image<Bgr, Byte>(img.ToBitmap());//在新图像上表示结果
for (int i = 0; i < vec.GetLength(0); i++)
{
Rectangle r = vec[i].Rect;
MCvScalar s = new MCvScalar(0,0,0);
CvInvoke.Rectangle(ss,r,s);
}
pictureBox1.Image = ss.ToBitmap();
}
}
}