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