IEnumerator <ITask> ProcessImage(List <ColorBin> bins)
        {
            Fault fault = null;

            cam.QueryFrameRequest request = new cam.QueryFrameRequest();
            request.Format = Guid.Empty;// new Guid("b96b3cae-0728-11d3-9d7b-0000f81ef32e");

            byte[]   frame     = null;
            DateTime timestamp = DateTime.MinValue;
            int      height    = 0;
            int      width     = 0;

            yield return(Arbiter.Choice(
                             _camPort.QueryFrame(request),
                             delegate(cam.QueryFrameResponse response)
            {
                timestamp = response.TimeStamp;
                frame = response.Frame;
                width = response.Size.Width;
                height = response.Size.Height;
            },
                             delegate(Fault f)
            {
                fault = f;
            }
                             ));

            ImageProcessedRequest processed = new ImageProcessedRequest();

            if (fault != null)
            {
                _mainPort.Post(new ImageProcessed(processed));
                yield break;
            }

            int size = width * height * 3;

            processed.TimeStamp = timestamp;
            List <FoundBlob> results = processed.Results;

            foreach (ColorBin bin in bins)
            {
                FoundBlob blob = new FoundBlob();

                blob.Name        = bin.Name;
                blob.XProjection = new int[width];
                blob.YProjection = new int[height];

                results.Add(blob);
            }

            int offset;

            for (int y = 0; y < height; y++)
            {
                offset = y * width * 3;

                for (int x = 0; x < width; x++, offset += 3)
                {
                    int r, g, b;

                    b = frame[offset];
                    g = frame[offset + 1];
                    r = frame[offset + 2];

                    for (int i = 0; i < bins.Count; i++)
                    {
                        ColorBin bin = bins[i];

                        if (bin.Test(r, g, b))
                        {
                            results[i].AddPixel(x, y);
                        }
                    }
                }
            }

            foreach (FoundBlob blob in results)
            {
                if (blob.Area > 0)
                {
                    blob.MeanX = blob.MeanX / blob.Area;
                    blob.MeanY = blob.MeanY / blob.Area;

                    blob.CalculateMoments();
                }
            }

            _mainPort.Post(new ImageProcessed(processed));
        }
Esempio n. 2
0
 /// <summary>
 /// Creates a new ImageProcessed message with the specified request body
 /// </summary>
 /// <param name="body"></param>
 public ImageProcessed(ImageProcessedRequest body)
     : base(body)
 {
 }