private void ProcessFrame(object sender, EventArgs arg) { capVid.SetCaptureProperty((CAP_PROP)1, frameCount); frameCount += speedX; //DateTime t1 = DateTime.Now; //rawScreen.Image = capVid.RetrieveBgrFrame().Bitmap; //return; Image <Bgr, Byte> frame_cap;// = _capture.RetrieveBgrFrame(); frame_cap = capVid.RetrieveBgrFrame(); Image <Gray, byte> grayIm = new Image <Gray, byte>(frame_cap.Width, frame_cap.Height); grayIm = frame_cap.Convert <Gray, byte>(); Image <Gray, byte> binmask = new Image <Gray, byte>("bitMask.bmp"); Image <Gray, Byte> rodmask = new Image <Gray, byte>("nightmask.bmp"); Image <Gray, byte> result = new Image <Gray, byte>(frame_cap.Width, frame_cap.Height); Gray avgclr = rodmask.GetAverage(); int avg = (int)avgclr.Intensity; result = binmask.And(grayIm); result = result.Sub(rodmask); //Gray thr = new Gray(50); //result = result.ThresholdToZero(thr); result = result.Mul(5); result = result.SmoothBlur(15, 15); Gray thr = new Gray(80); Gray mx = new Gray(255); result = result.ThresholdBinary(thr, mx); for (int i = 0; i < 6; i++) { ln[i] = 0; //hasCar[i] = false; } for (int i = 313; i < 400; i++) { if (result.Data[row, i, 0] > 100) { ln[0]++; } } for (int i = 410; i < 490; i++) { if (result.Data[row, i, 0] > 100) { ln[1]++; } } for (int i = 510; i < 580; i++) { if (result.Data[row, i, 0] > 100) { ln[2]++; } } for (int i = 720; i < 800; i++) { if (result.Data[row, i, 0] > 100) { ln[3]++; } } for (int i = 830; i < 920; i++) { if (result.Data[row, i, 0] > 100) { ln[4]++; } } for (int i = 945; i < 1040; i++) { if (result.Data[row, i, 0] > 100) { ln[5]++; } } for (int i = 0; i < grayIm.Width; i++) { if ((i > 313 && i < 400) || (i > 410 && i < 490) || (i > 510 && i < 580) || (i > 720 && i < 800) || (i > 830 && i < 920) || (i > 945 && i < 1040)) { grayIm.Data[row, i, 0] = 0; result.Data[row, i, 0] = 255; } } //displayScreen.Image = result.ToBitmap(); rawScreen.Image = frame_cap.ToBitmap(); //displayScreen.Invalidate(); rawScreen.Invalidate(); //MessageBox.Show(""); for (int i = 0; i < 6; i++) { //if (!hasCar[i] && ln[i] > 20) //{ // cars++; // MessageBox.Show("car in lane " + (i + 1).ToString() + "\nTotal cars =" + cars.ToString() + "\nHTV = " + HTV.ToString() + "\nLTV = " + LTV.ToString()); // //txtCarCnt.Text = "Car Count = " + cars.ToString(); //} if (hasCar[i] && ln[i] < 20) { cars++; //MessageBox.Show("car in lane " + (i + 1).ToString() + "\nTotal cars =" + cars.ToString() + "\nHTV = " + HTV.ToString() + "\nLTV = " + LTV.ToString()); //txtCarCnt.Text = "Car Count = " + cars.ToString(); } if (ln[i] > 20) { lnsize[i] += ln[i]; hasCar[i] = true; } else { if (lnsize[i] > HTVSIZE) { HTV++; if (i >= 0 && i < 4) { inHTV++; CrossThreadOperation.Invoke(txtInHTV, () => { txtInHTV.Text = inHTV.ToString(); }); } else if (i > 3 && i < 6) { outHTV++; CrossThreadOperation.Invoke(txtOutHTV, () => { txtOutHTV.Text = outHTV.ToString(); }); } } else if (lnsize[i] < HTVSIZE && lnsize[i] > 0) { LTV++; if (i >= 0 && i < 3) { inLTV++; CrossThreadOperation.Invoke(txtInLTV, () => { txtInLTV.Text = inLTV.ToString(); }); } else if (i > 2 && i < 6) { outLTV++; CrossThreadOperation.Invoke(txtOutLTV, () => { txtOutLTV.Text = outLTV.ToString(); }); } } lnsize[i] = 0; hasCar[i] = false; } } flowTime = DateTime.Now; if (flowTime.Minute != preMin) { for (int i = 59; i > 0; i--) { inFlowCount[i] = inFlowCount[i - 1]; outFlowCount[i] = outFlowCount[i - 1]; } inFlowCount[0] = inHTV + inLTV - inFlowCount[1]; CrossThreadOperation.Invoke(txtInFlow, () => { txtInFlow.Text = inFlowCount.Sum().ToString(); }); outFlowCount[0] = outHTV + outLTV - outFlowCount[1]; CrossThreadOperation.Invoke(txtOutFlow, () => { txtOutFlow.Text = outFlowCount.Sum().ToString(); }); } preMin = flowTime.Minute; //DateTime t2 = DateTime.Now; //MessageBox.Show(t1.Millisecond.ToString() + "\n" + t2.Millisecond.ToString()); }
private void ProcessFrame(object sender, EventArgs arg) { capVid.SetCaptureProperty((CAP_PROP)1, frameCount); frameCount += speedX; //DateTime t1 = DateTime.Now; //rawScreen.Image = capVid.RetrieveBgrFrame().Bitmap; //return; Image <Bgr, Byte> frame_cap;// = _capture.RetrieveBgrFrame(); frame_cap = capVid.RetrieveBgrFrame(); Image <Gray, byte> grayIm = new Image <Gray, byte>(frame_cap.Width, frame_cap.Height); grayIm = frame_cap.Convert <Gray, byte>(); Image <Gray, byte> binmask = new Image <Gray, byte>(frame_cap.Width, frame_cap.Height); Image <Gray, byte> rodmask = new Image <Gray, byte>(frame_cap.Width, frame_cap.Height); if (DAY) { binmask = new Image <Gray, byte>("bitMask.bmp"); rodmask = new Image <Gray, byte>("maskRoad.bmp"); } else { binmask = new Image <Gray, byte>("bitMaskNight.bmp"); rodmask = new Image <Gray, byte>("nightmask.bmp"); } Image <Gray, byte> result = new Image <Gray, byte>(frame_cap.Width, frame_cap.Height); Gray avgclr = rodmask.GetAverage(); int avg = (int)avgclr.Intensity; result = binmask.And(grayIm); if (DAY) { result = result.Sub(rodmask);// +rodmask.Sub(result); } else { result = result.Sub(rodmask) + rodmask.Sub(result); } //result = result.Sub(rodmask);//add this for night +rodmask.Sub(result); //for (int i = row-200; i < grayIm.Height; i++) //{ // for (int j = 0; j < grayIm.Width; j++) // { // int r = rodmask.Data[i, j, 0] - result.Data[i, j, 0]; // if (r > 20 || r < -20) // { // result.Data[i, j, 0] = 255; // } // else // { // result.Data[i, j, 0] = 0; // } // } //} //displayScreen.Image = grayIm.ToBitmap(); //rawScreen.Image = result.ToBitmap(); //return; //Gray thr = new Gray(50); //result = result.ThresholdToZero(thr); result = result.Mul(5); result = result.SmoothBlur(25, 15); Gray thr; if (DAY) { thr = new Gray(80);//day threshhold 80 } else { thr = new Gray(60);//night threshhold 60 } //Gray thr = new Gray(80);//night threshhold 60 Gray mx = new Gray(255); result = result.ThresholdBinary(thr, mx); for (int i = 0; i < 6; i++) { ln[i] = 0; //hasCar[i] = false; } for (int i = 210; i < 350; i++) { if (result.Data[row, i, 0] > 100) { ln[0]++; } } for (int i = 350; i < 420; i++) { if (result.Data[row, i, 0] > 100) { ln[1]++; } } for (int i = 420; i < 570; i++) { if (result.Data[row, i, 0] > 100) { ln[2]++; } } //for (int i = 720; i < 800; i++) //{ // if (result.Data[row, i, 0] > 100) // { // ln[3]++; // } //} //for (int i = 830; i < 920; i++) //{ // if (result.Data[row, i, 0] > 100) // { // ln[4]++; // } //} //for (int i = 945; i < 1040; i++) //{ // if (result.Data[row, i, 0] > 100) // { // ln[5]++; // } //} //for night for (int i = 750; i < 870; i++) { if (result.Data[row, i, 0] > 100) { ln[3]++; } } for (int i = 920; i < 1070; i++) { if (result.Data[row, i, 0] > 100) { ln[4]++; } } for (int i = 1130; i < 1220; i++) { if (result.Data[row, i, 0] > 100) { ln[5]++; } } //for (int i = 0; i < grayIm.Width; i++) //{ // if ((i > 313 && i < 400) || (i > 410 && i < 490) || (i > 510 && i < 580) || (i > 720 && i < 800) || (i > 830 && i < 920) || (i > 945 && i < 1040)) // { // grayIm.Data[row, i, 0] = 0; // result.Data[row, i, 0] = 255; // } //} //for night for (int i = 0; i < grayIm.Width; i++) { if ((i > 210 && i < 350) || (i > 350 && i < 420) || (i > 420 && i < 570) || (i > 750 && i < 870) || (i > 920 && i < 1070) || (i > 1130 && i < 1220)) { grayIm.Data[row, i, 0] = 0; result.Data[row, i, 0] = 255; } } if (!(prv.Equals(grayIm.CopyBlank()))) { //subtr = subtr.Add(prv.Sub(grayIm)); subtr = prv.Sub(grayIm.And(binmask)); } subtr = subtr.ThresholdBinary(new Gray(50), new Gray(255)); prv = grayIm; //displayScreen.Image = frame_cap.ToBitmap(); CrossThreadOperation.Invoke(rawScreen, () => { rawScreen.Image = frame_cap.ToBitmap(); }); //displayScreen.Invalidate(); //rawScreen.Invalidate(); //MessageBox.Show(""); for (int i = 0; i < 6; i++) { //if (!hasCar[i] && ln[i] > 20) //{ // cars++; // MessageBox.Show("car in lane " + (i + 1).ToString() + "\nTotal cars =" + cars.ToString() + "\nHTV = " + HTV.ToString() + "\nLTV = " + LTV.ToString()); // //txtCarCnt.Text = "Car Count = " + cars.ToString(); //} if (hasCar[i] && ln[i] < isCAR) { cars++; //MessageBox.Show("car in lane " + (i + 1).ToString() + "\nTotal cars =" + cars.ToString() + "\nHTV = " + HTV.ToString() + "\nLTV = " + LTV.ToString()); //txtCarCnt.Text = "Car Count = " + cars.ToString(); } if (ln[i] > isCAR) { lnsize[i] += ln[i]; hasCar[i] = true; } else { if (lnsize[i] > HTVSIZE) { HTV++; if (i >= 0 && i < 3) { inHTV++; CrossThreadOperation.Invoke(txtInHTV, () => { txtInHTV.Text = inHTV.ToString(); }); } else if (i > 2 && i < 6) { outHTV++; CrossThreadOperation.Invoke(txtOutHTV, () => { txtOutHTV.Text = outHTV.ToString(); }); } } else if (lnsize[i] < HTVSIZE && lnsize[i] > isSumCAR) { LTV++; if (i >= 0 && i < 3) { inLTV++; CrossThreadOperation.Invoke(txtInLTV, () => { txtInLTV.Text = inLTV.ToString(); }); } else if (i > 2 && i < 6) { outLTV++; CrossThreadOperation.Invoke(txtOutLTV, () => { txtOutLTV.Text = outLTV.ToString(); }); } } lnsize[i] = 0; hasCar[i] = false; } } if (outHTV % 5 == 0 && outHTV > 0) { if (!alert_sent) { //MessageBox.Show("sent"); CrossThreadOperation.Invoke(textBox_alarm, () => { textBox_alarm.Text = "HTV Alarm Generated"; }); analysis_event_handler.SendMessage(VideoAnalyticsEvents.EventType.HTV_ALARM, "5 HTVs Gone Towards Tunnel"); alert_sent = true; } } else { CrossThreadOperation.Invoke(textBox_alarm, () => { textBox_alarm.Text = ""; }); alert_sent = false; } flowTime = DateTime.Now; if (flowTime.Minute != preMin) { for (int i = 59; i > 0; i--) { inFlowCount[i] = inFlowCount[i - 1]; outFlowCount[i] = outFlowCount[i - 1]; } inFlowCount[0] = inHTV + inLTV - inFlowCount[1]; CrossThreadOperation.Invoke(txtInFlow, () => { txtInFlow.Text = inFlowCount.Sum().ToString(); }); outFlowCount[0] = outHTV + outLTV - outFlowCount[1]; CrossThreadOperation.Invoke(txtOutFlow, () => { txtOutFlow.Text = outFlowCount.Sum().ToString(); }); } preMin = flowTime.Minute; //DateTime t2 = DateTime.Now; //MessageBox.Show(t1.Millisecond.ToString() + "\n" + t2.Millisecond.ToString()); if (capVid.GetCaptureProperty((CAP_PROP)1) >= 1100) { //capVid.SetCaptureProperty((CAP_PROP)1, 0); frameCount = 200; } }