Reputation: 83
I'm creating a program which will get the width and length of the rectangle drawn on a whole body picture of the user. I can't seem to get the right scaleFactor, minNeighbor and size. how or what should I do to get the right info's...
Here is my code:
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 Emgu.CV;
using Emgu.CV.Structure;
using Emgu.Util;
namespace fitting
{
public partial class Form1 : Form
{
HaarCascade UpperBody = new HaarCascade("haarcascade_mcs_upperbody.xml");
HaarCascade LowerBody = new HaarCascade("haarcascade_lowerbody.xml");
Capture camera;
bool captureProcess = false;
Image<Bgr, Byte> img;
public Form1()
{
InitializeComponent();
}
void viewImage(object sender, EventArgs e)
{
img = camera.QueryFrame();
if (img == null)
return;
CamImageBox.Image = img;
}
private void btnCapture_Click(object sender, EventArgs e)
{
if (captureProcess == true)
{
string data;
Application.Idle -= viewImage;
captureProcess = false;
SaveFileDialog dlg = new SaveFileDialog();
//dlg="Image|*.jpg;*png";
if (dlg.ShowDialog() == DialogResult.OK)
{
img.ToBitmap().Save(dlg.FileName + ".jpg", System.Drawing.Imaging.ImageFormat.Png);
data = dlg.FileName + ".jpg";
}
measureImage();
}
}
void measureImage()
{
OpenFileDialog dlg2 = new OpenFileDialog();
dlg2.Filter = "Image|*.jpg;*png";
if (dlg2.ShowDialog() == DialogResult.OK)
{
Image<Bgr, Byte> frame = new Image<Bgr, byte>(dlg2.FileName);
Image<Gray, Byte> Gray_Frame = frame.Convert<Gray, Byte>();
MCvAvgComp[][] LowerBodyDetect = Gray_Frame.DetectHaarCascade(
LowerBody,
1.985603925968,
0,
Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
new Size());
MCvAvgComp[][] UpperBodyDetect = Gray_Frame.DetectHaarCascade(
UpperBody,
1.3,
5,
Emgu.CV.CvEnum.HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
new Size());
//foreach (MCvAvgComp Upp_Body in UpperBodyDetect[0])
//{
// frame.Draw(Upp_Body.rect, new Bgr(Color.Red), 2);
// double width = (Upp_Body.rect.Width * 0.264583333);
// textBox1.Text = (Convert.ToString(width));
//}
try
{
frame.Draw(UpperBodyDetect[0][0].rect, new Bgr(Color.Red), 2);
double width = (UpperBodyDetect[0][0].rect.Width);
textBox1.Text = (Convert.ToString(width));
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
//foreach (MCvAvgComp Low_Body in LowerBodyDetect[0])
//{
// frame.Draw(Low_Body.rect, new Bgr(Color.Green), 2);
//}
try
{
frame.Draw(LowerBodyDetect[0][0].rect, new Bgr(Color.Green), 2);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
CamImageBox.Image = frame;
}
}
private void Form1_Load(object sender, EventArgs e)
{
bool useCam = false;
if (!useCam)
measureImage();
else {
try
{
camera = new Capture();
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
return;
}
Application.Idle += viewImage;
captureProcess = true;
}
}
}
}
Upvotes: 0
Views: 1534
Reputation: 5250
There is no right parameter. You should select them for your problem. For that you should know what those parameters stand for. Let me help you little bit.
scaleFactor: There is a trade of for this parameter. If you select it bigger your detector work faster but detection rate will be less. It won't find upper or lower body in every scale if you select it bigger. So simply bigger scaleFactor -> faster detection, less detection rate. Smaller scaleFactor -> slower detection, higher detection rate.
minNeighbor: Basic definition is this, detector first detect candidate body regions, and then by applying some filtering on those candidate regions it selects body regions. Here, filtering is minimum neighbour count of each candidate region, so if you select the parameter 3 and you have 10 candidate regions, but non of them are neighbours (so to say overlapping), you won't get any detected bodies. If you select this parameter high it will find less bodies and select it low find more bodies. But not all detected regions will be correct. If you select it low, detector will probably find most of the bodies in the given image but it will also give lots of false detection. If you select it high false detection rate will be very small but it might miss some correct detections.
It is better you read the original documentation about this.
Hope it helps..
Upvotes: 3