Пример #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);
        }
Пример #2
0
 /// <summary>
 /// Method to perform all necessary steps to find one calibration point
 /// in three-point calibration
 /// </summary>
 /// <param name="frame">Frame number 0-based from start of operation</param>
 /// <param name="camImage">The current camera image</param>
 /// <param name="voltages">The voltages to move laser to</param>
 /// <returns>-1,-1 or the identified point</returns>
 protected IppiPoint MoveAndDetect(int frame, Image8 camImage, IppiPoint_32f voltages)
 {
     if (frame < Properties.Settings.Default.FrameRate / 10)
     {
         //In the first 100 ms we just give the scanner ample time to reach the target
         _scanner.Hit(voltages);
         return(new IppiPoint(-1, -1));
     }
     else if (frame < Properties.Settings.Default.FrameRate * 1.1)
     {
         //In the next second we build our foreground
         _laser.LaserPower = Properties.Settings.Default.LaserCalibPowermW;
         _fgModel.UpdateBackground(camImage);
         return(new IppiPoint(-1, -1));
     }
     else
     {
         //Let's find the beam location and return it
         return(FindBeamLocation(_fgModel.Background, _bgModel.Background, _calc, _foreground));
     }
 }
Пример #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);
        }
Пример #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);
        }