PageRenderTime 26ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/BlobTracker/BlobTrackerTypes.cs

#
C# | 508 lines | 318 code | 48 blank | 142 comment | 11 complexity | e47ab76eb45909400e1fb283d255d8aa MD5 | raw file
  1. //-----------------------------------------------------------------------
  2. // This file is part of Microsoft Robotics Developer Studio Code Samples.
  3. //
  4. // Copyright (C) Microsoft Corporation. All rights reserved.
  5. //
  6. // $File: BlobTrackerTypes.cs $ $Revision: 1 $
  7. //-----------------------------------------------------------------------
  8. using Microsoft.Ccr.Core;
  9. using Microsoft.Dss.Core.Attributes;
  10. using Microsoft.Dss.ServiceModel.Dssp;
  11. using System;
  12. using System.Collections.Generic;
  13. using W3C.Soap;
  14. using blobtracker = Microsoft.Robotics.Services.Sample.BlobTracker;
  15. using System.Drawing;
  16. using System.ComponentModel;
  17. namespace Microsoft.Robotics.Services.Sample.BlobTracker
  18. {
  19. /// <summary>
  20. /// BlobTracker Contract class
  21. /// </summary>
  22. public sealed class Contract
  23. {
  24. /// <summary>
  25. /// The Dss Service contract
  26. /// </summary>
  27. public const String Identifier = "http://schemas.microsoft.com/robotics/2007/03/blobtracker.user.html";
  28. }
  29. /// <summary>
  30. /// The BlobTracker State
  31. /// </summary>
  32. [DataContract]
  33. [Description ("The blob tracker's state")]
  34. public class BlobTrackerState
  35. {
  36. /// <summary>
  37. /// Indicates whether an update frame is available.
  38. /// </summary>
  39. [Browsable(false)]
  40. public bool UpdateFrame
  41. {
  42. get { return _updateFrame; }
  43. set { _updateFrame = value; }
  44. }
  45. private bool _updateFrame;
  46. /// <summary>
  47. /// The set of color bins
  48. /// </summary>
  49. [DataMember(IsRequired = true)]
  50. public List<ColorBin> ColorBins
  51. {
  52. get { return _colorBins; }
  53. set { _colorBins = value; }
  54. }
  55. private List<ColorBin> _colorBins = new List<ColorBin>();
  56. /// <summary>
  57. /// TimeStamp of the last update
  58. /// </summary>
  59. [DataMember]
  60. [Browsable(false)]
  61. public DateTime TimeStamp
  62. {
  63. get { return _timeStamp; }
  64. set { _timeStamp = value; }
  65. }
  66. private DateTime _timeStamp;
  67. /// <summary>
  68. /// The list of matching blobs found
  69. /// </summary>
  70. [DataMember(IsRequired = true)]
  71. [Browsable(false)]
  72. public List<FoundBlob> Results
  73. {
  74. get { return _results; }
  75. set { _results = value; }
  76. }
  77. private List<FoundBlob> _results = new List<FoundBlob>();
  78. }
  79. /// <summary>
  80. /// Specifies a color bin (set)
  81. /// </summary>
  82. [DataContract]
  83. public class ColorBin
  84. {
  85. /// <summary>
  86. /// Indicates the name of the color bin (set).
  87. /// </summary>
  88. [DataMember]
  89. public string Name
  90. {
  91. get { return _name; }
  92. set { _name = value; }
  93. }
  94. private string _name;
  95. /// <summary>
  96. /// Indicates minimum red value of the color bin.
  97. /// </summary>
  98. [DataMember]
  99. public int RedMin
  100. {
  101. get { return _redMin; }
  102. set { _redMin = value; }
  103. }
  104. private int _redMin;
  105. /// <summary>
  106. /// Indicates maximum red value of the color bin.
  107. /// </summary>
  108. [DataMember]
  109. public int RedMax
  110. {
  111. get { return _redMax; }
  112. set { _redMax = value; }
  113. }
  114. private int _redMax;
  115. /// <summary>
  116. /// Indicates minimum green value of the color bin.
  117. /// </summary>
  118. [DataMember]
  119. public int GreenMin
  120. {
  121. get { return _greenMin; }
  122. set { _greenMin = value; }
  123. }
  124. private int _greenMin;
  125. /// <summary>
  126. /// Indicates maximum green value of the color bin.
  127. /// </summary>
  128. [DataMember]
  129. public int GreenMax
  130. {
  131. get { return _greenMax; }
  132. set { _greenMax = value; }
  133. }
  134. private int _greenMax;
  135. /// <summary>
  136. /// Indicates minimum blue value of the color bin.
  137. /// </summary>
  138. [DataMember]
  139. public int BlueMin
  140. {
  141. get { return _blueMin; }
  142. set { _blueMin = value; }
  143. }
  144. private int _blueMin;
  145. /// <summary>
  146. /// Indicates maximum blue value of the color bin.
  147. /// </summary>
  148. [DataMember]
  149. public int BlueMax
  150. {
  151. get { return _blueMax; }
  152. set { _blueMax = value; }
  153. }
  154. private int _blueMax;
  155. /// <summary>
  156. /// Checks if the specified color matches the current color bin.
  157. /// </summary>
  158. /// <param name="color">Color object</param>
  159. /// <returns>True if the color matches, false otherwise</returns>
  160. public bool Test(Color color)
  161. {
  162. return Test(color.R, color.G, color.B);
  163. }
  164. internal bool Test(int red, int green, int blue)
  165. {
  166. return (red >= _redMin && red < _redMax &&
  167. green >= _greenMin && green < _greenMax &&
  168. blue >= _blueMin && blue < _blueMax);
  169. }
  170. }
  171. /// <summary>
  172. /// Specifies information about the detected blob.
  173. /// </summary>
  174. [DataContract]
  175. public class FoundBlob
  176. {
  177. /// <summary>
  178. /// Indicates the X projection.
  179. /// </summary>
  180. [DataMember]
  181. public int[] XProjection
  182. {
  183. get { return _xProjection; }
  184. set { _xProjection = value; }
  185. }
  186. private int[] _xProjection;
  187. /// <summary>
  188. /// Indicates the Y projection.
  189. /// </summary>
  190. [DataMember]
  191. public int[] YProjection
  192. {
  193. get { return _yProjection; }
  194. set { _yProjection = value; }
  195. }
  196. private int[] _yProjection;
  197. /// <summary>
  198. /// Indicates the name of the blob.
  199. /// </summary>
  200. [DataMember]
  201. public string Name
  202. {
  203. get { return _name; }
  204. set { _name = value; }
  205. }
  206. private string _name;
  207. /// <summary>
  208. /// Indicates the X mean value.
  209. /// </summary>
  210. [DataMember]
  211. public double MeanX
  212. {
  213. get { return _meanX; }
  214. set { _meanX = value; }
  215. }
  216. private double _meanX;
  217. /// <summary>
  218. /// Indicates the Y mean value.
  219. /// </summary>
  220. [DataMember]
  221. public double MeanY
  222. {
  223. get { return _meanY; }
  224. set { _meanY = value; }
  225. }
  226. private double _meanY;
  227. /// <summary>
  228. /// Indicates the X standard deviation value.
  229. /// </summary>
  230. [DataMember]
  231. public double StdDevX
  232. {
  233. get { return _stdDevX; }
  234. set { _stdDevX = value; }
  235. }
  236. private double _stdDevX;
  237. /// <summary>
  238. /// Indicates the Y standard deviation value.
  239. /// </summary>
  240. [DataMember]
  241. public double StdDevY
  242. {
  243. get { return _stdDevY; }
  244. set { _stdDevY = value; }
  245. }
  246. private double _stdDevY;
  247. /// <summary>
  248. /// Indicates the X skew value.
  249. /// </summary>
  250. [DataMember]
  251. public double SkewX
  252. {
  253. get { return _skewX; }
  254. set { _skewX = value; }
  255. }
  256. private double _skewX;
  257. /// <summary>
  258. /// Indicates the Y skew value.
  259. /// </summary>
  260. [DataMember]
  261. public double SkewY
  262. {
  263. get { return _skewY; }
  264. set { _skewY = value; }
  265. }
  266. private double _skewY;
  267. /// <summary>
  268. /// Indicates area. This is the number of pixels that contribute to the blob.
  269. /// </summary>
  270. [DataMember]
  271. public double Area
  272. {
  273. get { return _area; }
  274. set { _area = value; }
  275. }
  276. private double _area;
  277. internal void CalculateMoments()
  278. {
  279. double square;
  280. double yOff = -_meanY;
  281. _stdDevY = 0.0;
  282. _skewY = 0.0;
  283. for (int y = 0; y < _yProjection.Length; y++, yOff++)
  284. {
  285. if (_yProjection[y] > 0)
  286. {
  287. square = yOff * yOff * _yProjection[y];
  288. _stdDevY += square;
  289. _skewY += yOff * square;
  290. }
  291. }
  292. _stdDevY = Math.Sqrt(_stdDevY / _area);
  293. _skewY = _skewY / (_area * _stdDevY * _stdDevY * _stdDevY);
  294. double xOff = -_meanX;
  295. _stdDevX = 0.0;
  296. _skewX = 0.0;
  297. for (int x = 0; x < _xProjection.Length; x++, xOff++)
  298. {
  299. if (_xProjection[x] > 0)
  300. {
  301. square = xOff * xOff * _xProjection[x];
  302. _stdDevX += square;
  303. _skewX += xOff * square;
  304. }
  305. }
  306. _stdDevX = Math.Sqrt(_stdDevX / _area);
  307. _skewX = _skewX / (_area * _stdDevX * _stdDevX * _stdDevX);
  308. }
  309. internal void AddPixel(int x, int y)
  310. {
  311. _meanX += x;
  312. _meanY += y;
  313. _xProjection[x]++;
  314. _yProjection[y]++;
  315. _area++;
  316. }
  317. }
  318. /// <summary>
  319. /// Indicates the ImageProcessed request.
  320. /// </summary>
  321. [DataContract]
  322. public class ImageProcessedRequest
  323. {
  324. /// <summary>
  325. /// Indicates the time the image was processed.
  326. /// </summary>
  327. [DataMember]
  328. public DateTime TimeStamp
  329. {
  330. get { return _timeStamp; }
  331. set { _timeStamp = value; }
  332. }
  333. private DateTime _timeStamp;
  334. /// <summary>
  335. /// Indicates the list of blobs found.
  336. /// </summary>
  337. [DataMember(IsRequired = true)]
  338. public List<FoundBlob> Results
  339. {
  340. get { return _results; }
  341. set { _results = value; }
  342. }
  343. private List<FoundBlob> _results = new List<FoundBlob>();
  344. }
  345. /// <summary>
  346. /// BlobTracker Main Operations Port
  347. /// </summary>
  348. [ServicePort]
  349. public class BlobTrackerOperations : PortSet
  350. {
  351. /// <summary>
  352. /// BlobTracker Operations PortSet
  353. /// </summary>
  354. public BlobTrackerOperations()
  355. : base(
  356. typeof(DsspDefaultLookup),
  357. typeof(DsspDefaultDrop),
  358. typeof(Get),
  359. typeof(ImageProcessed),
  360. typeof(Subscribe),
  361. typeof(InsertBin),
  362. typeof(DeleteBin),
  363. typeof(UpdateBin)
  364. )
  365. {
  366. }
  367. /// <summary>
  368. /// Implicit cast from BlobTrackerOperations to Port&lt;ImageProcessed&gt;
  369. /// </summary>
  370. public static implicit operator Port<ImageProcessed>(BlobTrackerOperations portSet)
  371. {
  372. if (portSet == null)
  373. {
  374. return null;
  375. }
  376. return (Port<ImageProcessed>)portSet[typeof(ImageProcessed)];
  377. }
  378. /// <summary>
  379. /// Post helper method
  380. /// </summary>
  381. public void Post(ImageProcessed msg)
  382. {
  383. base.PostUnknownType(msg);
  384. }
  385. /// <summary>
  386. /// Implicit cast from BlobTrackerOperations to Port&lt;DsspDefaultDrop&gt;
  387. /// </summary>
  388. public static implicit operator Port<DsspDefaultDrop>(BlobTrackerOperations portSet)
  389. {
  390. if (portSet == null)
  391. {
  392. return null;
  393. }
  394. return (Port<DsspDefaultDrop>)portSet[typeof(DsspDefaultDrop)];
  395. }
  396. /// <summary>
  397. /// Post helper method
  398. /// </summary>
  399. public void Post(DsspDefaultDrop msg)
  400. {
  401. base.PostUnknownType(msg);
  402. }
  403. }
  404. /// <summary>
  405. /// BlobTracker Get Operation
  406. /// </summary>
  407. [Description("Gets the current state of the service.")]
  408. public class Get : Get<GetRequestType, PortSet<BlobTrackerState, Fault>>
  409. {
  410. }
  411. /// <summary>
  412. /// BlobTracker Subscribe Operation
  413. /// </summary>
  414. [Description("Subscribes to the service notifications.")]
  415. public class Subscribe : Subscribe<SubscribeRequestType, PortSet<SubscribeResponseType, Fault>>
  416. {
  417. }
  418. /// <summary>
  419. /// BlobTracker Update Operation
  420. /// </summary>
  421. [Description("Indicates when an image has been processed.")]
  422. public class ImageProcessed : Update<ImageProcessedRequest, PortSet<DefaultUpdateResponseType, Fault>>
  423. {
  424. /// <summary>
  425. /// Default constructor
  426. /// </summary>
  427. public ImageProcessed()
  428. {
  429. }
  430. /// <summary>
  431. /// Creates a new ImageProcessed message with the specified request body
  432. /// </summary>
  433. /// <param name="body"></param>
  434. public ImageProcessed(ImageProcessedRequest body)
  435. : base(body)
  436. {
  437. }
  438. }
  439. /// <summary>
  440. /// BlobTracker InsertBin Operation
  441. /// </summary>
  442. [Description("Inserts a color bin for processing/analysis.")]
  443. public class InsertBin : Insert<ColorBin, PortSet<DefaultInsertResponseType, Fault>>
  444. {
  445. }
  446. /// <summary>
  447. /// BlobTracker DeleteBin Operation
  448. /// </summary>
  449. [Description("Deletes a color bin for processing/analysis.")]
  450. public class DeleteBin : Delete<ColorBin, PortSet<DefaultDeleteResponseType, Fault>>
  451. {
  452. }
  453. /// <summary>
  454. /// BlobTracker UpdateBin Operation
  455. /// </summary>
  456. [Description("Updates a color bin for processing/analysis.")]
  457. public class UpdateBin : Update<ColorBin, PortSet<DefaultUpdateResponseType, Fault>>
  458. {
  459. }
  460. }