Exemple #1
0
        public Image <Bgr, byte> CutImageByRectangle(Image <Bgr, byte> image)
        {
            image.ROI = _rectangleOfCut;
            Image <Bgr, byte> result = image.CopyBlank();

            image.CopyTo(result);
            image.ROI = Rectangle.Empty;
            return(result);
        }
Exemple #2
0
        public static Image <Gray, byte> CreateMaskFromPoints(Image <Gray, byte> image, Point[][] conturMatrix)
        {
            Image <Gray, byte> mask = image.CopyBlank();

            using (VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint(conturMatrix))
            {
                CvInvoke.FillPoly(mask, contours, new MCvScalar(255, 255, 255), LineType.FourConnected);
                mask._Dilate(2);
                return(mask);
            }
        }
Exemple #3
0
        public static Image <Gray, byte> GeneralBluredBinaryImage(Image <Bgr, byte> image)
        {
            Gray avg = image.Convert <Gray, byte>().GetAverage();

            using (Image <Bgr, byte> bluredImage = image.CopyBlank())
            {
                CvInvoke.MedianBlur(image, bluredImage, 31);
                using (Image <Gray, byte> mask = bluredImage.Copy().Convert <Gray, byte>())
                {
                    return(mask.ThresholdBinary(new Gray((int)avg.Intensity), new Gray(255)));
                }
            }
        }
Exemple #4
0
        private void RepairColor(ref Image <Bgr, byte> cleanedImage, Image <Gray, byte> grayImage, Image <Gray, byte> generalImageMask, BgrColor bgrColor, double colorTone, CmpType cmpType)
        {
            using (Image <Gray, byte> defectsMask = CreateMaskOfOverInappropriateColorProportions(grayImage, cleanedImage /*_image*/, colorTone, bgrColor, cmpType, _margin))
            {
                using (Image <Gray, byte> repairMask = generalImageMask.Mul(defectsMask))
                {
                    Image <Bgr, byte> cleanedPatchImage = cleanedImage.CopyBlank();
                    cleanedPatchImage[0] = cleanedImage[(int)bgrColor].Mul(BlueTone * 3);
                    cleanedPatchImage[1] = cleanedImage[(int)bgrColor].Mul(GreenTone * 3);
                    cleanedPatchImage[2] = cleanedImage[(int)bgrColor].Mul(RedTone * 3);

                    cleanedImage = MorphologicalProcessing.CombineTwoImages(cleanedImage, cleanedPatchImage, repairMask);
                }
            }
        }
Exemple #5
0
        private Image <Gray, byte> CreateMaskOfOverInappropriateColorProportions(Image <Gray, byte> grayImage, Image <Bgr, byte> image, double tone, BgrColor color, CmpType cmpType, double margin)
        {
            double             interval = 0;
            float              count    = (image?.CountNonzero()[(int)color]) ?? 1;
            float              nonZeroCount;
            Image <Gray, byte> resultMask = grayImage.CopyBlank();

            switch (cmpType)
            {
            case CmpType.LessThan: { interval = Math.Round((1 - margin) * tone, 3); } break;

            case CmpType.GreaterThan: { interval = Math.Round((1 + margin) * tone, 3); } break;

            default: { interval = tone; } break;
            }

            using (Image <Gray, byte> model = grayImage.Mul(interval * 3.0))
            {
                using (Image <Gray, byte> cmpImage = _image[(int)color].Cmp(model[0], cmpType))
                {
                    using (Image <Gray, byte> disColorMask = cmpImage.ThresholdBinary(new Gray(1), new Gray(255)))
                    {
                        Mat kernel = CvInvoke.GetStructuringElement(ElementShape.Ellipse, new Size(3, 3), new Point(-1, -1));
                        CvInvoke.MorphologyEx(disColorMask, resultMask, MorphOp.Open, kernel, new Point(-1, -1), 1, BorderType.Replicate, new MCvScalar(1.0));

                        nonZeroCount = resultMask[0]?.CountNonzero()[0] ?? 0;
                    }
                }
            }

            if (nonZeroCount / count > 0.20 && margin < 1.0)
            {
                margin += 0.05;
                return(CreateMaskOfOverInappropriateColorProportions(grayImage, image, tone, color, cmpType, margin));
            }
            else
            {
                return(resultMask);
            }
        }