Exemplo n.º 1
0
        public static BinaryImage CropAroundFigures(BinaryImage binaryImage)
        {
            int topPixelRowMWithOffset = binaryImage.FindTopMostPixelPosition() - CroppingOffset;

            if (topPixelRowMWithOffset == -1)
            {
                return(binaryImage);
            }

            int bottomPixelRowMWithOffset   = binaryImage.FindBottomLinePixelIn() + CroppingOffset;
            int leftPixelColumnNWithOffset  = binaryImage.FindLeftMostPixelIn() - CroppingOffset;
            int rightPixelColumnNWithOffset = binaryImage.FindRightMostPixelIn() + CroppingOffset;

            int newTopM    = topPixelRowMWithOffset > 0 ? topPixelRowMWithOffset : 0;
            int newBottomM = bottomPixelRowMWithOffset < binaryImage.Size.Height - 1
                ? bottomPixelRowMWithOffset
                : binaryImage.Size.Height - 1;
            int newLeftN  = leftPixelColumnNWithOffset > 0 ? leftPixelColumnNWithOffset : 0;
            int newRightN = rightPixelColumnNWithOffset < binaryImage.Size.Width - 1
                ? rightPixelColumnNWithOffset
                : binaryImage.Size.Width - 1;

            int newHeight = newBottomM - newTopM + 1;
            int newWidth  = newRightN - newLeftN + 1;

            var croppedImage = new BinaryImage(newWidth, newHeight);

            for (int m = 0; m < newHeight; m++)
            {
                int mPositionInOriginal = topPixelRowMWithOffset + m;

                for (int n = 0; n < newWidth; n++)
                {
                    int nPositionInOriginal = leftPixelColumnNWithOffset + n;

                    croppedImage[m, n] = binaryImage[mPositionInOriginal, nPositionInOriginal];
                }
            }

            return(croppedImage);
        }