/// <summary> /// Execute segmentation process of inner structures. /// </summary> public void Execute() { if (image != null) { Image <Bgr, byte> maskedImage = new Image <Bgr, byte>(image.Width, image.Height, new Bgr(255, 255, 255)); CvInvoke.cvCopy(image, maskedImage, particleMask); Console.WriteLine("Image dimensions: " + image.Width + " / " + image.Height); Console.WriteLine("Masked Image dimensions: " + maskedImage.Width + " / " + maskedImage.Height); UMat temp = SegmentationUtils.IncreaseContrast(maskedImage.ToUMat(), 5.0, 8); temp = SegmentationUtils.Blur(temp, 3, 1.0); temp = SegmentationUtils.Sharp(temp); Image <Bgr, byte> tempImage = new Image <Bgr, byte>(temp.Bitmap); UMat uMatChannel = SegmentationUtils.GetColorChannelAsUMat(segmentationParameters.Colorspace, tempImage, segmentationParameters.Channel); if (segmentationParameters.UseKmeans) { KmeansSegmentation(uMatChannel); } else { StandardSegmentation(uMatChannel); } } }
/// <summary> /// Execute segmentation process. /// </summary> public void Execute() { if (image != null) { UMat temp = SegmentationUtils.IncreaseContrast(image.ToUMat(), 5.0, 8); temp = SegmentationUtils.Blur(temp, 3, 1.0); temp = SegmentationUtils.Sharp(temp); Image <Bgr, byte> tempImage = new Image <Bgr, byte>(temp.Bitmap); UMat uMatChannel = SegmentationUtils.GetColorChannelAsUMat(segmentationParameters.Colorspace, tempImage, segmentationParameters.Channel); UMat thresholded = null; using (UMat copy = uMatChannel.Clone()) { if (segmentationParameters.UseOtsu) { thresholded = SegmentationUtils.ThresholdOtsu(copy); } else { thresholded = SegmentationUtils.Threshold(copy, segmentationParameters.Threshold); } } UMat closed = SegmentationUtils.Closing(thresholded, 5); //UMat eroded = SegmentationUtils.Erode(closed, 15); UMat dilated = SegmentationUtils.Dilate(closed, 10); //ReadWriteUtils.WriteUMatToFile(ApplicationContext.OutputPath + "\\dilated.png", dilated); VectorOfVectorOfPoint contours = new VectorOfVectorOfPoint(); VectorOfVectorOfPoint contoursRelevant = new VectorOfVectorOfPoint(); CvInvoke.FindContours(dilated, contours, null, RetrType.External, ChainApproxMethod.ChainApproxSimple); int autoMaxPixelSize = ImageUtils.GetPercentualImagePixelCount(image, 0.03f); for (int i = 0; i < contours.Size; i++) { double area = CvInvoke.ContourArea(contours[i]); if (IsContourRelevant(area, autoMaxPixelSize, segmentationParameters)) { contoursRelevant.Push(contours[i]); } } CvInvoke.DrawContours(image, contoursRelevant, -1, new MCvScalar(0, 0, 255)); mask = new Image <Gray, Byte>(image.Width, image.Height, new Gray(0.0)); CvInvoke.DrawContours(mask, contoursRelevant, -1, new MCvScalar(255.0), thickness: -1); } }