public static List <sitk.VectorOfParameterMap> PerformMultipleRigidRegistrationForComponents( string fixedSegmentedMask, string movingSegmentedMask, int numberOfComposedTransformations, RegistrationParameters parameters) { // created corresponding masks Image <Gray, byte> fixedSegments = new Image <Gray, byte>(fixedSegmentedMask); Image <Gray, byte> movingSegments = new Image <Gray, byte>(movingSegmentedMask); VectorOfVectorOfPoint contoursFixed = new VectorOfVectorOfPoint(); VectorOfVectorOfPoint contoursMoving = new VectorOfVectorOfPoint(); CvInvoke.FindContours(fixedSegments, contoursFixed, null, Emgu.CV.CvEnum.RetrType.Ccomp, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple); CvInvoke.FindContours(movingSegments, contoursMoving, null, Emgu.CV.CvEnum.RetrType.Ccomp, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple); // retireve dict with contour index and area size ordered by size Dictionary <int, double> contoursFixedDict = GetContourAreaDict(ref contoursFixed); Dictionary <int, double> contoursMovingDict = GetContourAreaDict(ref contoursMoving); List <Tuple <string, string> > filenameOfMaskComponents = new List <Tuple <string, string> >(); for (int i = 0; i <= numberOfComposedTransformations; i++) { var contourFixed = contoursFixed[contoursFixedDict.ElementAt(i).Key]; var contourMoving = contoursMoving[contoursMovingDict.ElementAt(i).Key]; Image <Gray, byte> maskFixed = new Image <Gray, byte>(fixedSegments.Width, fixedSegments.Height, new Gray(0.0)); Image <Gray, byte> maskMoving = new Image <Gray, byte>(movingSegments.Width, movingSegments.Height, new Gray(0.0)); CvInvoke.DrawContours(maskFixed, contourFixed, -1, new MCvScalar(255.0), thickness: -1); CvInvoke.DrawContours(maskMoving, contourMoving, -1, new MCvScalar(255.0), thickness: -1); string filenameFixed = Path.GetTempPath() + "\\fixed_0" + i + ".png"; string filenameMoving = Path.GetTempPath() + "\\moving_0" + i + ".png"; maskFixed.Save(filenameFixed); maskMoving.Save(filenameMoving); Tuple <string, string> temp = new Tuple <string, string>(filenameFixed, filenameMoving); filenameOfMaskComponents.Add(temp); } sitk.ParameterMap map = GetDefaultParameterMap(parameters.RegistrationDefaultParams); List <sitk.VectorOfParameterMap> list = new List <sitk.VectorOfParameterMap>(); foreach (Tuple <string, string> tuple in filenameOfMaskComponents) { sitk.Image img01 = ReadWriteUtils.ReadITKImageFromFile(tuple.Item1); sitk.Image img02 = ReadWriteUtils.ReadITKImageFromFile(tuple.Item2); parameters.ParamMapToUse = map; RigidRegistration reg = new RigidRegistration(img01, img02, parameters); reg.Execute(); sitk.VectorOfParameterMap toAdd = new sitk.VectorOfParameterMap(reg.GetTransformationParameterMap()); list.Add(toAdd); reg.Dispose(); } return(list); }
/// <summary> /// Componentwise rigid registration. /// Each corresponding masked components will be registrated discretly. /// Transformation parameters will be composed befor image is transformed. /// </summary> /// <param name="fixedMask">reference mask</param> /// <param name="movingMask">template mask</param> /// <param name="v">number of corresponding components</param> /// <param name="filename">moving image filename</param> /// <returns></returns> private List <sitk.VectorOfParameterMap> ComponentWiseRigidRegistration(sitk.Image fixedMask, sitk.Image movingMask, int v, string filename) { // convert from sitk to opencv var fixedImage = ReadWriteUtils.ConvertSitkImageToOpenCv <Gray, byte>(fixedMask); var movingImage = ReadWriteUtils.ConvertSitkImageToOpenCv <Gray, byte>(movingMask); // find contours VectorOfVectorOfPoint contoursFixed = new VectorOfVectorOfPoint(); VectorOfVectorOfPoint contoursMoving = new VectorOfVectorOfPoint(); CvInvoke.FindContours(fixedImage, contoursFixed, null, Emgu.CV.CvEnum.RetrType.Ccomp, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple); CvInvoke.FindContours(movingImage, contoursMoving, null, Emgu.CV.CvEnum.RetrType.Ccomp, Emgu.CV.CvEnum.ChainApproxMethod.ChainApproxSimple); // retireve dict with contour index and area size ordered by size var contoursFixedDict = RegistrationUtils.GetContourAreaDict(ref contoursFixed).OrderByDescending(it => it.Value); var contoursMovingDict = RegistrationUtils.GetContourAreaDict(ref contoursMoving).OrderByDescending(it => it.Value); List <Tuple <string, string> > filenameOfMaskComponents = new List <Tuple <string, string> >(); for (int i = 1; i < v; i++) { var contourFixed = contoursFixed[contoursFixedDict.ElementAt(i).Key].ToArray(); var contourMoving = contoursMoving[contoursMovingDict.ElementAt(i).Key].ToArray(); Image <Gray, byte> maskFixed = new Image <Gray, byte>(fixedImage.Width, fixedImage.Height, new Gray(0.0)); Image <Gray, byte> maskMoving = new Image <Gray, byte>(movingImage.Width, movingImage.Height, new Gray(0.0)); maskFixed.Draw(contourFixed, new Gray(255.0), -1); maskMoving.Draw(contourMoving, new Gray(255.0), -1); //CvInvoke.DrawContours(maskFixed, contourFixed, -1, new MCvScalar(255.0), thickness: -1); //CvInvoke.DrawContours(maskMoving, contourMoving, -1, new MCvScalar(255.0), thickness: -1); string filenameFixed = Path.GetTempPath() + "\\fixed_0" + i + ".png"; string filenameMoving = Path.GetTempPath() + "\\moving_0" + i + ".png"; maskFixed.Save(filenameFixed); maskMoving.Save(filenameMoving); Tuple <string, string> temp = new Tuple <string, string>(filenameFixed, filenameMoving); filenameOfMaskComponents.Add(temp); } sitk.ParameterMap map = RegistrationUtils.GetDefaultParameterMap(_parameters.RegistrationDefaultParams); List <sitk.VectorOfParameterMap> list = new List <sitk.VectorOfParameterMap>(); foreach (Tuple <string, string> tuple in filenameOfMaskComponents) { sitk.Image img01 = ReadWriteUtils.ReadITKImageFromFile(tuple.Item1, sitk.PixelIDValueEnum.sitkUInt8); sitk.Image img02 = ReadWriteUtils.ReadITKImageFromFile(tuple.Item2, sitk.PixelIDValueEnum.sitkUInt8); _parameters.ParamMapToUse = map; RigidRegistration reg = new RigidRegistration(img01, img02, _parameters); reg.Execute(); sitk.VectorOfParameterMap toAdd = new sitk.VectorOfParameterMap(reg.GetTransformationParameterMap()); list.Add(toAdd); reg.Dispose(); } return(list); }
/// <summary> /// Runs the actual registration process with default settings. /// </summary> /// <param name="refImage">reference image</param> /// <param name="movImage">template image</param> /// <param name="fixedMask">fixed mask</param> /// <param name="movingMask">moving mask</param> /// <param name="imageFilename">filename of moving image</param> /// <returns></returns> private sitk.VectorOfParameterMap DefaultRigidRegistration(sitk.Image refImage, sitk.Image movImage, sitk.Image fixedMask, sitk.Image movingMask, string imageFilename) { RigidRegistration regRigid = new RigidRegistration(refImage, movImage, _parameters); // set fixed mask if defined if (fixedMask != null) { regRigid.SetFixedMask(fixedMask); } // set moving mask if defined if (movingMask != null) { regRigid.SetMovingMask(movingMask); } ExecuteRegistration(regRigid); sitk.VectorOfParameterMap transformparams = regRigid.GetTransformationParameterMap(); regRigid.Dispose(); return(transformparams); }