/// <summary> /// Hog训练 /// </summary> /// <param name="posdir">正样本文件夹路径</param> /// <param name="negdir">负样本文件夹路径</param> private void HogTrain(string posdir, string negdir) { Bitmap bmp = null; // 训练负样本 string[] files = Directory.GetFiles(negdir); foreach (string file in files) { bmp = AForge.Imaging.Image.FromFile(file); pri_tld.TrainNegative(bmp); } // 训练正样本 files = Directory.GetFiles(posdir); foreach (string file in files) { bmp = AForge.Imaging.Image.FromFile(file); pri_tld.TrainPositive(bmp); } }
private void btnOk_Click(object sender, EventArgs e) { Bitmap patch = null; if (pri_tld != null && pri_bmp != null && pri_choose_rect != Rectangle.Empty) { for (double lrshift = (-1) * Parameter.SHIFT_BORDER; lrshift < Parameter.SHIFT_BORDER + Parameter.SHIFT_INTERVAL; lrshift += Parameter.SHIFT_INTERVAL) { for (double tbshift = (-1) * Parameter.SHIFT_BORDER; tbshift < Parameter.SHIFT_BORDER + Parameter.SHIFT_INTERVAL; tbshift += Parameter.SHIFT_INTERVAL) { if (pri_choose_rect.X + lrshift >= 0 && pri_choose_rect.X + pri_choose_rect.Width - 1 + lrshift < pri_bmp.Width - 1 && pri_choose_rect.Y + tbshift >= 0 && pri_choose_rect.Y + pri_choose_rect.Height - 1 + tbshift < pri_bmp.Height - 1) { patch = ImgOper.CutImage(pri_bmp, (int)(pri_choose_rect.X + lrshift), (int)(pri_choose_rect.Y + tbshift), (int)pri_choose_rect.Width, (int)pri_choose_rect.Height); pri_tld.TrainPositive(patch); //patch.Save("Image\\VideoSave\\" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg"); } } } for (int row = 0; row < pri_bmp.Height - Parameter.DETECT_WINDOW_SIZE.Height + 1; row += Parameter.DETECT_WINDOW_SIZE.Height) { for (int col = 0; col < pri_bmp.Width - Parameter.DETECT_WINDOW_SIZE.Width + 1; col += Parameter.DETECT_WINDOW_SIZE.Width) { Rectangle rect = new Rectangle(col, row, pri_choose_rect.Width, pri_choose_rect.Height); double areaportion = pri_tld.AreaProportion(rect, pri_choose_rect); if (areaportion < Parameter.AREA_INTERSECT_PROPORTION) { patch = ImgOper.CutImage(pri_bmp, rect.X, rect.Y, rect.Width, rect.Height); pri_tld.TrainNegative(patch); } } } } pri_player.Start(); this.Close(); }