/// <summary> /// Get the coefficient map of inner structures of tissue for the transform rigidity penalty term. /// </summary> /// <param name="filename">image filename</param> /// <returns>return coefficient map filename</returns> private string GetInnerStructureSegmentationsAsCoefficientMap(string filename) { InnerTissueSegmentation innerSegImage = GetInnerStructureSegmentation(filename); string filenameCoefficientMap = ReadWriteUtils.GetOutputDirectory(_parameters, _parameters.Iteration) + Constants.cCoefficientFilename; ReadWriteUtils.WriteUMatToFile(filenameCoefficientMap, innerSegImage.GetOutput().FirstOrDefault()); innerSegImage.Dispose(); // rescale image sitk.Image img = ReadWriteUtils.ReadITKImageFromFile(filenameCoefficientMap); sitk.CastImageFilter castFilter = new sitk.CastImageFilter(); castFilter.SetOutputPixelType(sitk.PixelIDValueEnum.sitkFloat32); img = castFilter.Execute(img); sitk.RescaleIntensityImageFilter filter = new sitk.RescaleIntensityImageFilter(); filter.SetOutputMinimum(0.0); filter.SetOutputMaximum(1.0); sitk.Image coefficientMap = filter.Execute(img); // save as mhd filenameCoefficientMap = ReadWriteUtils.GetOutputDirectory(_parameters, _parameters.Iteration) + Constants.cCoefficientFilename; ReadWriteUtils.WriteSitkImage(coefficientMap, filenameCoefficientMap); coefficientMap.Dispose(); return(filenameCoefficientMap); }
public static sitk.Image GetCheckerBoard(sitk.Image img01, sitk.Image img02, uint size = 0) { uint width = img01.GetWidth() > img02.GetWidth() ? img01.GetWidth() : img02.GetWidth(); uint height = img01.GetHeight() > img02.GetHeight() ? img01.GetHeight() : img02.GetHeight(); //Console.WriteLine(string.Format("width: Img01 [{0}] - Img02 [{1}]", img01.GetWidth(), img02.GetWidth())); //Console.WriteLine(string.Format("height: Img01 [{0}] - Img02 [{1}]", img01.GetHeight(), img02.GetHeight())); //Console.WriteLine(string.Format("pixel type: Img01 [{0}] - Img02 [{1}]", img01.GetPixelIDTypeAsString(), img02.GetPixelIDTypeAsString())); sitk.Image reference = ImageUtils.ResizeImage(img01, width, height, sitk.PixelIDValueEnum.sitkFloat32); sitk.Image transformed = ImageUtils.ResizeImage(img02, width, height, sitk.PixelIDValueEnum.sitkFloat32); sitk.CheckerBoardImageFilter checkerBoard = new sitk.CheckerBoardImageFilter(); if (size != 0) { sitk.VectorUInt32 vec = new sitk.VectorUInt32(); vec.Add(size); vec.Add(size); checkerBoard.SetCheckerPattern(vec); } sitk.Image temp = checkerBoard.Execute(reference, transformed); sitk.CastImageFilter castImageFilter = new sitk.CastImageFilter(); castImageFilter.SetOutputPixelType(sitk.PixelIDValueEnum.sitkUInt8); sitk.Image result = castImageFilter.Execute(temp); temp.Dispose(); return(result); }
/// <summary> /// Write transformed file to disk. /// </summary> /// <param name="imagename">filename</param> public static void WriteTransformedImage(sitk.Image img, string fullFilename) { sitk.CastImageFilter castImageFilter = new sitk.CastImageFilter(); castImageFilter.SetOutputPixelType(sitk.PixelIDValueEnum.sitkVectorUInt8); sitk.Image temp = castImageFilter.Execute(img); sitk.ImageFileWriter writer = new sitk.ImageFileWriter(); writer.SetFileName(fullFilename); writer.Execute(temp); }
/// <summary> /// Write transformed image to disk. /// </summary> /// <param name="imagename">image filename</param> public void WriteTransformedImage(string imagename) { sitk.CastImageFilter castImageFilter = new sitk.CastImageFilter(); castImageFilter.SetOutputPixelType(sitk.PixelIDValueEnum.sitkVectorUInt8); sitk.Image temp = castImageFilter.Execute(transformedImage); sitk.ImageFileWriter writer = new sitk.ImageFileWriter(); writer.SetFileName(Path.Combine(registrationParameters.OutputDirectory, imagename)); writer.Execute(temp); }
/// <summary> /// Set moving mask for registration process. /// </summary> /// <param name="movingMask">moving mask</param> public virtual void SetMovingMask(sitk.Image movingMask) { try { sitk.CastImageFilter castImageFilter = new sitk.CastImageFilter(); castImageFilter.SetOutputPixelType(sitk.PixelIDValueEnum.sitkUInt8); this.movingMask = castImageFilter.Execute(movingMask); } catch (Exception) { // ignore when mask of pixeltype uint8 } }
/// <summary> /// Cast image to float 32. /// </summary> /// <param name="img">image</param> /// <returns>casted image</returns> public static sitk.Image CastImageToFloat32(sitk.Image img) { sitk.CastImageFilter castImageFilter = new sitk.CastImageFilter(); castImageFilter.SetOutputPixelType(sitk.PixelIDValueEnum.sitkVectorFloat32); sitk.Image vector = castImageFilter.Execute(img); sitk.VectorIndexSelectionCastImageFilter vectorFilter = new sitk.VectorIndexSelectionCastImageFilter(); sitk.Image tempImage = vectorFilter.Execute(vector, 0, sitk.PixelIDValueEnum.sitkFloat32); castImageFilter.Dispose(); vector.Dispose(); return(tempImage); }
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 static sitk.Image BinaryThinning(sitk.Image inputImage) { sitk.VectorIndexSelectionCastImageFilter vectorIndexSelectionCastImageFilter = new sitk.VectorIndexSelectionCastImageFilter(); sitk.CastImageFilter castImageFilter = new sitk.CastImageFilter(); sitk.Image image = vectorIndexSelectionCastImageFilter.Execute(inputImage, 0, castImageFilter.GetOutputPixelType()); sitk.BinaryThresholdImageFilter binaryThresholdImageFilter = new sitk.BinaryThresholdImageFilter(); binaryThresholdImageFilter.SetInsideValue(0); binaryThresholdImageFilter.SetOutsideValue(255); binaryThresholdImageFilter.SetLowerThreshold(0); binaryThresholdImageFilter.SetUpperThreshold(100); sitk.Image binaryThresholdImage = binaryThresholdImageFilter.Execute(image); sitk.BinaryThinningImageFilter binaryThinningImageFilter = new sitk.BinaryThinningImageFilter(); sitk.Image binaryThinningImage = binaryThinningImageFilter.Execute(binaryThresholdImage); sitk.RescaleIntensityImageFilter rescaleIntensityImageFilter = new sitk.RescaleIntensityImageFilter(); rescaleIntensityImageFilter.SetOutputMinimum(0); rescaleIntensityImageFilter.SetOutputMaximum(255); sitk.Image rescaleIntensityImage = rescaleIntensityImageFilter.Execute(binaryThinningImage); return(rescaleIntensityImage); }