public static ImgStripStatus Process(Bitmap imgWithoutModel, Bitmap img, ComparisonWithImgWithoutModelParams @params, Color backgroundColor)
        {
            var imgStripStatus = new ImgStripStatus(img.Width, img.Height);

            for (var y = 0; y < img.Height; y++)
            {
                for (var x = 0; x < img.Width; x++)
                {
                    var imgWithoutModelColor = imgWithoutModel.GetPixel(x, y);
                    var imgColor             = img.GetPixel(x, y);

                    var variationR = Math.Abs(imgColor.R - imgWithoutModelColor.R);
                    var variationG = Math.Abs(imgColor.G - imgWithoutModelColor.G);
                    var variationB = Math.Abs(imgColor.B - imgWithoutModelColor.B);

                    var similar = variationR <= @params.AllowedVariationInRgb && variationG <= @params.AllowedVariationInRgb && variationB <= @params.AllowedVariationInRgb;

                    //if the color is the same as the color on the image without model then it is the background
                    if (similar)
                    {
                        imgStripStatus.SetInvalid(x, y);
                    }
                }
            }
            return(imgStripStatus);
        }
Beispiel #2
0
        public BlockSizeBasedStripper(ImgStripStatus stripStatus, bool blocksOfInvalidPixels = true)
        {
            _stripStatus           = stripStatus;
            _blocksOfInvalidPixels = blocksOfInvalidPixels;
            _blockIndices          = new long[stripStatus.Width, stripStatus.Height];
            Blocks = new Dictionary <long, Block>();

            long blockCounter = 0;

            for (var y = 0; y < _stripStatus.Height; y++)
            {
                for (var x = 0; x < _stripStatus.Width; x++)
                {
                    if (SatisfiesCondition(y, x))
                    {
                        continue;                           //is a background pixel
                    }
                    //if any pixel around it is assigned to a block, take that number
                    var validNeighbors = GetValidNeighbors(y, x, _stripStatus.Width);

                    if (validNeighbors.Count() > 0)
                    {
                        //use the first block index
                        var indexToUse = validNeighbors.First();
                        var blockToUse = Blocks[indexToUse];

                        _blockIndices[x, y] = indexToUse;
                        blockToUse.AddPixelIndex(x, y);

                        //handle the condition where there can be neighbors with different block indices
                        //they all should be normalized to have one block index as they are all connected
                        foreach (var blockIndex in validNeighbors.Where(index => index != indexToUse))
                        {
                            //merge the blocks
                            var blockToMerge = Blocks[blockIndex];
                            foreach (var position in blockToMerge.PixelPositions)
                            {
                                _blockIndices[position.X, position.Y] = indexToUse;
                            }

                            blockToUse.MergeBlock(blockToMerge);
                            Blocks.Remove(blockIndex);
                        }
                    }
                    else
                    {
                        //if none of the neighbors had a block assigned, then assign to a new block
                        blockCounter++;
                        var block = new Block();

                        block.AddPixelIndex(x, y);
                        _blockIndices[x, y] = blockCounter;

                        Blocks.Add(blockCounter, block);
                    }
                }
            }
        }