00001 using System;
00002 using System.Collections.Generic;
00003 using System.ComponentModel;
00004 using System.Data;
00005 using System.Drawing;
00006 using System.Text;
00007 using System.Windows.Forms;
00008 using System.IO;
00009 using System.Diagnostics;
00010 using System.Runtime.InteropServices;
00011
00012 namespace VisionSystem {
00013 public partial class VisionSystemMainForm : Form {
00014
00015 public delegate void updateUiEventHandler();
00016 public event updateUiEventHandler updateUiEvent;
00017 public delegate void updateDataEventHandler();
00018 public event updateDataEventHandler updateDataEvent;
00019 public delegate void updateErrorEventHandler(Exception error);
00020 public event updateErrorEventHandler updateErrorEvent;
00021 public delegate void cameraEventHandler();
00022 public event cameraEventHandler stopCameraEvent;
00023 public event cameraEventHandler startCameraEvent;
00024 protected Stopwatch _watchdogTimer;
00025
00026 VisionData _data;
00027 public VisionData Data {
00028 get { return _data; }
00029 }
00030
00031 string _status = "";
00032 public string Status {
00033 get { return _status; }
00034 }
00035
00036 bool _silent = true;
00037 public bool Silent {
00038 get { return _silent; }
00039 set { _silent = value; }
00040 }
00041
00042 Bitmap _filtered;
00043
00044 public VisionSystemMainForm() {
00045 InitializeComponent();
00046 initializeObject(198, 46, 48);
00047 }
00048
00049 public VisionSystemMainForm(int red, int green, int blue) {
00050 InitializeComponent();
00051 initializeObject(red, green, blue);
00052 }
00053
00054 private void initializeObject(int red, int green, int blue) {
00055 _filtered = new Bitmap((int)cameraDsCapture.VideoSize.Width
00056 , (int)cameraDsCapture.VideoSize.Height);
00057 _data = new VisionData(ref _filtered);
00058 updateUiEvent += updateUI;
00059 startCameraEvent += startCamera;
00060 stopCameraEvent += stopCamera;
00061 rTextBox.Text = _data.Orange.R.ToString();
00062 gTextBox.Text = _data.Orange.G.ToString();
00063 bTextBox.Text = _data.Orange.B.ToString();
00064 rTTextBox.Text = _data.RedT.ToString();
00065 gTTextBox.Text = _data.GrnT.ToString();
00066 bTTextBox.Text = _data.BluT.ToString();
00067 setColor(Color.FromArgb(red, green, blue));
00068 _watchdogTimer = new Stopwatch();
00069 _watchdogTimer.Start();
00070 }
00071
00072 private void updateUI() {
00073 if (coneAquiredTextBox.InvokeRequired) {
00074 try {
00075 this.Invoke(updateUiEvent);
00076 } catch (Exception e) {
00077 if(updateErrorEvent != null) updateErrorEvent.Invoke(e);
00078 _status = e.Message;
00079 }
00080 } else {
00081 try {
00082 filteredConePictureBox.Image = _filtered;
00083 coneAquiredTextBox.Text = _data.ConeAquired.ToString();
00084 turnMagnitudeTextBox.Text = _data.TurnMagnitude.ToString();
00085 turnDierectionTextBox.Text = _data.TurnDirection;
00086 deltaTextBox.Text = _data.DeltaX.ToString();
00087 leftSlopeTextBox.Text = Math.Round(_data.LeftSlope, 2).ToString();
00088 rightSlopeTextBox.Text = Math.Round(_data.RightSlope, 2).ToString();
00089 scaleFactorTextBox.Text = _data.ScaleFactor.ToString();
00090 brightnessTextBox.Text = _data.Brightness.ToString();
00091 dataUpdateRateTextBox.Text = _watchdogTimer.ElapsedMilliseconds.ToString();
00092 } catch (Exception e) {
00093 if (updateErrorEvent != null)
00094 updateErrorEvent.Invoke(e);
00095 _status = e.Message;
00096 }
00097 }
00098 }
00099
00100 private void coneGenericFilter_ProcessData(object Sender
00101 , Mitov.VideoLab.VideoProcessDataEventArgs Args) {
00102 unfilteredConePictureBox.Image = Args.InBuffer.ToBitmap();
00103 _data.processData(Args.InBuffer);
00104
00105 if (!_silent) {
00106
00107 _data.drawOrange();
00108 updateUI();
00109 }
00110 if (updateDataEvent != null) updateDataEvent.Invoke();
00111 _watchdogTimer.Reset();
00112 _watchdogTimer.Start();
00113 }
00114
00115 private void saveOriginal_Click(object sender, EventArgs e) {
00116 saveRawImage();
00117 }
00118
00119 private void saveFilter_Click(object sender, EventArgs e) {
00120 savefilteredImage();
00121 }
00122
00123 public void saveRawImage() {
00124 try {
00125 string dt = DateTime.Now.ToString().Replace('/', '-');
00126 dt = dt.Replace(':', '-');
00127 toolStripStatusLabel2.Text = "saving raw image as a .bmp";
00128 unfilteredConePictureBox.Image.Save(".\\images\\" + dt + "_original.bmp");
00129 toolStripStatusLabel2.Text = "raw image saved";
00130 } catch (Exception e) {
00131 if (updateErrorEvent != null)
00132 updateErrorEvent.Invoke(e);
00133 }
00134 }
00135
00136 public void savefilteredImage() {
00137 try {
00138 string dt = DateTime.Now.ToString().Replace('/', '-');
00139 dt = dt.Replace(':', '-');
00140 toolStripStatusLabel2.Text = "saving filter image as a .bmp";
00141 _filtered.Save(".\\images\\" + dt + "_filter.bmp");
00142 toolStripStatusLabel2.Text = "filter image Saved";
00143 } catch (Exception e) {
00144 if (updateErrorEvent != null)
00145 updateErrorEvent.Invoke(e);
00146 }
00147 }
00148
00149 private void editFilterButton_Click(object sender, EventArgs e) {
00150 setFilter();
00151 }
00152
00153 private void setFilter() {
00154 try {
00155 _data.setFilter(Byte.Parse(rTTextBox.Text)
00156 , Byte.Parse(gTTextBox.Text)
00157 , Byte.Parse(bTTextBox.Text));
00158 toolStripStatusLabel2.Text = "changed cilter";
00159 } catch (Exception e) {
00160 if (updateErrorEvent != null)
00161 updateErrorEvent.Invoke(e);
00162 toolStripStatusLabel2.BackColor = Color.Red;
00163 toolStripStatusLabel2.Text
00164 = "non-numeric value entered, or entry out of range";
00165 }
00166 }
00167
00168 private void pickColorButton_Click(object sender, EventArgs e) {
00169 colorDialog1.ShowDialog();
00170 setColor(colorDialog1.Color);
00171 }
00172
00173 private void videoDeviceButton_Click(object sender, EventArgs e) {
00174 cameraDsCapture.Stop();
00175 cameraDsCapture.VideoCaptureDevice.ShowDeviceSelctDialog();
00176 toolStripStatusLabel2.Text = "video device selected";
00177 }
00178
00179 public void initializeCamera() {
00180 cameraDsCapture.Stop();
00181 cameraDsCapture.VideoCaptureDevice.DeviceName = "USB Video Device";
00182
00183 }
00184
00185 private void startDeviceButton_Click(object sender, EventArgs e) {
00186 startCamera();
00187 }
00188
00189 public void startCamera() {
00190 if (silentCheckBox.InvokeRequired) {
00191 this.Invoke(startCameraEvent);
00192 } else {
00193 if (!cameraDsCapture.Opened) {
00194 toolStripStatusLabel2.Text = "starting video device";
00195
00196
00197 cameraDsCapture.Start();
00198 toolStripStatusLabel2.Text = "video device started";
00199 toolStripStatusLabel1.BackColor = Color.Green;
00200 toolStripStatusLabel1.Text = "vis_init";
00201 }
00202 }
00203 }
00204
00205 public void stopDeviceButton_Click(object sender, EventArgs e) {
00206 stopCamera();
00207 }
00208
00209 public void stopCamera() {
00210 if (silentCheckBox.InvokeRequired) {
00211 this.Invoke(stopCameraEvent);
00212 } else {
00213 if (cameraDsCapture.Opened) {
00214 silentCheckBox.Checked = true;
00215 cameraDsCapture.Stop();
00216 cameraDsCapture.Close();
00217 toolStripStatusLabel2.Text = "video device stopped";
00218
00219 toolStripStatusLabel1.BackColor = Color.Red;
00220 toolStripStatusLabel1.Text = "not initialized";
00221 }
00222 }
00223 }
00224
00225 private void setColor(Color c) {
00226 _data.setColor(c);
00227 rTextBox.Text = _data.Orange.R.ToString();
00228 gTextBox.Text = _data.Orange.G.ToString();
00229 bTextBox.Text = _data.Orange.B.ToString();
00230 toolStripStatusLabel2.Text = "color changed";
00231 }
00232
00233 private void hideButton_Click(object sender, EventArgs e) {
00234 this.Hide();
00235 }
00236
00237 private void silentCheckBox_CheckedChanged(object sender, EventArgs e) {
00238 Silent = silentCheckBox.Checked;
00239 }
00240
00241 private void unfilteredConePictureBox_MouseDoubleClick
00242 (object sender, MouseEventArgs e) {
00243 setColor(((Bitmap)(unfilteredConePictureBox.Image)).GetPixel(e.X, e.Y));
00244 }
00245
00246 private void VisionSystemMainForm_FormClosing(object sender, FormClosingEventArgs e) {
00247 stopCamera();
00248 }
00249
00250 }
00251 }