Exemple #1
0
 /// <summary>
 /// Convert a chain code contour to a polygon.
 /// </summary>
 /// <param name="cc">Chain code contour.</param>
 /// <returns>A polygon.</returns>
 public static CvContourPolygon ConvertChainCodesToPolygon(CvContourChainCode cc)
 {
     if (cc == null)
     {
         throw new ArgumentNullException("cc");
     }
     return(cc.ConvertToPolygon());
 }
Exemple #2
0
 /// <summary>
 /// Draw a contour.
 /// </summary>
 /// <param name="contour"> Chain code contour.</param>
 /// <param name="img">Image to draw on.</param>
 /// <param name="color">Color to draw (default, white).</param>
 public static void RenderContourChainCode(CvContourChainCode contour, IplImage img, CvScalar color)
 {
     if (contour == null)
     {
         throw new ArgumentNullException("contour");
     }
     contour.Render(img, color);
 }
Exemple #3
0
 /// <summary>
 /// Draw a contour.
 /// </summary>
 /// <param name="contour"> Chain code contour.</param>
 /// <param name="img">Image to draw on.</param>
 public static void RenderContourChainCode(CvContourChainCode contour, Mat img)
 {
     if (contour == null)
     {
         throw new ArgumentNullException("contour");
     }
     contour.Render(img);
 }
Exemple #4
0
 /// <summary>
 /// Calculates perimeter of a chain code contour.
 /// </summary>
 /// <param name="cc">Contour (chain code type).</param>
 /// <returns>Perimeter of the contour.</returns>
 public static double ContourChainCodePerimeter(CvContourChainCode cc)
 {
     if (cc == null)
     {
         throw new ArgumentNullException("cc");
     }
     return(cc.Perimeter());
 }
Exemple #5
0
 /// <summary>
 /// Draw a contour.
 /// </summary>
 /// <param name="contour"> Chain code contour.</param>
 /// <param name="img">Image to draw on.</param>
 /// <param name="color">Color to draw (default, white).</param>
 public static void RenderContourChainCode(CvContourChainCode contour, Mat img, Scalar color)
 {
     if (contour == null)
     {
         throw new ArgumentNullException(nameof(contour));
     }
     contour.Render(img, color);
 }
Exemple #6
0
 /// <summary>
 /// Draw a contour.
 /// </summary>
 /// <param name="contour"> Chain code contour.</param>
 /// <param name="img">Image to draw on.</param>
 public static void RenderContourChainCode(CvContourChainCode contour, IplImage img)
 {
     if (contour == null)
     {
         throw new ArgumentNullException(nameof(contour));
     }
     contour.Render(img);
 }
Exemple #7
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="img"></param>
        /// <param name="blobs"></param>
        /// <returns></returns>
        public static int Perform(Mat img, CvBlobs blobs)
        {
            if (img == null)
            {
                throw new ArgumentNullException(nameof(img));
            }
            if (blobs == null)
            {
                throw new ArgumentNullException(nameof(blobs));
            }
            if (img.Type() != MatType.CV_8UC1)
            {
                throw new ArgumentException("'img' must be a 1-channel U8 image.");
            }

            if (blobs.Labels == null)
            {
                throw new ArgumentException("blobs.Labels == null", nameof(blobs));
            }
            LabelData labels = blobs.Labels;

            //if(labels.GetLength(0) != h || labels.GetLength(1) != w)
            if (labels.Rows != img.Height || labels.Cols != img.Width)
            {
                throw new ArgumentException("img.Size != labels' size");
            }

            int numPixels = 0;

            blobs.Clear();

            int w    = img.Cols;
            int h    = img.Rows;
            int step = (int)img.Step();

            byte[] imgIn;
            unsafe
            {
                byte *imgInPtr = (byte *)img.Data;
                if ((long)h * step > int.MaxValue)
                {
                    throw new ArgumentException("Too big image (image data > 2^31)");
                }
                int length = h * step;
                imgIn = new byte[length];
                Marshal.Copy(new IntPtr(imgInPtr), imgIn, 0, imgIn.Length);
            }
            int    label     = 0;
            int    lastLabel = 0;
            CvBlob?lastBlob  = null;

            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    if (imgIn[x + y * step] == 0)
                    {
                        continue;
                    }

                    bool labeled = labels[y, x] != 0;
                    if (!labeled && ((y == 0) || (imgIn[x + (y - 1) * step] == 0)))
                    {
                        labeled = true;

                        // Label contour.
                        label++;
                        if (label == MarkerValue)
                        {
                            throw new Exception();
                        }

                        labels[y, x] = label;
                        numPixels++;

                        // XXX This is not necessary at all. I only do this for consistency.
                        if (y > 0)
                        {
                            labels[y - 1, x] = MarkerValue;
                        }

                        var blob = new CvBlob(label, x, y);
                        blobs.Add(label, blob);
                        lastLabel = label;
                        lastBlob  = blob;

                        blob.Contour.StartingPoint = new Point(x, y);
                        int  direction  = 1;
                        int  xx         = x;
                        int  yy         = y;
                        bool contourEnd = false;

                        do
                        {
                            for (int numAttempts = 0; numAttempts < 3; numAttempts++)
                            {
                                bool found = false;
                                for (int i = 0; i < 3; i++)
                                {
                                    int nx = xx + MovesE[direction, i, 0];
                                    int ny = yy + MovesE[direction, i, 1];
                                    if ((nx < w) && (nx >= 0) && (ny < h) && (ny >= 0))
                                    {
                                        if (imgIn[nx + ny * step] != 0)
                                        {
                                            found = true;
                                            blob.Contour.ChainCode.Add((CvChainCode)MovesE[direction, i, 3]);
                                            xx        = nx;
                                            yy        = ny;
                                            direction = MovesE[direction, i, 2];
                                            break;
                                        }
                                        labels[ny, nx] = MarkerValue;
                                    }
                                }

                                if (!found)
                                {
                                    direction = (direction + 1) % 4;
                                }
                                else
                                {
                                    if (labels[yy, xx] != label)
                                    {
                                        labels[yy, xx] = label;
                                        numPixels++;

                                        if (xx < blob.MinX)
                                        {
                                            blob.MinX = xx;
                                        }
                                        else if (xx > blob.MaxX)
                                        {
                                            blob.MaxX = xx;
                                        }
                                        if (yy < blob.MinY)
                                        {
                                            blob.MinY = yy;
                                        }
                                        else if (yy > blob.MaxY)
                                        {
                                            blob.MaxY = yy;
                                        }

                                        blob.Area++;
                                        blob.M10 += xx;
                                        blob.M01 += yy;
                                        blob.M11 += xx * yy;
                                        blob.M20 += xx * xx;
                                        blob.M02 += yy * yy;
                                    }
                                    break;
                                }

                                contourEnd = ((xx == x) && (yy == y) && (direction == 1));
                                if (contourEnd)
                                {
                                    break;
                                }
                            }
                        } while (!contourEnd);
                    }

                    if ((y + 1 < h) && (imgIn[x + (y + 1) * step] == 0) && (labels[y + 1, x] == 0))
                    {
                        labeled = true;

                        // Label internal contour
                        int    l;
                        CvBlob?blob;

                        if (labels[y, x] == 0)
                        {
                            l            = labels[y, x - 1];
                            labels[y, x] = l;
                            numPixels++;

                            if (l == lastLabel)
                            {
                                blob = lastBlob;
                            }
                            else
                            {
                                blob      = blobs[l];
                                lastLabel = l;
                                lastBlob  = blob;
                            }
                            if (blob == null)
                            {
                                throw new Exception();
                            }
                            blob.Area++;
                            blob.M10 += x;
                            blob.M01 += y;
                            blob.M11 += x * y;
                            blob.M20 += x * x;
                            blob.M02 += y * y;
                        }
                        else
                        {
                            l = labels[y, x];
                            if (l == lastLabel)
                            {
                                blob = lastBlob;
                            }
                            else
                            {
                                blob      = blobs[l];
                                lastLabel = l;
                                lastBlob  = blob;
                            }
                        }

                        if (blob == null)
                        {
                            throw new Exception();
                        }

                        // XXX This is not necessary (I believe). I only do this for consistency.
                        labels[y + 1, x] = MarkerValue;
                        var contour = new CvContourChainCode
                        {
                            StartingPoint = new Point(x, y)
                        };

                        int direction = 3;
                        int xx        = x;
                        int yy        = y;

                        do
                        {
                            for (int numAttempts = 0; numAttempts < 3; numAttempts++)
                            {
                                bool found = false;

                                for (int i = 0; i < 3; i++)
                                {
                                    int nx = xx + MovesI[direction, i, 0];
                                    int ny = yy + MovesI[direction, i, 1];
                                    if (imgIn[nx + ny * step] != 0)
                                    {
                                        found = true;
                                        contour.ChainCode.Add((CvChainCode)MovesI[direction, i, 3]);
                                        xx        = nx;
                                        yy        = ny;
                                        direction = MovesI[direction, i, 2];
                                        break;
                                    }
                                    labels[ny, nx] = MarkerValue;
                                }

                                if (!found)
                                {
                                    direction = (direction + 1) % 4;
                                }
                                else
                                {
                                    if (labels[yy, xx] == 0)
                                    {
                                        labels[yy, xx] = l;
                                        numPixels++;

                                        blob.Area++;
                                        blob.M10 += xx;
                                        blob.M01 += yy;
                                        blob.M11 += xx * yy;
                                        blob.M20 += xx * xx;
                                        blob.M02 += yy * yy;
                                    }
                                    break;
                                }
                            }
                        } while (!(xx == x && yy == y));

                        blob.InternalContours.Add(contour);
                    }

                    //else if (!imageOut(x, y))
                    if (!labeled)
                    {
                        // Internal pixel
                        int l = labels[y, x - 1];
                        labels[y, x] = l;
                        numPixels++;

                        CvBlob?blob;
                        if (l == lastLabel)
                        {
                            blob = lastBlob;
                        }
                        else
                        {
                            blob      = blobs[l];
                            lastLabel = l;
                            lastBlob  = blob;
                        }
                        if (blob == null)
                        {
                            throw new Exception();
                        }
                        blob.Area++;
                        blob.M10 += x;
                        blob.M01 += y;
                        blob.M11 += x * y;
                        blob.M20 += x * x;
                        blob.M02 += y * y;
                    }
                }
            }


            foreach (var kv in blobs)
            {
                kv.Value.SetMoments();
            }

            GC.KeepAlive(img);

            return(numPixels);
        }
Exemple #8
0
 /// <summary>
 /// Convert a chain code contour to a polygon.
 /// </summary>
 /// <param name="cc">Chain code contour.</param>
 /// <returns>A polygon.</returns>
 public static CvContourPolygon ConvertChainCodesToPolygon(CvContourChainCode cc)
 {
     if (cc == null)
         throw new ArgumentNullException(nameof(cc));
     return cc.ConvertToPolygon();
 }
Exemple #9
0
 /// <summary>
 /// Calculates perimeter of a chain code contour.
 /// </summary>
 /// <param name="cc">Contour (chain code type).</param>
 /// <returns>Perimeter of the contour.</returns>
 public static double ContourChainCodePerimeter(CvContourChainCode cc)
 {
     if (cc == null)
         throw new ArgumentNullException(nameof(cc));
     return cc.Perimeter();
 }
Exemple #10
0
 /// <summary>
 /// Draw a contour.
 /// </summary>
 /// <param name="contour"> Chain code contour.</param>
 /// <param name="img">Image to draw on.</param>
 /// <param name="color">Color to draw (default, white).</param>
 public static void RenderContourChainCode(CvContourChainCode contour, Mat img, Scalar color)
 {
     if (contour == null)
         throw new ArgumentNullException(nameof(contour));
     contour.Render(img, color);
 }
Exemple #11
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="img"></param>
        /// <param name="blobs"></param>
        /// <returns></returns>
        public static int Perform(IplImage img, CvBlobs blobs)
        {
            if (img == null)
                throw new ArgumentNullException("img");
            if (blobs == null)
                throw new ArgumentNullException("blobs");
            if (img.Depth != BitDepth.U8 || img.NChannels != 1)
                throw new ArgumentException("'img' must be a 1-channel U8 image.");

            LabelData labels = blobs.Labels;
            if (labels == null)
                throw new ArgumentException("");
            //if(labels.GetLength(0) != h || labels.GetLength(1) != w)
            if (labels.Rows != img.Height || labels.Cols != img.Width)
                throw new ArgumentException("img.Size != labels' size");

            int numPixels = 0;
            blobs.Clear();

            int step = img.WidthStep;
            CvRect roi = img.ROI;
            int w = roi.Width;
            int h = roi.Height;
            int offset = 0;
            if (img.ROIPointer != IntPtr.Zero)
            {
                IplROI r = img.ROIValue;
                w = r.width;
                h = r.height;
                offset = r.xOffset + (r.yOffset * step);
            }
            byte[] imgIn;
            unsafe
            {
                byte* imgInPtr = img.ImageDataPtr + offset;
                imgIn = new byte[h * step];
                Marshal.Copy(new IntPtr(imgInPtr), imgIn, 0, imgIn.Length);
            }
            int label = 0;
            int lastLabel = 0;
            CvBlob lastBlob = null;


            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    if (imgIn[x + y * step] == 0)
                        continue;

                    bool labeled = labels[y, x] != 0;
                    if (!labeled && ((y == 0) || (imgIn[x + (y - 1) * step] == 0)))
                    {
                        labeled = true;

                        // Label contour.
                        label++;
                        if (label == MarkerValue)
                            throw new Exception();

                        labels[y, x] = label;
                        numPixels++;

                        // XXX This is not necessary at all. I only do this for consistency.
                        if (y > 0)
                            labels[y - 1, x] = MarkerValue;

                        CvBlob blob = new CvBlob(label, x, y);
                        blobs.Add(label, blob);
                        lastLabel = label;
                        lastBlob = blob;

                        blob.Contour.StartingPoint = new CvPoint(x, y);
                        int direction = 1;
                        int xx = x;
                        int yy = y;
                        bool contourEnd = false;

                        do
                        {
                            for (int numAttempts = 0; numAttempts < 3; numAttempts++)
                            {
                                bool found = false;
                                for (int i = 0; i < 3; i++)
                                {
                                    int nx = xx + MovesE[direction, i, 0];
                                    int ny = yy + MovesE[direction, i, 1];
                                    if ((nx < w) && (nx >= 0) && (ny < h) && (ny >= 0))
                                    {
                                        if (imgIn[nx + ny * step] != 0)
                                        {
                                            found = true;
                                            blob.Contour.ChainCode.Add((CvChainCode)MovesE[direction, i, 3]);
                                            xx = nx;
                                            yy = ny;
                                            direction = MovesE[direction, i, 2];
                                            break;
                                        }
                                        labels[ny, nx] = MarkerValue;
                                    }
                                }

                                if (!found)
                                    direction = (direction + 1) % 4;
                                else
                                {
                                    if (labels[yy, xx] != label)
                                    {
                                        labels[yy, xx] = label;
                                        numPixels++;

                                        if (xx < blob.MinX)
                                            blob.MinX = xx;
                                        else if (xx > blob.MaxX)
                                            blob.MaxX = xx;
                                        if (yy < blob.MinY)
                                            blob.MinY = yy;
                                        else if (yy > blob.MaxY)
                                            blob.MaxY = yy;

                                        blob.Area++;
                                        blob.M10 += xx;
                                        blob.M01 += yy;
                                        blob.M11 += xx * yy;
                                        blob.M20 += xx * xx;
                                        blob.M02 += yy * yy;
                                    }
                                    break;
                                }

                                contourEnd = ((xx == x) && (yy == y) && (direction == 1));
                                if (contourEnd)
                                    break;
                            }
                        } while (!contourEnd);

                    }

                    if ((y + 1 < h) && (imgIn[x + (y + 1) * step] == 0) && (labels[y + 1, x] == 0))
                    {
                        labeled = true;

                        // Label internal contour
                        int l;
                        CvBlob blob;

                        if (labels[y, x] == 0)
                        {
                            l = labels[y, x - 1];
                            labels[y, x] = l;
                            numPixels++;

                            if (l == lastLabel)
                                blob = lastBlob;
                            else
                            {
                                blob = blobs[l];
                                lastLabel = l;
                                lastBlob = blob;
                            }
                            if (blob == null)
                                throw new Exception();
                            blob.Area++;
                            blob.M10 += x;
                            blob.M01 += y;
                            blob.M11 += x * y;
                            blob.M20 += x * x;
                            blob.M02 += y * y;
                        }
                        else
                        {
                            l = labels[y, x];
                            if (l == lastLabel)
                                blob = lastBlob;
                            else
                            {
                                blob = blobs[l];
                                lastLabel = l;
                                lastBlob = blob;
                            }
                        }

                        if (blob == null)
                            throw new Exception();

                        // XXX This is not necessary (I believe). I only do this for consistency.
                        labels[y + 1, x] = MarkerValue;
                        var contour = new CvContourChainCode
                        {
                            StartingPoint = new CvPoint(x, y)
                        };

                        int direction = 3;
                        int xx = x;
                        int yy = y;

                        do
                        {
                            for (int numAttempts = 0; numAttempts < 3; numAttempts++)
                            {
                                bool found = false;

                                for (int i = 0; i < 3; i++)
                                {
                                    int nx = xx + MovesI[direction, i, 0];
                                    int ny = yy + MovesI[direction, i, 1];
                                    if (imgIn[nx + ny * step] != 0)
                                    {
                                        found = true;
                                        contour.ChainCode.Add((CvChainCode)MovesI[direction, i, 3]);
                                        xx = nx;
                                        yy = ny;
                                        direction = MovesI[direction, i, 2];
                                        break;
                                    }
                                    labels[ny, nx] = MarkerValue;
                                }

                                if (!found)
                                    direction = (direction + 1) % 4;
                                else
                                {
                                    if (labels[yy, xx] == 0)
                                    {
                                        labels[yy, xx] = l;
                                        numPixels++;

                                        blob.Area++;
                                        blob.M10 += xx;
                                        blob.M01 += yy;
                                        blob.M11 += xx * yy;
                                        blob.M20 += xx * xx;
                                        blob.M02 += yy * yy;
                                    }
                                    break;
                                }
                            }
                        } while (!(xx == x && yy == y));

                        blob.InternalContours.Add(contour);
                    }

                    //else if (!imageOut(x, y))
                    if (!labeled)
                    {
                        // Internal pixel
                        int l = labels[y, x - 1];
                        labels[y, x] = l;
                        numPixels++;

                        CvBlob blob;
                        if (l == lastLabel)
                            blob = lastBlob;
                        else
                        {
                            blob = blobs[l];
                            lastLabel = l;
                            lastBlob = blob;
                        }
                        if (blob == null)
                            throw new Exception();
                        blob.Area++;
                        blob.M10 += x;
                        blob.M01 += y;
                        blob.M11 += x * y;
                        blob.M20 += x * x;
                        blob.M02 += y * y;
                    }
                }
            }


            foreach (var kv in blobs)
            {
                kv.Value.SetMoments();
            }

            return numPixels;

        }
Exemple #12
0
 /// <summary>
 /// Constructor
 /// </summary>
 public CvBlob()
 {
     Contour          = new CvContourChainCode();
     InternalContours = new List <CvContourChainCode>();
 }
Exemple #13
0
 /// <summary>
 /// Constructor
 /// </summary>
 public CvBlob()
 {
     Contour = new CvContourChainCode();
     InternalContours = new List<CvContourChainCode>();
 }
Exemple #14
0
 /// <summary>
 /// Draw a contour.
 /// </summary>
 /// <param name="contour"> Chain code contour.</param>
 /// <param name="img">Image to draw on.</param>
 /// <param name="color">Color to draw (default, white).</param>
 public static void RenderContourChainCode(CvContourChainCode contour, IplImage img, CvScalar color)
 {
     if (contour == null)
         throw new ArgumentNullException("contour");
     contour.Render(img, color);
 }
Exemple #15
0
 /// <summary>
 /// Draw a contour.
 /// </summary>
 /// <param name="contour"> Chain code contour.</param>
 /// <param name="img">Image to draw on.</param>
 public static void RenderContourChainCode(CvContourChainCode contour, Mat img)
 {
     if (contour == null)
         throw new ArgumentNullException("contour");
     contour.Render(img);
 }
Exemple #16
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="img"></param>
        /// <param name="blobs"></param>
        /// <returns></returns>
        public static int Perform(IplImage img, CvBlobs blobs)
        {
            if (img == null)
            {
                throw new ArgumentNullException("img");
            }
            if (blobs == null)
            {
                throw new ArgumentNullException("blobs");
            }
            if (img.Depth != BitDepth.U8 || img.NChannels != 1)
            {
                throw new ArgumentException("'img' must be a 1-channel U8 image.");
            }

            LabelData labels = blobs.Labels;

            if (labels == null)
            {
                throw new ArgumentException("");
            }
            //if(labels.GetLength(0) != h || labels.GetLength(1) != w)
            if (labels.Rows != img.Height || labels.Cols != img.Width)
            {
                throw new ArgumentException("img.Size != labels' size");
            }

            int numPixels = 0;

            blobs.Clear();

            int    step   = img.WidthStep;
            CvRect roi    = img.ROI;
            int    w      = roi.Width;
            int    h      = roi.Height;
            int    offset = 0;

            if (img.ROIPointer != IntPtr.Zero)
            {
                IplROI r = img.ROIValue;
                w      = r.width;
                h      = r.height;
                offset = r.xOffset + (r.yOffset * step);
            }
            byte[] imgIn;
            unsafe
            {
                byte *imgInPtr = img.ImageDataPtr + offset;
                imgIn = new byte[h * step];
                Marshal.Copy(new IntPtr(imgInPtr), imgIn, 0, imgIn.Length);
            }
            int    label     = 0;
            int    lastLabel = 0;
            CvBlob lastBlob  = null;


            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    if (imgIn[x + y * step] == 0)
                    {
                        continue;
                    }

                    bool labeled = labels[y, x] != 0;
                    if (!labeled && ((y == 0) || (imgIn[x + (y - 1) * step] == 0)))
                    {
                        labeled = true;

                        // Label contour.
                        label++;
                        if (label == MarkerValue)
                        {
                            throw new Exception();
                        }

                        labels[y, x] = label;
                        numPixels++;

                        // XXX This is not necessary at all. I only do this for consistency.
                        if (y > 0)
                        {
                            labels[y - 1, x] = MarkerValue;
                        }

                        CvBlob blob = new CvBlob(label, x, y);
                        blobs.Add(label, blob);
                        lastLabel = label;
                        lastBlob  = blob;

                        blob.Contour.StartingPoint = new CvPoint(x, y);
                        int  direction  = 1;
                        int  xx         = x;
                        int  yy         = y;
                        bool contourEnd = false;

                        do
                        {
                            for (int numAttempts = 0; numAttempts < 3; numAttempts++)
                            {
                                bool found = false;
                                for (int i = 0; i < 3; i++)
                                {
                                    int nx = xx + MovesE[direction, i, 0];
                                    int ny = yy + MovesE[direction, i, 1];
                                    if ((nx < w) && (nx >= 0) && (ny < h) && (ny >= 0))
                                    {
                                        if (imgIn[nx + ny * step] != 0)
                                        {
                                            found = true;
                                            blob.Contour.ChainCode.Add((CvChainCode)MovesE[direction, i, 3]);
                                            xx        = nx;
                                            yy        = ny;
                                            direction = MovesE[direction, i, 2];
                                            break;
                                        }
                                        labels[ny, nx] = MarkerValue;
                                    }
                                }

                                if (!found)
                                {
                                    direction = (direction + 1) % 4;
                                }
                                else
                                {
                                    if (labels[yy, xx] != label)
                                    {
                                        labels[yy, xx] = label;
                                        numPixels++;

                                        if (xx < blob.MinX)
                                        {
                                            blob.MinX = xx;
                                        }
                                        else if (xx > blob.MaxX)
                                        {
                                            blob.MaxX = xx;
                                        }
                                        if (yy < blob.MinY)
                                        {
                                            blob.MinY = yy;
                                        }
                                        else if (yy > blob.MaxY)
                                        {
                                            blob.MaxY = yy;
                                        }

                                        blob.Area++;
                                        blob.M10 += xx;
                                        blob.M01 += yy;
                                        blob.M11 += xx * yy;
                                        blob.M20 += xx * xx;
                                        blob.M02 += yy * yy;
                                    }
                                    break;
                                }

                                contourEnd = ((xx == x) && (yy == y) && (direction == 1));
                                if (contourEnd)
                                {
                                    break;
                                }
                            }
                        } while (!contourEnd);
                    }

                    if ((y + 1 < h) && (imgIn[x + (y + 1) * step] == 0) && (labels[y + 1, x] == 0))
                    {
                        labeled = true;

                        // Label internal contour
                        int    l;
                        CvBlob blob;

                        if (labels[y, x] == 0)
                        {
                            l            = labels[y, x - 1];
                            labels[y, x] = l;
                            numPixels++;

                            if (l == lastLabel)
                            {
                                blob = lastBlob;
                            }
                            else
                            {
                                blob      = blobs[l];
                                lastLabel = l;
                                lastBlob  = blob;
                            }
                            if (blob == null)
                            {
                                throw new Exception();
                            }
                            blob.Area++;
                            blob.M10 += x;
                            blob.M01 += y;
                            blob.M11 += x * y;
                            blob.M20 += x * x;
                            blob.M02 += y * y;
                        }
                        else
                        {
                            l = labels[y, x];
                            if (l == lastLabel)
                            {
                                blob = lastBlob;
                            }
                            else
                            {
                                blob      = blobs[l];
                                lastLabel = l;
                                lastBlob  = blob;
                            }
                        }

                        if (blob == null)
                        {
                            throw new Exception();
                        }

                        // XXX This is not necessary (I believe). I only do this for consistency.
                        labels[y + 1, x] = MarkerValue;
                        var contour = new CvContourChainCode
                        {
                            StartingPoint = new CvPoint(x, y)
                        };

                        int direction = 3;
                        int xx        = x;
                        int yy        = y;

                        do
                        {
                            for (int numAttempts = 0; numAttempts < 3; numAttempts++)
                            {
                                bool found = false;

                                for (int i = 0; i < 3; i++)
                                {
                                    int nx = xx + MovesI[direction, i, 0];
                                    int ny = yy + MovesI[direction, i, 1];
                                    if (imgIn[nx + ny * step] != 0)
                                    {
                                        found = true;
                                        contour.ChainCode.Add((CvChainCode)MovesI[direction, i, 3]);
                                        xx        = nx;
                                        yy        = ny;
                                        direction = MovesI[direction, i, 2];
                                        break;
                                    }
                                    labels[ny, nx] = MarkerValue;
                                }

                                if (!found)
                                {
                                    direction = (direction + 1) % 4;
                                }
                                else
                                {
                                    if (labels[yy, xx] == 0)
                                    {
                                        labels[yy, xx] = l;
                                        numPixels++;

                                        blob.Area++;
                                        blob.M10 += xx;
                                        blob.M01 += yy;
                                        blob.M11 += xx * yy;
                                        blob.M20 += xx * xx;
                                        blob.M02 += yy * yy;
                                    }
                                    break;
                                }
                            }
                        } while (!(xx == x && yy == y));

                        blob.InternalContours.Add(contour);
                    }

                    //else if (!imageOut(x, y))
                    if (!labeled)
                    {
                        // Internal pixel
                        int l = labels[y, x - 1];
                        labels[y, x] = l;
                        numPixels++;

                        CvBlob blob;
                        if (l == lastLabel)
                        {
                            blob = lastBlob;
                        }
                        else
                        {
                            blob      = blobs[l];
                            lastLabel = l;
                            lastBlob  = blob;
                        }
                        if (blob == null)
                        {
                            throw new Exception();
                        }
                        blob.Area++;
                        blob.M10 += x;
                        blob.M01 += y;
                        blob.M11 += x * y;
                        blob.M20 += x * x;
                        blob.M02 += y * y;
                    }
                }
            }


            foreach (var kv in blobs)
            {
                kv.Value.SetMoments();
            }

            return(numPixels);
        }