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))); } } }
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); } }