public RigidRegistration(sitk.Image fixedImage, sitk.Image movingImage, RegistrationParameters parameters) : base(parameters)
        {
            this.fixedImage  = ImageUtils.CastImageToFloat32(fixedImage);
            this.movingImage = ImageUtils.CastImageToFloat32(movingImage);

            this.registrationParameters = parameters;

            elastix = new sitk.ElastixImageFilter();
            if (parameterMap == null)
            {
                parameterMap = RegistrationUtils.GetDefaultParameterMap(parameters.RegistrationDefaultParams);
            }

            // set output dir
            outputDirectory = Path.Combine(registrationParameters.OutputDirectory, registrationParameters.Iteration.ToString());
            if (!Directory.Exists(outputDirectory))
            {
                Directory.CreateDirectory(outputDirectory);
            }
            elastix.SetOutputDirectory(outputDirectory);
            //elastix.SetLogFileName(Path.Combine(outputDirectory, registrationParameters.ElastixLogFileName));
            elastix.LogToFileOn();

            //base.SetGeneralParameters();
        }
        /// <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);
        }
        public NonRigidRegistration(sitk.Image fixedImage, sitk.Image movingImage, RegistrationParameters parameters) : base(parameters)
        {
            // cast images to from pixel type uint to vector of float
            sitk.CastImageFilter castImageFilter = new sitk.CastImageFilter();
            castImageFilter.SetOutputPixelType(sitk.PixelIDValueEnum.sitkVectorFloat32);
            sitk.Image vector1 = castImageFilter.Execute(fixedImage);
            sitk.Image vector2 = castImageFilter.Execute(movingImage);

            sitk.Image tempImage1 = TransformationUtils.GetColorChannelAsImage(vector1, ColorChannel.R);
            sitk.Image tempImage2 = TransformationUtils.GetColorChannelAsImage(vector2, ColorChannel.R);

            this.fixedImage  = tempImage1;
            this.movingImage = tempImage2;

            // initiate elastix and set default registration params
            elastix = new sitk.ElastixImageFilter();
            if (parameterMap == null)
            {
                parameterMap = RegistrationUtils.GetDefaultParameterMap(parameters.RegistrationDefaultParams);
            }

            // coefficient map is used for penalty term
            if (parameters.CoefficientMapFilename != null && parameters.NonRigidOptions == MaskedNonRigidRegistrationOptions.BsplineWithPenaltyTermAndCoefficientMap)
            {
                RegistrationUtils.ChangeOrAddParamIfNotExist(ref parameterMap, "DilateRigidityImages", RegistrationUtils.GetVectorString("true"));
                RegistrationUtils.ChangeOrAddParamIfNotExist(ref parameterMap, "FixedRigidityImageName", RegistrationUtils.GetVectorString(parameters.CoefficientMapFilename));
            }

            // coefficient map is used for diffuse registration
            if (parameters.CoefficientMapFilename != null && parameters.NonRigidOptions == MaskedNonRigidRegistrationOptions.DiffuseRegistration)
            {
                RegistrationUtils.ChangeOrAddParamIfNotExist(ref parameterMap, "UseMovingSegmentation", RegistrationUtils.GetVectorString("true"));
                RegistrationUtils.ChangeOrAddParamIfNotExist(ref parameterMap, "MovingSegmentationFileName", RegistrationUtils.GetVectorString(parameters.CoefficientMapFilename));
            }

            // set output dir and log file
            outputDirectory = Path.Combine(registrationParameters.OutputDirectory, registrationParameters.Iteration.ToString());
            if (!Directory.Exists(outputDirectory))
            {
                Directory.CreateDirectory(outputDirectory);
            }
            elastix.SetOutputDirectory(outputDirectory);
            elastix.SetLogFileName(outputDirectory + registrationParameters.ElastixLogFileName);
            elastix.LogToConsoleOn();
        }
 public RigidRegistration(RegistrationParameters parameters) : base(parameters)
 {
     elastix      = new sitk.ElastixImageFilter();
     parameterMap = RegistrationUtils.GetDefaultParameterMap(parameters.RegistrationDefaultParams);
 }