00001 using System;
00002 using System.Collections.Generic;
00003 using System.Linq;
00004 using System.Text;
00005 using System.Drawing;
00006
00007 namespace VisionSystem {
00008 public class VisionData {
00009
00010 Bitmap _filteredCone;
00011 public Bitmap FilteredCone {
00012 get { return _filteredCone; }
00013 }
00014
00015 Mitov.VideoLab.VideoBuffer _buffer;
00016 public Mitov.VideoLab.VideoBuffer Buffer {
00017 get { return _buffer; }
00018 }
00019 const byte red = 224, grn = 48, blu = 42;
00020
00021 byte _redT = 50;
00022 public byte BluT {
00023 get { return _bluT; }
00024 }
00025
00026 byte _grnT = 50;
00027 public byte GrnT {
00028 get { return _grnT; }
00029 }
00030
00031 byte _bluT = 50;
00032 public byte RedT {
00033 get { return _redT; }
00034 }
00035
00036 Point _top, _left, _right;
00037 public Point Right {
00038 get { return _right; }
00039 }
00040
00041 public Point Left {
00042 get { return _left; }
00043 }
00044
00045 public Point Top {
00046 get { return _top; }
00047 }
00048
00049
00050 public int DeltaX {
00051 get { return _right.X - _left.X; }
00052 }
00053
00054 Color _orange;
00055 public Color Orange {
00056 get { return _orange; }
00057 }
00058
00059 int _width = 320, _height = 240;
00060 int _orangePixelCount = 0;
00061
00062 string _status = "ok";
00063 public string Status {
00064 get { return _status; }
00065 }
00066
00067 Color[,] _orngPoints;
00068 public Color[,] OrngPoints {
00069 get { return _orngPoints; }
00070 }
00071
00072 int _scaleFactor = 0;
00073 public int ScaleFactor {
00074 get { return _scaleFactor; }
00075 }
00076
00077 double _leftSlope = 0;
00078 public double LeftSlope {
00079 get { return _leftSlope; }
00080 }
00081
00082 double _rightSlope = 0;
00083 public double RightSlope {
00084 get { return _rightSlope; }
00085 }
00086
00087 bool _coneAquired = false;
00088 public bool ConeAquired {
00089 get { return _coneAquired; }
00090 }
00091
00092 int _turnMagnitude = 0;
00093 public int TurnMagnitude {
00094 get { return _turnMagnitude; }
00095 }
00096
00097 string _turnDirection = "null";
00098 public string TurnDirection {
00099 get { return _turnDirection; }
00100 }
00101
00102 public VisionData(ref Bitmap filtered) {
00103 _filteredCone = filtered;
00104 _width = _filteredCone.Width;
00105 _height = _filteredCone.Height;
00106 _orngPoints = new Color[_width, _height];
00107 _orange = Color.FromArgb(red, grn, blu);
00108 _top = new Point(0, 0);
00109 _left = new Point(0, 0);
00110 _right = new Point(0, 0);
00111 }
00112
00113 public void processData(Mitov.VideoLab.VideoBuffer buffer) {
00114 _buffer = buffer;
00115 lookForOrange();
00116 if (_orangePixelCount > 50) {
00117 determineIsCone();
00118 if (_coneAquired) {
00119 _turnMagnitude = _width / 2 - _top.X;
00120 if (_width / 2 - _top.X > 0) _turnDirection = "left";
00121 else if (_width / 2 - _top.X < 0) _turnDirection = "right";
00122 else _turnDirection = "null";
00123 }
00124 } else _coneAquired = false;
00125 }
00126
00127 public void lookForOrange() {
00128 _orngPoints = new Color[_width, _height];
00129 _orangePixelCount = 0;
00130 for (int j = 0; j < _buffer.GetHeight(); ++j) {
00131 for (int i = 0; i < _buffer.GetWidth(); ++i) {
00132 if (Math.Abs(_buffer.GetRed(i, j) - _orange.R) < _redT
00133 && Math.Abs(_buffer.GetGreen(i, j) - _orange.G) < _grnT
00134 && Math.Abs(_buffer.GetBlue(i, j) - _orange.B) < _bluT) {
00135
00136 _orngPoints[i, j] = Color.FromArgb(_buffer.GetRed(i, j),
00137 _buffer.GetGreen(i, j), _buffer.GetBlue(i, j));
00138 ++_orangePixelCount;
00139 }
00140 }
00141 }
00142 }
00143
00144
00145 Graphics g;
00146 SolidBrush sBrush = new SolidBrush(Color.White);
00147 Pen pen = new Pen(Color.Green, 2);
00148
00149 public void drawOrange() {
00150 g = Graphics.FromImage(_filteredCone);
00151 g.FillRectangle(sBrush, 0, 0, _width, _height);
00152 for (int j = 0; j < _height; ++j) {
00153 for (int i = 0; i < _width; ++i) {
00154 if (!_orngPoints[i, j].IsEmpty)
00155 _filteredCone.SetPixel(i, j, _orngPoints[i, j]);
00156 }
00157 }
00158
00159 g.DrawLine(pen, _top, _left);
00160 g.DrawLine(pen, _top, _right);
00161 }
00162
00163
00164 bool isPointy;
00165
00166 private void determineIsCone() {
00167 _coneAquired = false;
00168
00169 _top.X = 0; _top.Y = 0;
00170 _left.X = 0; _left.Y = 0;
00171 _right.X = 0; _right.Y = 0;
00172
00173 for (int j = 0; j < _height; ++j) {
00174 for (int i = 0; i < _width; ++i) {
00175 if (!_orngPoints[i, j].IsEmpty) {
00176 _top.X = i; _top.Y = j + _scaleFactor; break;
00177 }
00178 }
00179 if (_top.X != 0 && _top.Y != 0) break;
00180 }
00181
00182 for (int j = _height - 1; j > 0; --j) {
00183 for (int i = 0; i < _width; ++i) {
00184 if (!_orngPoints[i, j].IsEmpty) {
00185 _left.Y = j - _scaleFactor; break;
00186 }
00187 }
00188 if (_left.Y != 0) break;
00189 }
00190
00191 for (int i = 0; i < _width; i++) {
00192 if (!_orngPoints[i, _left.Y].IsEmpty) {
00193 _left.X = i; break;
00194 }
00195 }
00196
00197 for (int i = _width - 1; i > 0; --i) {
00198 if (!_orngPoints[i, _left.Y].IsEmpty) {
00199 _right.X = i; _right.Y = _left.Y; break;
00200 }
00201 }
00202 _scaleFactor = (int)Math.Round(Math.Sqrt(Math.Pow(_left.X - _top.X, 2)
00203 + Math.Pow(_left.Y - _top.Y, 2)) / 10.0, 0);
00204 isPointy = true;
00205
00206 for (int i = _top.X + (_right.X - _left.X / 2); i < _width; i++) {
00207 int j = _top.X - i;
00208 if (!_orngPoints[i, _top.Y].IsEmpty) {
00209 isPointy = false; break;
00210 }
00211 if (_top.X - i > 0) {
00212 if (!_orngPoints[j, _top.Y].IsEmpty) {
00213 isPointy = false; break;
00214 }
00215 }
00216 } if (isPointy) {
00217 if (_left.X != _top.X)
00218 _leftSlope = (double)(_left.Y - _top.Y)
00219 / (double)(_left.X - _top.X);
00220 else _leftSlope = double.MaxValue;
00221 if (_right.X != _top.X)
00222 _rightSlope = (double)(_right.Y - _top.Y)
00223 / (double)(_right.X - _top.X);
00224 else _rightSlope = double.MaxValue;
00225 if (Math.Abs(_leftSlope + 5) < 3
00226 || Math.Abs(_rightSlope - 5) < 3)
00227 _coneAquired = true;
00228 }
00229 }
00230
00231 public void setColor(Color c) {
00232 _orange = c;
00233 }
00234
00235 public void setFilter(byte redT, byte grnT, byte bluT) {
00236 _redT = redT;
00237 _grnT = grnT;
00238 _bluT = bluT;
00239 }
00240
00241 int _brightness = 0;
00242 public int Brightness {
00243 get { return _brightness; }
00244 set { _brightness = value; }
00245 }
00246
00247 public void calibrateCamera(Mitov.VideoLab.VideoBuffer buffer) {
00248 int red = 0, green = 0, blue = 0;
00249 int pixelCount = buffer.GetHeight() * buffer.GetWidth();
00250 for (int j = 0; j < buffer.GetHeight(); ++j) {
00251 for (int i = 0; i < buffer.GetWidth(); ++i) {
00252 red += buffer.GetRed(i, j);
00253 green += buffer.GetGreen(i, j);
00254 blue += buffer.GetBlue(i, j);
00255 }
00256 }
00257 red /= pixelCount;
00258 green /= pixelCount;
00259 blue /= pixelCount;
00260 _brightness = (red + green + blue) / 3;
00261 }
00262
00263 }
00264 }