private void tmrCheckMotion_Tick(object sender, EventArgs e)
{
int N = 7;
int aperature_size = N;
//CVImage cv_out;
DateTime start = DateTime.Now;
//using (CVImage nextframe = cam.QueryFrame())
//{
//pbCurrent.Image = nextframe.ToBitmap();
//}
try
{
Image<Bgr, byte> frame = cam.QuerySmallFrame();
HiResFrame = frame.ToBitmap(); //cam.QueryFrame().ToBitmap();
pbCurrent.Image = frame.ToBitmap();
}
catch { return; }
tracker.SetFrameSize(pbCurrent.Width, pbCurrent.Height);
if (pbCurrent.Image == null || pbLast.Image == null)
return;
Color[] cols = {
Color.Blue,
Color.Green,
Color.Orange,
Color.Purple,
Color.Yellow,
Color.Violet,
Color.DarkGreen,
Color.DarkBlue,
Color.DarkMagenta,
Color.DarkSeaGreen
};
Bitmap blur = new Bitmap(pbLast.Image);
Bitmap __cur = new Bitmap(pbCurrent.Image);
if(Count % 25 == 0)
helper.MotionBlur(ref blur, ref __cur, 5);
Count++;
pbLast.Image = blur;
Bitmap cur_img = new Bitmap(pbCurrent.Image);
//if (frames[frames.Length - 1] != null)
//{
Bitmap bmp = new Bitmap(pbCurrent.Image);
detector.SetNextImages(pbLast.Image, pbCurrent.Image);
Target[] targs = new List<Target>(detector.GetTargets()).ToArray();
tracker.UpdateTargets(targs);
bmp = detector.motionpic;
string lable_data = "";
#region DrawTargets
LinkedList<ObjectTracked> objs = tracker.GetObjects();
lable_data += "Tracking " + objs.Count.ToString() + " objects\n";
int count = 0;
foreach (Target t in targs)
{
//helper.DrawBox(t, ref bmp, Color.Red);
//lable_data += "TARGET\n";
}
foreach (ObjectTracked obj in objs)
{
double a = 255 - (((DateTime.Now - obj.LastSeen)).TotalMilliseconds / tracker.UnseenRemovalLimit)*255 ;
a = Math.Min(255, Math.Max(0, a));
Color col;
col = cols[Math.Min(cols.Length - 1, obj.ID)];
if( (DateTime.Now - obj.LastSeen).TotalMilliseconds > 500) // hell, we are no longer activeley tracking this guy
col = Color.FromArgb((int)a, 255, 0, 0);
count++;
//lable_data += obj.ID.ToString() + " " + obj.Score.ToString() + "\n";
helper.DrawBox(obj, ref bmp, col);
//helper.DrawBox(obj.Position.X, obj.Position.Y, obj.Size.X, obj.Size.Y, ref bmp, col);
}
#endregion
/*
#region MoveServo
if (besttarget != null)
{
double per_x = ((double)besttarget.Position.X / (double)bmp.Width);
double per_y = ((double)besttarget.Position.Y / (double)bmp.Height);
double pitch = Scale(per_y, 0, 1, MinY, MaxY);
double yaw = Scale(per_x, 0, 1, MinX, MaxX);
SetServoPos((int)yaw, (int)pitch);
}
#endregion
*/
double ms = ( DateTime.Now - start ).TotalMilliseconds;
lblData.Text = "Frametime: " + ms.ToString() + " (" + ( Math.Round( 1000 / ms ) ).ToString() + ") " + lable_data + " Ammount of noise: " + detector.BadTargets.ToString();
pbMotion.Image = bmp;
AvgMisses[AvgMisses_pos] = detector.BadTargets;
AvgMisses_pos = (AvgMisses_pos + 1) % AvgMisses.Length;
float avg = 0f;
if (cbAuto.Checked && tracker.ObjectsTracked.Count == 0)
{
foreach (int value in AvgMisses)
avg += (float)value;
avg /= (float)AvgMisses.Length;
if (avg == 0)
detector.Difference--;
else if (avg > 0.15)
detector.Difference++;
tbDifference.Value = Math.Max(1, Math.Min(159, detector.Difference));
lblData.Text += " Avg Noise: " + avg.ToString("0.00");
}
}