Exemplo n.º 1
0
        void TrackThreadRun(AutoResetEvent stop, IExperiment experiment)
        {
            //Set up image for small region around fish
            Image8           fishImage = new Image8(60, 60);
            CameraLinkCamera camera    = null;

            try
            {
                camera = new CameraLinkCamera(Properties.Settings.Default.CameraInterface);
                using (Image8 image = new Image8(camera.Width, camera.Height))
                {
                    if (experiment != null && experiment.SuggestedBufferSeconds != null)
                    {
                        int buffsize = (int)(experiment.SuggestedBufferSeconds.Value * FrameRate);
                        camera.Start(buffsize > 2 ? buffsize : 2);
                    }
                    else
                    {
                        camera.Start(100);//start camera with 100 frames in buffer by default
                    }
                    while (!stop.WaitOne(0))
                    {
                        try
                        {
                            FrameIndex = camera.Extract(image, FrameIndex) + 1;
                            IppiPoint?fishCentroid = null;
                            if (experiment != null)
                            {
                                if (!experiment.ProcessNext((int)FrameIndex, image, out fishCentroid))
                                {
                                    break;
                                }
                            }

                            //at 10Hz display camera and fish image
                            if (FrameIndex % (FrameRate / 10) == 0)
                            {
                                //blank the fish image
                                ip.ippiSet_8u_C1R(0, fishImage.Image, fishImage.Stride, fishImage.Size);
                                if (experiment is PreviewTrack && DisplayImage != ImageType.Camera)
                                {
                                    Image8       toHandle = null;
                                    PreviewTrack pt       = experiment as PreviewTrack;
                                    switch (DisplayImage)
                                    {
                                    case ImageType.Background:
                                        toHandle       = pt.Background;
                                        MainImage.CMax = 255;
                                        FishImage.CMax = 255;
                                        break;

                                    case ImageType.Foreground:
                                        toHandle       = pt.Foreground;
                                        MainImage.CMax = 255;
                                        FishImage.CMax = 255;
                                        break;

                                    default:
                                        toHandle       = pt.Thresholded;
                                        MainImage.CMax = 1;
                                        FishImage.CMax = 1;
                                        break;
                                    }
                                    if (toHandle != null)
                                    {
                                        if (fishCentroid != null)
                                        {
                                            CopyRegionImage(fishCentroid.Value, fishImage, toHandle);
                                        }
                                        MainImage.Write(toHandle, stop);
                                        FishImage.Write(fishImage, stop);
                                    }
                                }
                                else
                                {
                                    //In preview we also apply our mask
                                    if (experiment is PreviewTrack)
                                    {
                                        if (_maskImage == null || _maskImage.Width != image.Width || _maskImage.Height != image.Height)
                                        {
                                            if (_maskImage != null)
                                            {
                                                _maskImage.Dispose();
                                            }
                                            _maskImage = new Image8(image.Width, image.Height);
                                            DishCenter = new IppiPoint(_maskImage.Width / 2, _maskImage.Height / 2);
                                            CreateCircularMask(_maskImage, DishCenter, MaskRadius);
                                        }
                                        DrawMask(_maskImage, image);
                                    }

                                    if (fishCentroid != null)
                                    {
                                        CopyRegionImage(fishCentroid.Value, fishImage, image);
                                    }
                                    MainImage.CMax = 255;
                                    FishImage.CMax = 255;
                                    MainImage.Write(image, stop);
                                    FishImage.Write(fishImage, stop);
                                }
                            }
                        }
                        catch (OperationCanceledException)
                        {
                            break;
                        }
                    }
                }
            }
            catch (NIImaqException camException)
            {
                System.Diagnostics.Debug.WriteLine(camException);
            }
            finally
            {
                if (experiment != null && experiment is IDisposable)
                {
                    (experiment as IDisposable).Dispose();
                }
                if (camera != null)
                {
                    camera.Dispose();
                }
                if (fishImage != null)
                {
                    fishImage.Dispose();
                }
                if (IsRunning)
                {
                    DispatcherHelper.CheckBeginInvokeOnUI(() => { Stop(); });
                }
            }
        }