public Tracker90mmDish(int imageWidth, int imageHeight)
        {
            _foreground   = new Image8(imageWidth, imageHeight);
            _calc         = new Image8(imageWidth, imageHeight);
            _labelMarkers = new Image8(imageWidth, imageHeight);
            int bufferSize = 0;

            IppHelper.IppCheckCall(cv.ippiLabelMarkersGetBufferSize_8u_C1R(new IppiSize(imageWidth, imageHeight), &bufferSize));
            _markerBuffer = (byte *)Marshal.AllocHGlobal(bufferSize);
            fixed(IppiMomentState_64s **ppState = &_momentState)
            {
                //let ipp decide whether to give accurate or fast results
                IppHelper.IppCheckCall(ip.ippiMomentInitAlloc_64s(ppState, IppHintAlgorithm.ippAlgHintNone));
            }

            _frame = 0;
            //populate tracking parameters with default values
            _threshold        = 5;
            _minArea          = 10;
            _maxArea          = 300;
            _fullTrustMinArea = 20;
            _imageROI         = new IppiROI(0, 0, imageWidth, imageHeight);
            //The following calculation for FramesInBackground means that after ~30s of movie
            //a stationary object will have dissappeared into the background (at 63% level)
            FramesInBackground      = (int)((30 * 240));
            FramesInitialBackground = 2 * FramesInBackground;
        }
Example #2
0
        public Tracker90mmDish(int imageWidth, int imageHeight, IppiPoint dishCenter)
        {
            _foreground   = new Image8(imageWidth, imageHeight);
            _bgSubtracted = new Image8(imageWidth, imageHeight);
            _calc         = new Image8(imageWidth, imageHeight);
            _dishCenter   = dishCenter;
            int bufferSize = 0;

            IppHelper.IppCheckCall(cv.ippiLabelMarkersGetBufferSize_8u_C1R(new IppiSize(imageWidth, imageHeight), &bufferSize));
            _markerBuffer = (byte *)Marshal.AllocHGlobal(bufferSize);
            int momentSize = 0;

            IppHelper.IppCheckCall(ip.ippiMomentGetStateSize_64f(IppHintAlgorithm.ippAlgHintNone, &momentSize));
            _momentState = (IppiMomentState_64f *)Marshal.AllocHGlobal(momentSize);
            //let ipp decide whether to give accurate or fast results
            IppHelper.IppCheckCall(ip.ippiMomentInit_64f(_momentState, IppHintAlgorithm.ippAlgHintNone));
            _frame = 0;
            //populate tracking parameters with default values
            _threshold        = 6;
            _minArea          = 11;
            _maxAllowedArea   = 120;
            _minEccentricity  = 0.3;
            _fullTrustMinArea = 20;
            _imageROI         = new IppiROI(0, 0, imageWidth, imageHeight);
            _searchRegionSize = 90;
            _removeCMOSISBrightLineArtefact = false;
            _strel3x3 = Morphology.Generate3x3Mask(_foreground.Size);
            //The following calculation for FramesInBackground means that after ~30s of movie
            //a stationary object will have dissappeared into the background (at 63% level)
            FramesInBackground      = (int)((30 * 250));
            FramesInitialBackground = 2 * 30 * 250;
            BGUpdateEvery           = 2;
        }
 /// <summary>
 /// Creates a new BLIScanLookup Table which is prefilled
 /// with previously obtained point-voltage relationships
 /// </summary>
 /// <param name="xVolts">The 2D table of x-Voltages</param>
 /// <param name="yVolts">The 2D table of y-Voltages</param>
 /// <param name="scanRoi">The image ROI of the coordinates</param>
 /// <param name="spacing">The spacing between the interpolation anchors stored in the table</param>
 public BLIScanLookupTable(float[] xVolts, float[] yVolts, IppiROI scanRoi, int spacing = 8)
 {
     if (xVolts.Length != yVolts.Length)
     {
         throw new ArgumentException("The two voltage lookup tables must have the same number of elements");
     }
     _c = (int)Math.Floor((float)scanRoi.Width / spacing) + 1;
     _r = (int)Math.Floor((float)scanRoi.Height / spacing) + 1;
     if (xVolts.Length != _c * _r)
     {
         throw new ArgumentException("The supplied voltage tables don't match with the image/border/spacing dimensions");
     }
     _spacing  = spacing;
     _xVolts   = xVolts;
     _yVolts   = yVolts;
     _complete = true;
     _addIndex = _c * _r;
     _scanRoi  = scanRoi;
 }
 /// <summary>
 /// Creates a new empty BLIScanLookupTable
 /// </summary>
 /// <param name="scanRoi">The image ROI in which to compute coordinates</param>
 /// <param name="spacing">The spacing between the interpolation anchors stored in the table</param>
 public BLIScanLookupTable(IppiROI scanRoi, int spacing = 8)
 {
     //validate dimensions in relation to spacing
     if (scanRoi.Width % spacing != 0)
     {
         throw new ArgumentException("The ROI width has to be dividable by the spacing");
     }
     if (scanRoi.Height % spacing != 0)
     {
         throw new ArgumentException("The ROI height has to be dividable by the spacing");
     }
     _spacing = spacing;
     //initialize arrays
     _c        = (int)Math.Floor((float)scanRoi.Width / _spacing) + 1;
     _r        = (int)Math.Floor((float)scanRoi.Height / spacing) + 1;
     _xVolts   = new float[_c * _r];
     _yVolts   = new float[_c * _r];
     _addIndex = -1;
     _scanRoi  = scanRoi;
 }
Example #5
0
        /// <summary>
        /// Differences the current image with the previous one and computes the number of pixels
        /// above the set difference threshold in the selected ROI
        /// </summary>
        /// <param name="imCurrent">The current frame to difference with the previous</param>
        /// <param name="roi">The ROI in which to perform the compuation</param>
        /// <returns>Number of pixels above threshold in the delta frame</returns>
        public int ComputeNumDeltaPixels(Image8 imCurrent, IppiROI roi)
        {
            double nPixels = 0;

            if (!_isFirst)
            {
                //form difference image
                cv.ippiAbsDiff_8u_C1R(imCurrent[roi.TopLeft], imCurrent.Stride, _imPrevious[roi.TopLeft], _imPrevious.Stride, _imDelta[roi.TopLeft], _imDelta.Stride, roi.Size);
                //threshold image
                BWImageProcessor.Im2Bw(_imDelta, _imThresh, roi, _threshold);
                //count pixels
                ip.ippiSum_8u_C1R(_imThresh[roi.TopLeft], _imThresh.Stride, roi.Size, &nPixels);
            }
            else
            {
                _isFirst = false;
            }
            //copy current image to previous image buffer
            ip.ippiCopy_8u_C1R(imCurrent[roi.TopLeft], imCurrent.Stride, _imPrevious[roi.TopLeft], _imPrevious.Stride, roi.Size);
            //return pixel count - divide sum by 255 since threshold sets al values to 255
            return((int)nPixels / 255);
        }
        /// <summary>
        /// Extracts a fish (candidate) from an image by performing background subtraction, noise filtering, thresholding and closing to obtain a foreground
        /// followed by marker extraction.
        /// </summary>
        /// <param name="im">The image to extract the fish from</param>
        /// <param name="region">The ROI to search</param>
        /// <returns>The most likely fish blob or null if no suitable candidate was found</returns>
        protected BlobWithMoments ExtractFish(/*Image8 im,*/ IppiROI region)
        {
            int nMarkers = 0;

            BlobWithMoments[] blobsDetected;



            //Copy foreground to marker and label connected components
            //IppHelper.IppCheckCall(ip.ippiCopy_8u_C1R(_foreground[region.TopLeft], _foreground.Stride, _labelMarkers[region.TopLeft], _labelMarkers.Stride, region.Size));
            IppHelper.IppCheckCall(cv.ippiLabelMarkers_8u_C1IR(_foreground[region.TopLeft], _foreground.Stride, region.Size, 1, 254, IppiNorm.ippiNormInf, &nMarkers, _markerBuffer));
            //loop over returned markers and use ipp to extract blobs
            if (nMarkers > 0)
            {
                if (nMarkers > 254)
                {
                    nMarkers = 254;
                }
                blobsDetected = new BlobWithMoments[nMarkers];
                for (int i = 1; i <= nMarkers; i++)
                {
                    //label all pixels with the current marker as 255 and others as 0
                    IppHelper.IppCheckCall(ip.ippiCompareC_8u_C1R(_foreground[region.TopLeft], _foreground.Stride, (byte)i, _calc[region.TopLeft], _calc.Stride, region.Size, IppCmpOp.ippCmpEq));
                    //calculate image moments
                    IppHelper.IppCheckCall(ip.ippiMoments64s_8u_C1R(_calc[region.TopLeft], _calc.Stride, region.Size, _momentState));
                    //retrieve moments
                    long m00 = 0;
                    long m10 = 0;
                    long m01 = 0;
                    long m20 = 0;
                    long m02 = 0;
                    long m11 = 0;
                    long m30 = 0;
                    long m03 = 0;
                    long m21 = 0;
                    long m12 = 0;
                    ip.ippiGetSpatialMoment_64s(_momentState, 0, 0, 0, new IppiPoint(region.X, region.Y), &m00, 0);
                    //since our input image is not 0s and 1s but 0s and 255s we have to divide by 255 in order to re-normalize our moments
                    System.Diagnostics.Debug.Assert(m00 % 255 == 0, "M00 was not a multiple of 255");
                    m00 /= 255;
                    //only retrieve other moments if this is a "fish candidate"
                    if (m00 > MinArea && m00 <= MaxArea)
                    {
                        ip.ippiGetSpatialMoment_64s(_momentState, 1, 0, 0, new IppiPoint(region.X, region.Y), &m10, 0);
                        m10 /= 255;
                        ip.ippiGetSpatialMoment_64s(_momentState, 0, 1, 0, new IppiPoint(region.X, region.Y), &m01, 0);
                        m01 /= 255;
                        ip.ippiGetSpatialMoment_64s(_momentState, 2, 0, 0, new IppiPoint(region.X, region.Y), &m20, 0);
                        m20 /= 255;
                        ip.ippiGetSpatialMoment_64s(_momentState, 0, 2, 0, new IppiPoint(region.X, region.Y), &m02, 0);
                        m02 /= 255;
                        ip.ippiGetSpatialMoment_64s(_momentState, 1, 1, 0, new IppiPoint(region.X, region.Y), &m11, 0);
                        m11 /= 255;
                        ip.ippiGetSpatialMoment_64s(_momentState, 3, 0, 0, new IppiPoint(region.X, region.Y), &m30, 0);
                        m30 /= 255;
                        ip.ippiGetSpatialMoment_64s(_momentState, 0, 3, 0, new IppiPoint(region.X, region.Y), &m03, 0);
                        m03 /= 255;
                        ip.ippiGetSpatialMoment_64s(_momentState, 2, 1, 0, new IppiPoint(region.X, region.Y), &m21, 0);
                        m21 /= 255;
                        ip.ippiGetSpatialMoment_64s(_momentState, 1, 2, 0, new IppiPoint(region.X, region.Y), &m12, 0);
                        m12 /= 255;
                        blobsDetected[i - 1] = new BlobWithMoments(m00, m10, m01, m20, m11, m02, m30, m03, m21, m12);
                        //Determine bounding box of the blob. The following seems kinda retarded as Ipp must already
                        //have obtained that information before so maybe there is some way to actually retrieve it??
                        //Do linescans using ipp's sum function starting from the blobs centroid until we hit a line
                        //the sum of which is 0
                        int       xStart, xEnd, yStart, yEnd;
                        double    sum      = 1;
                        IppiPoint centroid = blobsDetected[i - 1].Centroid;
                        xStart = centroid.x - 5;
                        xEnd   = centroid.x + 5;
                        yStart = centroid.y - 5;
                        yEnd   = centroid.y + 5;
                        //in the following loops we PRE-increment, whence we stop the loop if we are at one coordinate short of the ends
                        //find xStart
                        while (sum > 0 && xStart > region.X + 4)
                        {
                            xStart -= 5;
                            IppHelper.IppCheckCall(ip.ippiSum_8u_C1R(_calc[xStart, region.Y], _calc.Stride, new IppiSize(1, region.Height), &sum));
                        }
                        xStart += 1;//we have a sum of 0, so go back one line towards the centroid
                        //find xEnd
                        sum = 1;
                        while (sum > 0 && xEnd < region.X + region.Width - 6)
                        {
                            xEnd += 5;
                            IppHelper.IppCheckCall(ip.ippiSum_8u_C1R(_calc[xEnd, region.Y], _calc.Stride, new IppiSize(1, region.Height), &sum));
                        }
                        xEnd -= 1;//we have sum of 0, so go back one line towards the centroid
                        //find yStart - we can limit our x-search-space as we already have those boundaries
                        sum = 1;
                        while (sum > 0 && yStart > region.Y + 4)
                        {
                            yStart -= 5;
                            IppHelper.IppCheckCall(ip.ippiSum_8u_C1R(_calc[xStart, yStart], _calc.Stride, new IppiSize(xEnd - xStart + 1, 1), &sum));
                        }
                        yStart += 1;
                        //find yEnd - again limit summation to x-search-space
                        sum = 1;
                        while (sum > 0 && yEnd < region.Y + region.Height - 6)
                        {
                            yEnd += 5;
                            IppHelper.IppCheckCall(ip.ippiSum_8u_C1R(_calc[xStart, yEnd], _calc.Stride, new IppiSize(xEnd - xStart + 1, 1), &sum));
                        }
                        yEnd -= 1;
                        blobsDetected[i - 1].BoundingBox = new IppiRect(xStart, yStart, xEnd - xStart + 1, yEnd - yStart + 1);
                    }
                    else
                    {
                        blobsDetected[i - 1] = new BlobWithMoments();
                    }
                }
            }
            else
            {
                return(null);
            }
            long maxArea  = 0;
            int  maxIndex = -1;

            for (int i = 0; i < blobsDetected.Length; i++)
            {
                if (blobsDetected[i] == null)
                {
                    break;
                }
                //Simply note down the largest blob
                if (blobsDetected[i].Area > maxArea)
                {
                    maxArea  = blobsDetected[i].Area;
                    maxIndex = i;
                }
            }

            if (maxArea < MinArea)
            {
                return(null);
            }
            else
            {
                return(blobsDetected[maxIndex]);
            }
        }
 /// <summary>
 /// Performs a 3x3 closing operation on an image
 /// </summary>
 /// <param name="im">The (thresholded) image to close</param>
 /// <param name="region">The ROI in which to perform the operation</param>
 protected void Close3x3(Image8 im, IppiROI region)
 {
     IppHelper.IppCheckCall(ip.ippiDilate3x3_8u_C1IR(im[region.TopLeft.x + 1, region.TopLeft.y + 1], im.Stride, new IppiSize(region.Width - 2, region.Height - 2)));
     IppHelper.IppCheckCall(ip.ippiErode3x3_8u_C1IR(im[region.TopLeft.x + 1, region.TopLeft.y + 1], im.Stride, new IppiSize(region.Width - 2, region.Height - 2)));
 }
 /// <summary>
 /// Implements a "greater than" threshold like MATLABS
 /// im2bw function
 /// </summary>
 /// <param name="im">The image to threshold</param>
 /// <param name="region">The ROI in which to perform the operation</param>
 /// <param name="threshold">The threshold to apply</param>
 protected void Im2Bw(Image8 im, IppiROI region)
 {
     IppHelper.IppCheckCall(ip.ippiThreshold_LTVal_8u_C1IR(im[region.TopLeft], im.Stride, region.Size, (byte)(_threshold + 1), 0));
     IppHelper.IppCheckCall(ip.ippiThreshold_GTVal_8u_C1IR(im[region.TopLeft], im.Stride, region.Size, _threshold, 255));
 }
        /// <summary>
        /// Extracts for each well the most likely fish-blob or null if no suitable candidate was found
        /// </summary>
        /// <returns>All fish blobs found in the image</returns>
        private BlobWithMoments[] ExtractAll()
        {
            int nMarkers = 0;

            //we have fixed bounding boxes for each fish that are square and 1.5 times the typical fish-length
            //with the fishes centroid centered in the box
            int bbOffset = (int)(_typicalFishLength * 0.75);
            int bbSize   = (int)(_typicalFishLength * 1.5);

            //Copy foreground to marker and label connected components
            //IppHelper.IppCheckCall(ip.ippiCopy_8u_C1R(_foreground.Image, _foreground.Stride, _labelMarkers.Image, _labelMarkers.Stride, _foreground.Size));
            IppHelper.IppCheckCall(cv.ippiLabelMarkers_8u_C1IR(_foreground.Image, _foreground.Stride, _foreground.Size, 1, 254, IppiNorm.ippiNormInf, &nMarkers, _markerBuffer));

            if (nMarkers > 254)
            {
                nMarkers = 254;
            }

            //fish are identified by looping over all wells and computing the histogram of pixel values for each well. This will effectively tell us
            //which marker is the most abundant in the given well => this would be the fish by our general detection logic
            //The maximum value that ippiLabelMarkers has used is equal to nMarkers. Therefore, when we compute our histogram, we supply
            //(nMarkers+2) as the nLevels parameter - this will give us (nMarkers+1) bins containing the counts from 0->nMarkers with 0 being our background

            //loop over wells
            for (int i = 0; i < _wellnumber; i++)
            {
                IppiROI well = _wells[i];
                //get well-specific maxvalue
                //byte maxVal = 0;
                //IppHelper.IppCheckCall(ip.ippiMax_8u_C1R(_labelMarkers[well.TopLeft], _labelMarkers.Stride, well.Size, &maxVal));
                //compute histogram in this well - from 0 until maxval
                IppHelper.IppCheckCall(ip.ippiHistogramRange_8u_C1R(_foreground[well.TopLeft], _foreground.Stride, well.Size, _hist, _histogramLevels, nMarkers + 2));
                //_hist now contains in position i the abundance of marker i => by looping over it from 1->nMarkers we can find the largest blob. If this blob is
                //larger than our minimum size, we compute the moments and initialize a BlobWithMoments structure for it
                int maxCount = 0;
                int maxIndex = -1;
                for (int j = 1; j <= nMarkers; j++)
                {
                    if (_hist[j] > maxCount && _hist[j] > _minArea && _hist[j] <= _maxArea)
                    {
                        maxCount = _hist[j];
                        maxIndex = j;
                    }
                }
                if (maxIndex == -1)//no suitable fish found
                {
                    _allFish[i] = null;
                }
                else
                {
                    //compare and compute moments
                    //label all pixels with the current marker as 255 and others as 0
                    IppHelper.IppCheckCall(ip.ippiCompareC_8u_C1R(_foreground[well.TopLeft], _foreground.Stride, (byte)maxIndex, _wellCompare.Image, _wellCompare.Stride, well.Size, IppCmpOp.ippCmpEq));
                    //calculate image moments
                    IppHelper.IppCheckCall(ip.ippiMoments64s_8u_C1R(_wellCompare.Image, _wellCompare.Stride, well.Size, _momentState));
                    //retrieve moments
                    long m00 = 0;
                    long m10 = 0;
                    long m01 = 0;
                    long m20 = 0;
                    long m02 = 0;
                    long m11 = 0;
                    long m30 = 0;
                    long m03 = 0;
                    long m21 = 0;
                    long m12 = 0;
                    ip.ippiGetSpatialMoment_64s(_momentState, 0, 0, 0, well.TopLeft, &m00, 0);
                    m00 /= 255;
                    ip.ippiGetSpatialMoment_64s(_momentState, 1, 0, 0, well.TopLeft, &m10, 0);
                    m10 /= 255;
                    ip.ippiGetSpatialMoment_64s(_momentState, 0, 1, 0, well.TopLeft, &m01, 0);
                    m01 /= 255;
                    ip.ippiGetSpatialMoment_64s(_momentState, 2, 0, 0, well.TopLeft, &m20, 0);
                    m20 /= 255;
                    ip.ippiGetSpatialMoment_64s(_momentState, 0, 2, 0, well.TopLeft, &m02, 0);
                    m02 /= 255;
                    ip.ippiGetSpatialMoment_64s(_momentState, 1, 1, 0, well.TopLeft, &m11, 0);
                    m11 /= 255;
                    ip.ippiGetSpatialMoment_64s(_momentState, 3, 0, 0, well.TopLeft, &m30, 0);
                    m30 /= 255;
                    ip.ippiGetSpatialMoment_64s(_momentState, 0, 3, 0, well.TopLeft, &m03, 0);
                    m03 /= 255;
                    ip.ippiGetSpatialMoment_64s(_momentState, 2, 1, 0, well.TopLeft, &m21, 0);
                    m21 /= 255;
                    ip.ippiGetSpatialMoment_64s(_momentState, 1, 2, 0, well.TopLeft, &m12, 0);
                    m12        /= 255;
                    _allFish[i] = new BlobWithMoments(m00, m10, m01, m20, m11, m02, m30, m03, m21, m12);
                    //assign bounding box
                    IppiRect bBox = new IppiRect(_allFish[i].Centroid.x - bbOffset, _allFish[i].Centroid.y - bbOffset, bbSize, bbSize);
                    if (bBox.x < well.X)
                    {
                        bBox.x = well.X;
                    }
                    if (bBox.y < well.Y)
                    {
                        bBox.y = well.Y;
                    }
                    if (bBox.x + bBox.width > well.X + well.Width)
                    {
                        bBox.width = well.X + well.Width - bBox.x;
                    }
                    if (bBox.y + bBox.height > well.Y + well.Height)
                    {
                        bBox.height = well.Y + well.Height - bBox.y;
                    }
                    _allFish[i].BoundingBox = bBox;
                }
            }//end looping over all wells

            return(_allFish);
        }
        /// <summary>
        /// Constructs a new multi-well tracker
        /// </summary>
        /// <param name="imageWidth">The total width of the image we want to track</param>
        /// <param name="imageHeight">The total height of the image we want to track</param>
        /// <param name="plate_index">This index is appended to the ROI file name to load file for proper plate</param>
        /// <param name="parallelChunks">The number of parallel chunks to track</param>
        public TrackerMultiWell(int imageWidth, int imageHeight, int plate_index, int parallelChunks = 2) : base(imageWidth, imageHeight)
        {
            _typicalFishLength = 30;
            _trackMethod       = TrackMethods.PerWell;//default to per-well tracking method
            //Load ROIs - the following way to assess how many lines we have and hence how many
            //ROIs we are dealing with is ugly but what the heck its quick to think up
            _wellnumber = 0;
            var          assembly   = Assembly.GetExecutingAssembly();
            StreamReader roiStream  = null;
            string       fileToOpen = string.Format("SleepTracker.ROI_defs_{0}.txt", plate_index);

            //open for prescanning
            var s = assembly.GetManifestResourceStream(fileToOpen);

            roiStream = new StreamReader(s);
            while (!roiStream.EndOfStream)
            {
                string   line   = roiStream.ReadLine();
                string[] values = line.Split('\t');
                if (values.Length == 4)
                {
                    _wellnumber++;
                }
            }
            roiStream.Dispose();
            //reopen to load wells
            s         = assembly.GetManifestResourceStream(fileToOpen);
            roiStream = new StreamReader(s);
            _wells    = new IppiROI[_wellnumber];
            //we use the following hash-table to keep track of wells belonging
            //to each row. This will give us the number of rows and their boundaries
            //for later parallel chunk boundary determination as well as which wells
            //should be tracked in which chunk
            Dictionary <int, List <IppiROI> > wellsPerRow  = new Dictionary <int, List <IppiROI> >();
            Dictionary <int, int>             columnStarts = new Dictionary <int, int>();

            for (int i = 0; i < _wellnumber; i++)
            {
                System.Diagnostics.Debug.Assert(!roiStream.EndOfStream, "Unexpectedly reached end of ROI file");
                string   line   = roiStream.ReadLine();
                string[] values = line.Split('\t');
                System.Diagnostics.Debug.Assert(values.Length == 4, "Found line in ROI file that does not contain 4 tab-separated strings");
                int[] numValues = new int[4];
                for (int j = 0; j < 4; j++)
                {
                    numValues[j] = int.Parse(values[j]);
                }
                _wells[i] = new IppiROI(numValues[1], numValues[0], numValues[2], numValues[3]);
                if (wellsPerRow.ContainsKey(_wells[i].Y))
                {
                    wellsPerRow[_wells[i].Y].Add(_wells[i]);
                }
                else
                {
                    //create a new list for this row and append our first well
                    wellsPerRow[_wells[i].Y] = new List <IppiROI>();
                    wellsPerRow[_wells[i].Y].Add(_wells[i]);
                }
                if (columnStarts.ContainsKey(_wells[i].X))
                {
                    columnStarts[_wells[i].X]++;
                }
                else
                {
                    columnStarts[_wells[i].X] = 1;
                }
            }
            roiStream.Dispose();
            //can't have more parallel regions than we have rows of wells
            if (parallelChunks > wellsPerRow.Count)
            {
                parallelChunks = wellsPerRow.Count;
            }
            _allFish = new BlobWithMoments[_wellnumber];
            //initialize our histogram bin boundaries (=histogram levels)
            //the following assignment will allow us to study marker values from 0 to 254
            //since: h[k] = countof(pLevels[k] <= pixels(x,y) < pLevels[k+1])
            _histogramLevels = (int *)Marshal.AllocHGlobal(sizeof(int) * 256);
            for (int i = 0; i < 256; i++)
            {
                _histogramLevels[i] = i;
            }
            _hist = (int *)Marshal.AllocHGlobal(sizeof(int) * 255);
            //assume all wells have the same size
            _wellCompare = new Image8(_wells[1].Size.width, _wells[1].Size.height);

            _parallelChunks = parallelChunks;

            //Initialize result array for parallel tracking
            _parallelTrackResults = new BlobWithMoments[_wellnumber];
            //Set up image regions for parallel tracking
            _parallelImageRegions = new IppiROI[_parallelChunks];
            //populate image regions and parallel buffers if _parallelChunks is larger 1
            if (_parallelChunks > 1)
            {
                _parallelMarkerBuffers = new byte *[_parallelChunks];
                _parallelMomentStates  = new IppiMomentState_64s *[_parallelChunks];
                //determine the number of rows in each chunk - integer division and last chunk get's the remainder tucked on
                int nPerChunk  = wellsPerRow.Count / _parallelChunks;
                int nLastChunk = wellsPerRow.Count - (_parallelChunks - 1) * nPerChunk;
                //obtain all our row-starting coordinates and sort ascending
                var rowCoordinates = wellsPerRow.Keys.ToArray();
                Array.Sort(rowCoordinates);
                //do the same for column starting coordinates
                var colCoordinates = columnStarts.Keys.ToArray();
                Array.Sort(colCoordinates);
                //Inititalize our parallel-well list array
                _parallelChunkWells = new List <IppiROI> [_parallelChunks];
                for (int i = 0; i < _parallelChunks; i++)
                {
                    //for each chunk initialize it's well-list
                    _parallelChunkWells[i] = new List <IppiROI>();
                    int startRowInChunk = i * nPerChunk;
                    int endRowInChunk;
                    //are we dealing with the last chunk - this one potentially has a different number of rows!
                    if (i == _parallelChunks - 1)
                    {
                        endRowInChunk = startRowInChunk + nLastChunk - 1;
                    }
                    else
                    {
                        endRowInChunk = startRowInChunk + nPerChunk - 1;
                    }
                    //add all wells of this chunk to the list by looping over rows
                    //finding their start coordinate and using that to index into
                    //our dictionary. Then loop over the list in the dictionary
                    for (int j = startRowInChunk; j <= endRowInChunk; j++)
                    {
                        foreach (IppiROI wr in wellsPerRow[rowCoordinates[j]])
                        {
                            _parallelChunkWells[i].Add(wr);
                        }
                    }
                    //determine top-left corner as well as width and height of this chunk
                    int y_top    = wellsPerRow[rowCoordinates[0]][0].Y;
                    int y_bottom = wellsPerRow[rowCoordinates[endRowInChunk]][0].Y + wellsPerRow[rowCoordinates[endRowInChunk]][0].Height - 1;
                    int height   = y_bottom - y_top + 1;
                    System.Diagnostics.Debug.Assert(height > 1);
                    int x_left  = colCoordinates[0];
                    int x_right = colCoordinates[colCoordinates.Length - 1] + wellsPerRow[rowCoordinates[0]][0].Width - 1;
                    int width   = x_right - x_left + 1;
                    System.Diagnostics.Debug.Assert(width > 1);
                    _parallelImageRegions[i] = new IppiROI(x_left, y_top, width, height);
                    //Initialize marker buffer for this chunk
                    int bufferSize = 0;
                    IppHelper.IppCheckCall(cv.ippiLabelMarkersGetBufferSize_8u_C1R(_parallelImageRegions[i].Size, &bufferSize));
                    _parallelMarkerBuffers[i] = (byte *)Marshal.AllocHGlobal(bufferSize);
                    //initialize moment state for this chunk
                    fixed(IppiMomentState_64s **ppState = &_parallelMomentStates[i])
                    {
                        //let ipp decide whether to give accurate or fast results
                        IppHelper.IppCheckCall(ip.ippiMomentInitAlloc_64s(ppState, IppHintAlgorithm.ippAlgHintNone));
                    }
                }
                //determine each chunks start index in the fish output array based on the number of wells
                //in lower chunks
                _parallelCumWellCount = new int[_parallelChunks];
                for (int i = 1; i < _parallelChunks; i++)
                {
                    _parallelCumWellCount[i] = _parallelCumWellCount[i - 1] + _parallelChunkWells[i - 1].Count;
                }
            }
        }
Example #11
0
        /// <summary>
        /// Extracts a fish (candidate) from an image by performing background subtraction, noise filtering, thresholding and closing to obtain a foreground
        /// followed by marker extraction.
        /// </summary>
        /// <param name="im">The image to extract the fish from</param>
        /// <param name="region">The ROI to search</param>
        /// <returns>The most likely fish blob or null if no suitable candidate was found</returns>
        BlobWithMoments ExtractFish(Image8 im, IppiROI region)
        {
            int nMarkers = 0;


            //Perform background subtraction - CASH BACKGROUND IMAGE POINTER - otherwise we actually do the whole costly
            //32f conversion twice - once for accessing the actual image, and once for accessing the stride...
            var bg = _bgModel.Background;

            IppHelper.IppCheckCall(cv.ippiAbsDiff_8u_C1R(im[region.TopLeft], im.Stride, bg[region.TopLeft], bg.Stride, _calc[region.TopLeft], _calc.Stride, region.Size));
            //remove noise via median filtering
            _mFiltSize.width  = region.Width - 2;
            _mFiltSize.height = region.Height - 2;
            IppHelper.IppCheckCall(ip.ippiFilterMedianWeightedCenter3x3_8u_C1R(_calc[region.X + 1, region.Y + 1], _calc.Stride,
                                                                               _bgSubtracted[region.X + 1, region.Y + 1], _bgSubtracted.Stride, _mFiltSize, 1));
            //Threshold and close
            Im2Bw(_bgSubtracted, _foreground, region);
            //Do as two step to get information back into _foreground
            _strel3x3.Dilate(_foreground, _calc, region);
            _strel3x3.Erode(_calc, _foreground, region);
            //Label connected components
            IppHelper.IppCheckCall(cv.ippiLabelMarkers_8u_C1IR(_foreground[region.TopLeft], _foreground.Stride, region.Size, 1, 254, IppiNorm.ippiNormInf, &nMarkers, _markerBuffer));
            //loop over returned markers and use ipp to extract blobs
            if (nMarkers > 0)
            {
                if (nMarkers > 254)
                {
                    nMarkers = 254;
                }
                //create or update our intermediate blob storage to store the required number of marker representations
                if (_blobsDetected == null || _blobsDetected.Length < nMarkers)
                {
                    _blobsDetected = new BlobWithMoments[nMarkers];
                }

                for (int i = 1; i <= nMarkers; i++)
                {
                    //label all pixels with the current marker as 255 and others as 0
                    IppHelper.IppCheckCall(ip.ippiCompareC_8u_C1R(_foreground[region.TopLeft], _foreground.Stride, (byte)i, _calc[region.TopLeft], _calc.Stride, region.Size, IppCmpOp.ippCmpEq));
                    //calculate image moments
                    IppHelper.IppCheckCall(ip.ippiMoments64f_8u_C1R(_calc[region.TopLeft], _calc.Stride, region.Size, _momentState));
                    //retrieve moments
                    double m00 = 0;
                    double m10 = 0;
                    double m01 = 0;
                    double m20 = 0;
                    double m02 = 0;
                    double m11 = 0;
                    double m30 = 0;
                    double m03 = 0;
                    double m21 = 0;
                    double m12 = 0;
                    ip.ippiGetSpatialMoment_64f(_momentState, 0, 0, 0, region.TopLeft, &m00);
                    //since our input image is not 0s and 1s but 0s and 255s we have to divide by 255 in order to re-normalize our moments
                    //System.Diagnostics.Debug.Assert(m00 % 255 == 0, "M00 was not a multiple of 255");
                    m00 /= 255;
                    //only retrieve other moments if this is a "fish candidate"
                    if (m00 >= MinArea)
                    {
                        ip.ippiGetSpatialMoment_64f(_momentState, 1, 0, 0, region.TopLeft, &m10);
                        m10 /= 255;
                        ip.ippiGetSpatialMoment_64f(_momentState, 0, 1, 0, region.TopLeft, &m01);
                        m01 /= 255;
                        ip.ippiGetSpatialMoment_64f(_momentState, 2, 0, 0, region.TopLeft, &m20);
                        m20 /= 255;
                        ip.ippiGetSpatialMoment_64f(_momentState, 0, 2, 0, region.TopLeft, &m02);
                        m02 /= 255;
                        ip.ippiGetSpatialMoment_64f(_momentState, 1, 1, 0, region.TopLeft, &m11);
                        m11 /= 255;
                        ip.ippiGetSpatialMoment_64f(_momentState, 3, 0, 0, region.TopLeft, &m30);
                        m30 /= 255;
                        ip.ippiGetSpatialMoment_64f(_momentState, 0, 3, 0, region.TopLeft, &m03);
                        m03 /= 255;
                        ip.ippiGetSpatialMoment_64f(_momentState, 2, 1, 0, region.TopLeft, &m21);
                        m21 /= 255;
                        ip.ippiGetSpatialMoment_64f(_momentState, 1, 2, 0, region.TopLeft, &m12);
                        m12 /= 255;
                        if (_blobsDetected[i - 1] == null)
                        {
                            _blobsDetected[i - 1] = new BlobWithMoments((long)m00, (long)m10, (long)m01,
                                                                        (long)m20, (long)m11, (long)m02, (long)m30, (long)m03, (long)m21, (long)m12);
                        }
                        else
                        {
                            _blobsDetected[i - 1].UpdateBlob((long)m00, (long)m10, (long)m01, (long)m20,
                                                             (long)m11, (long)m02, (long)m30, (long)m03, (long)m21, (long)m12);
                        }
                        //Determine bounding box of the blob. The following seems kinda retarded as Ipp must already
                        //have obtained that information before so maybe there is some way to actually retrieve it??
                        //Do linescans using ipp's sum function starting from the blobs centroid until we hit a line
                        //the sum of which is 0


                        int       xStart, xEnd, yStart, yEnd;
                        double    sum      = 1;
                        IppiPoint centroid = _blobsDetected[i - 1].Centroid;
                        xStart = centroid.x - 1;
                        xEnd   = centroid.x + 1;
                        yStart = centroid.y - 1;
                        yEnd   = centroid.y + 1;
                        //in the following loops we PRE-increment, whence we stop the loop if we are at one coordinate short of the ends
                        //find xStart
                        _bboxScan.width  = 1;
                        _bboxScan.height = region.Height;
                        while (sum > 0 && xStart > (region.X + 4))
                        {
                            xStart -= 5;
                            IppHelper.IppCheckCall(ip.ippiSum_8u_C1R(_calc[xStart, region.Y], _calc.Stride, _bboxScan, &sum));
                        }
                        xStart += 1;//we have a sum of 0, so go back one line towards the centroid
                        //find xEnd
                        sum = 1;
                        while (sum > 0 && xEnd < region.X + region.Width - 5)
                        {
                            xEnd += 5;
                            IppHelper.IppCheckCall(ip.ippiSum_8u_C1R(_calc[xEnd, region.Y], _calc.Stride, _bboxScan, &sum));
                        }
                        xEnd -= 1;//we have sum of 0, so go back one line towards the centroid
                        //find yStart - we can limit our x-search-space as we already have those boundaries
                        _bboxScan.width  = xEnd - xStart + 1;
                        _bboxScan.height = 1;
                        sum = 1;
                        while (sum > 0 && yStart > (region.Y + 4))
                        {
                            yStart -= 5;
                            IppHelper.IppCheckCall(ip.ippiSum_8u_C1R(_calc[xStart, yStart], _calc.Stride, _bboxScan, &sum));
                        }
                        yStart += 1;
                        //find yEnd - again limit summation to x-search-space
                        sum = 1;
                        while (sum > 0 && yEnd < region.Y + region.Height - 5)
                        {
                            yEnd += 5;
                            IppHelper.IppCheckCall(ip.ippiSum_8u_C1R(_calc[xStart, yEnd], _calc.Stride, _bboxScan, &sum));
                        }
                        yEnd -= 1;
                        _blobsDetected[i - 1].UpdateBoundingBox(xStart, yStart, xEnd - xStart + 1, yEnd - yStart + 1);
                    }
                    else
                    {
                        if (_blobsDetected[i - 1] == null)
                        {
                            _blobsDetected[i - 1] = new BlobWithMoments();
                        }
                        else
                        {
                            _blobsDetected[i - 1].ResetBlob();
                        }
                    }
                }
            }
            else
            {
                return(null);
            }
            //decide which of the detected objects is the fish
            //usually we pick the larger blob - however to avoid
            //tracking reflections on the wall, if the largest blob
            //and second largest blob are of comparable size
            //we pick the one which is closer to the center (as reflections
            //are always more eccentric)
            long maxArea        = 0;
            long secondMaxArea  = 0;
            int  maxIndex       = -1;
            int  secondMaxIndex = -1;

            for (int i = 0; i < nMarkers; i++)
            {
                if (_blobsDetected[i] == null)
                {
                    break;
                }
                //Note down the largest and second-largest blob - but only if those blobs aren't larger than the maxArea and if they eccentricity is at least MinEccentricity
                //this comparison allows that if we find two exactly same-sized blobs to consider both but to not consider any further blobs of this size (which we hopefully never have anyways)
                //Eccentricity and MaxAllowedArea checks removed at this point as they were mainly concieved to not track the laser.
                if (_blobsDetected[i].Area >= maxArea && _blobsDetected[i].Area > secondMaxArea /*&& blobsDetected[i].Area<=MaxAllowedArea && blobsDetected[i].Eccentricity>=MinEccentricity*/)
                {
                    secondMaxArea  = maxArea;
                    maxArea        = _blobsDetected[i].Area;
                    secondMaxIndex = maxIndex;
                    maxIndex       = i;
                }
            }

            if (maxArea < MinArea)
            {
                return(null);
            }
            else
            {
                //if our second-largest blob is at least two-thirds the size
                //of the largest blob we also consider distance and swap accordingly
                if ((float)secondMaxArea * 1.5 >= (float)maxArea)
                {
                    double distMax, distSecondMax;
                    distMax       = Distance.Euclidian(_blobsDetected[maxIndex].Centroid, DishCenter);
                    distSecondMax = Distance.Euclidian(_blobsDetected[secondMaxIndex].Centroid, DishCenter);
                    if (distMax > distSecondMax)
                    {
                        maxIndex = secondMaxIndex;
                    }
                }
                return(_blobsDetected[maxIndex]);
            }
        }
Example #12
0
 /// <summary>
 /// Implements a "greater than" threshold like MATLABS
 /// im2bw function
 /// </summary>
 /// <param name="imIn">The image to threshold</param>
 /// <param name="imThresh">The image after thresholding</param>
 /// <param name="region">The ROI in which to perform the operation</param>
 /// <param name="threshold">The threshold to apply</param>
 void Im2Bw(Image8 imIn, Image8 imThresh, IppiROI region)
 {
     IppHelper.IppCheckCall(ip.ippiCompareC_8u_C1R(imIn[region.TopLeft], imIn.Stride, _threshold, imThresh[region.TopLeft], imThresh.Stride, region.Size, IppCmpOp.ippCmpGreater));
 }
Example #13
0
 /// <summary>
 /// Uses tail start and end positions as well
 /// as the image size to define the track region sizes
 /// </summary>
 private void DefineTrackRegions()
 {
     lock (_regionLock)
     {
         int startx, starty, width, height;
         //determine if tail is horizontal or vertical
         if (TailIsVertical())
         {
             starty = TailStart.y < TailEnd.y ? TailStart.y : TailEnd.y;
             height = Math.Abs(TailEnd.y - TailStart.y);
             startx = (int)((TailStart.x + TailEnd.x) / 2 - height);//ROI is centered around the tail
             width  = 2 * height;
         }
         else
         {
             startx = TailStart.x < TailEnd.x ? TailStart.x : TailEnd.x;
             width  = Math.Abs(TailEnd.x - TailStart.x);
             starty = (int)((TailStart.y + TailEnd.y) / 2 - width);
             height = 2 * width;
         }
         //trim to fit into image dimensions
         if (startx < 0)
         {
             startx = 0;
         }
         if (starty < 0)
         {
             starty = 0;
         }
         if (startx + width > _imageSize.width)
         {
             width = _imageSize.width - startx + 1;
         }
         if (starty + height > _imageSize.height)
         {
             height = _imageSize.height - starty + 1;
         }
         //compute and trim outer coordinates - morphological operations need to leave
         //border pixels and don't compute anything useful within the border for those
         //border pixels - therefore we leave a border twice the size of our structuring
         //element
         int outerx = startx - 2 * _strel.Anchor.x;
         int outery = starty - 2 * _strel.Anchor.y;
         int outerw = width + 2 * _strel.Mask.Width;
         int outerh = height + 2 * _strel.Mask.Height;
         if (outerx < 0)
         {
             outerx = 0;
         }
         if (outery < 0)
         {
             outery = 0;
         }
         if (outerx + outerw > _imageSize.width)
         {
             outerw = _imageSize.width - outerx + 1;
         }
         if (outery + outerh > _imageSize.height)
         {
             outerh = _imageSize.height - outery + 1;
         }
         //create regions
         _trackRegionInner = new IppiROI(startx, starty, width, height);
         _trackRegionOuter = new IppiROI(outerx, outery, outerw, outerh);
     }
     _bgValid = false;
 }