Exemplo n.º 1
0
        /// <summary>
        /// Process the next frame
        /// </summary>
        /// <param name="frameNumber">The frame index</param>
        /// <param name="camImage">The camera image</param>
        /// <param name="poi">The fish location</param>
        /// <returns>Whether experiment should continue or not</returns>
        public override bool ProcessNext(int frameNumber, Image8 camImage, out IppiPoint?poi)
        {
            base.ProcessNext(frameNumber, camImage, out poi);
            if (_scanner == null)
            {
                System.Diagnostics.Debug.WriteLine("Scanner was not initialized. Terminating experiment");
                return(false);
            }
            int writeEvery = Properties.Settings.Default.FrameRate * 4;

            if (frameNumber % writeEvery == writeEvery - 1)
            {
                _laser.LaserPower = Properties.Settings.Default.LaserCalibPowermW;
            }
            else
            {
                _laser.LaserPower = 0;
            }
            _lastFrame = frameNumber;
            if (frameNumber >= _totalFrames)
            {
                return(false);
            }
            BlobWithMoments fish = null;

            // Every 4s we turn on the laser - those frames won't be tracked but the image will be saved
            if (frameNumber % writeEvery != 0)
            {
                fish = Tracker.Track(camImage);
            }
            if (fish != null)
            {
                poi = new IppiPoint(fish.Centroid.x, fish.Centroid.y);
                if (_scanner != null)
                {
                    _scanner.Hit(poi.GetValueOrDefault());
                }
            }
            if (_trackWriter != null)
            {
                if (fish != null)
                {
                    _trackWriter.WriteLine("{0}\t{1}\t{2}\t{3}", frameNumber, fish.Centroid.x, fish.Centroid.y, fish.Angle);
                }
                else
                {
                    _trackWriter.WriteLine("NaN\tNaN\tNaN\tNaN");
                }
                // Write image of Laser On frames and the two surrounding frames on each side to file
                if (frameNumber % writeEvery <= 2 || frameNumber % writeEvery >= writeEvery - 2)
                {
                    _imageWriter.WriteFrame(camImage);
                }
            }
            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Process the next frame
        /// </summary>
        /// <param name="frameNumber">The frame index</param>
        /// <param name="camImage">The camera image</param>
        /// <param name="poi">The fish location</param>
        /// <returns>Whether experiment should continue or not</returns>
        public override bool ProcessNext(int frameNumber, Image8 camImage, out IppiPoint?poi)
        {
            base.ProcessNext(frameNumber, camImage, out poi);
            _lastFrame = frameNumber;
            if (frameNumber >= _totalFrames)
            {
                return(false);
            }
            var fish = Tracker.Track(camImage);

            if (fish != null)
            {
                poi = new IppiPoint(fish.Centroid.x, fish.Centroid.y);
            }
            if (_trackWriter != null)
            {
                if (fish != null)
                {
                    _trackWriter.WriteLine("{0}\t{1}\t{2}\t{3}", frameNumber, fish.Centroid.x, fish.Centroid.y, fish.Angle);
                    if (_writeFishImages)
                    {
                        //blank and copy
                        ip.ippiSet_8u_C1R(0, _camRegion.Image, _camRegion.Stride, _camRegion.Size);
                        MainViewModel.CopyRegionImage(fish.Centroid, _camRegion, camImage);
                        _imageWriter.WriteFrame(_camRegion);
                        MainViewModel.CopyRegionImage(fish.Centroid, _camRegion, Tracker.Background);
                        _backgroundWriter.WriteFrame(_camRegion);
                    }
                }
                else
                {
                    _trackWriter.WriteLine("NaN\tNaN\tNaN\tNaN");
                    if (_writeFishImages)
                    {
                        //blank
                        ip.ippiSet_8u_C1R(0, _camRegion.Image, _camRegion.Stride, _camRegion.Size);
                        _imageWriter.WriteFrame(_camRegion);
                        _backgroundWriter.WriteFrame(_camRegion);
                    }
                }
            }
            return(true);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Process the next frame
        /// </summary>
        /// <param name="frameNumber">The frame index</param>
        /// <param name="camImage">The camera image</param>
        /// <param name="poi">The fish location</param>
        /// <returns>Whether experiment should continue or not</returns>
        public override bool ProcessNext(int frameNumber, Image8 camImage, out IppiPoint?poi)
        {
            base.ProcessNext(frameNumber, camImage, out poi);
            _lastFrame = frameNumber;
            if (_originalType == OriginalType.Unknown)
            {
                //This should never happen...
                System.Diagnostics.Debug.WriteLine("Unknown original experiment type. Terminating experiment");
                return(false);
            }
            if (_scanner == null)
            {
                System.Diagnostics.Debug.WriteLine("Scanner was not initialized. Terminating experiment");
                return(false);
            }
            if (_experimentPhase == ExperimentPhase.Done)
            {
                return(false);
            }

            BlobWithMoments fish = null;

            fish = Tracker.Track(camImage);
            if (fish != null)
            {
                poi = new IppiPoint(fish.Centroid.x, fish.Centroid.y);
                try
                {
                    if (_scanner != null)
                    {
                        _scanner.Hit(poi.GetValueOrDefault());
                    }
                }
                catch (ArgumentOutOfRangeException)
                {
                    System.Diagnostics.Debug.WriteLine("Tried to hit coordinates outside scan table area.");
                    System.Diagnostics.Debug.WriteLine("Coordinates were: x={0}, y={1}", fish.xc, fish.yc);
                    System.Diagnostics.Debug.WriteLine("Terminating Experiment");
                    _laser.LaserPower = 0;
                    return(false);
                }
            }
            //Get the next data item
            string currentData = _originalTrackData.Dequeue();

            string[] currentItems = currentData.Split('\t');
            int      phase, trial;
            double   power = ProcessTrackItems(currentItems, out phase, out trial);

            if (double.IsNaN(power))
            {
                power = 0;
            }
            //Write track information
            WriteTrackInfo(frameNumber, fish, phase, trial);
            //Write images
            ip.ippiSet_8u_C1R(0, _camRegion.Image, _camRegion.Stride, _camRegion.Size);
            if (fish != null)
            {
                MainViewModel.CopyRegionImage(fish.Centroid, _camRegion, camImage);
            }
            _imageWriter.WriteFrame(_camRegion);
            if (fish != null)
            {
                MainViewModel.CopyRegionImage(fish.Centroid, _camRegion, Tracker.Background);
            }
            _backgroundWriter.WriteFrame(_camRegion);
            RunReplay(power);
            //If we have lost the fish, disengage the laser
            if (fish == null)
            {
                _laser.LaserPower = 0;
            }
            return(true);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Process the next frame
        /// </summary>
        /// <param name="frameNumber">The frame index</param>
        /// <param name="camImage">The camera image</param>
        /// <param name="poi">The fish location</param>
        /// <returns>Whether experiment should continue or not</returns>
        public override bool ProcessNext(int frameNumber, Image8 camImage, out IppiPoint?poi)
        {
            base.ProcessNext(frameNumber, camImage, out poi);
            _lastFrame++;
            if (_scanner == null)
            {
                System.Diagnostics.Debug.WriteLine("Scanner was not initialized. Terminating experiment");
                return(false);
            }
            if (_experimentPhase == ExperimentPhase.Done)
            {
                return(false);
            }

            BlobWithMoments fish = null;

            fish = Tracker.Track(camImage);
            if (fish != null)
            {
                poi = new IppiPoint(fish.Centroid.x, fish.Centroid.y);
                try
                {
                    if (_scanner != null)
                    {
                        _scanner.Hit(poi.GetValueOrDefault());
                    }
                }
                catch (ArgumentOutOfRangeException)
                {
                    System.Diagnostics.Debug.WriteLine("Tried to hit coordinates outside scan table area.");
                    System.Diagnostics.Debug.WriteLine("Coordinates were: x={0}, y={1}", fish.xc, fish.yc);
                    System.Diagnostics.Debug.WriteLine("Terminating Experiment");
                    _laser.LaserPower = 0;
                    return(false);
                }
            }
            //Write track information including current phase, current laser power
            WriteTrackInfo(frameNumber, fish);
            //Write images
            ip.ippiSet_8u_C1R(0, _camRegion.Image, _camRegion.Stride, _camRegion.Size);
            if (fish != null)
            {
                MainViewModel.CopyRegionImage(fish.Centroid, _camRegion, camImage);
            }
            _imageWriter.WriteFrame(_camRegion);
            if (fish != null)
            {
                MainViewModel.CopyRegionImage(fish.Centroid, _camRegion, Tracker.Background);
            }
            _backgroundWriter.WriteFrame(_camRegion);
            //Depending on phase call process method to control laser power and switch phase if appropriate
            switch (_experimentPhase)
            {
            case ExperimentPhase.Habituation:
                RunHabituation();
                break;

            case ExperimentPhase.Pre:
                RunPrePhase();
                break;

            case ExperimentPhase.Gradient:
                RunGradient(fish);
                break;
            }
            //If we have lost the fish, disengage the laser
            if (fish == null)
            {
                _laser.LaserPower = 0;
            }
            return(true);
        }