private Image <Bgr, byte> AligneColor(Image <Bgr, byte> image) { using (Image <Gray, byte> grayImage = image.Convert <Gray, byte>().Copy()) { image[(int)BgrColor.Blue] = grayImage.Mul(BlueTone * 3); image[(int)BgrColor.Green] = grayImage.Mul(GreenTone * 3); image[(int)BgrColor.Red] = grayImage.Mul(RedTone * 3); } return(image); }
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); } } }
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); } }