/// <summary> /// Masks an image of the same voxel count as the input doseGrid. Voxels inside the /// contour are filled with the specified color. Intent is white (0x255) on inside and outside /// areas/holes are left black (0x00) /// </summary> /// <param name="mask">the mask to fill or modify (recursively)</param> /// <param name="txMatrix">the patient transform matrix from the imagematrix/dosematrix</param> /// <param name="color">the color to fill the contour</param> /// <param name="scale">the scale of the image (in case the image has been resampled)</param> public void MaskImageFast(Mat mask, Mat txMatrix, byte color = 255, double scale = 1) { if (ContourPoints.Count == 0) { mask.SetArray(new byte[mask.Cols * mask.Rows]); return; } var allPoints = new Vec4f[ContourPoints.Count]; var points = ContourPoints.Select(c => new Vec4f(c.X, c.Y, Z, 1)); using (var pointMat = new Mat <float>(new[] { ContourPoints.Count }, points.ToArray())) { var txPointMat = pointMat.Transform(txMatrix.Inv()); txPointMat.GetArray(out allPoints); } var points2D = allPoints.Select(a => new Point(a.Item0 * scale, a.Item1 * scale)).ToList(); Cv2.FillPoly(mask, new[] { points2D }, new Scalar(color), LineTypes.Link8); var grandchildren = Children.SelectMany(c => c.Children); var outermostChildren = Children.Where(c => !grandchildren.Any(gc => gc == c)); foreach (var child in outermostChildren) { //Recursively alternates black white to account for holes and fills of children contours child.MaskImageFast(mask, txMatrix, color == 255 ? (byte)0 : (byte)255); } }
public Mat DrawOnSlice(Mat txMatrix, Mat slice, double scale = 1) { var allPoints = new Vec4f[ContourPoints.Count]; var points = ContourPoints.Select(c => new Vec4f(c.X, c.Y, Z, 1)); using (var pointMat = new Mat <float>(new[] { ContourPoints.Count }, points.ToArray())) { var txPointMat = pointMat.Transform(txMatrix.Inv()); txPointMat.GetArray(out allPoints); } var points2D = allPoints.Select(a => new Point(a.Item0 * scale, a.Item1 * scale)).ToList(); double min, max; slice.MinMaxIdx(out min, out max); var color = slice.Type() == MatType.CV_8UC3? slice: slice.WindowAndLevel((max - min) / 2, max - min); Cv2.DrawContours(color, new[] { points2D }, 0, new Scalar(255, 255, 0)); var grandchildren = Children.SelectMany(c => c.Children); var outermostChildren = Children.Where(c => !grandchildren.Any(gc => gc == c)); return(color); }