SimpleITK write Image to Dicom

I had read a series Dicom in Image class and resample the pixel to 1x1x1. I want to write the resampled Image to Dicom. But I only found the example in python. In C#, how should I split the 3D image to 2D image like image[:,:,i] in python? Or how can I write the Dicom in C# slice by slice?


Hello @jeffppp,

You are on the right path. Follow the Python and R example, just replace the bracket operator with the use of the ExtractImageFilter.

Thanks for your help.I can write Image to dicom, but I have another problem.
I’m not sure why I get the strange image position. I want to write [0,0,1], [0,0,2]…
But I get the the same value [0;2.5464e-313;4.3501e-312],[0;2.5464e-313;4.3501e-312] …

How should I change my code?

var idx = optimg.TransformIndexToPhysicalPoint(new VectorInt64() { 0, 0, i }).ToArray(); nImage.SetMetaData(“0020|0032”, string.Format(“{0},{1},{2}”,idx[0], idx[1], idx[2]));

I had tried this

nImage.SetMetaData(“0020|0032”, “0,0,”,i.ToString());

But the results are the same. QQ

Hello @jeffppp,

Setting the Image Position Patient attribute,0020|0032, will not have an effect as it is obtained directly from the image, its origin, when written to file. If you resampled the original volume correctly, then when you use the ExtractImageFilter the slice origin is valid and when you write it to file the Image Position Patient attribute will have the correct value.

Please confirm that the resampled volume has the expected origin, spacing and direction cosine matrix (GetOrigin(), GetSpacing, GetDirection). Then check the origin of the image obtained from the ExtractImageFilter, it should match what you expect without you needing to set anything explicitly.

Hello @zivy

I used another method to test my code.
I read the dicom to the Image. And I didn’t do anything.
Just write the Image to dicom directly.
I had checked the extracted Images, the parameters (GetOrigin() , GetSpacing , GetDirection ). seems normal.
But I check the dicom metadata in matlab, the ImageOrientationPatient is [1;0;0;0;1;0], and the ImagePositionPatient is [-118.1749;2.2493e-312;9.5065e-312] <== just the first number is normal.

How can I fix this problem?


ExtractImageFilter extract = new ExtractImageFilter();
            Image newimg = new Image(image.GetSize()[0], image.GetSize()[1], PixelIDValueEnum.sitkFloat32);
            for (int i=0;i<176;i++)

                extract.SetSize(new VectorUInt32() { image.GetSize()[0], image.GetSize()[1] , 0});
                extract.SetIndex(new VectorInt32() { 0, 0, i });
                newimg = extract.Execute(image);
                var origin1 = newimg.GetOrigin();
                var direction1 = newimg.GetDirection();
                var pixelspace = newimg.GetSpacing();
                CastImageFilter castImageFilter = new CastImageFilter();
                Image optimg = castImageFilter.Execute(newimg);
                optimg.SetMetaData("0010|0020", "T123");
                optimg.SetMetaData("0010|0030", "19870602");
                optimg.SetMetaData("0020|000D", "1.2.840.113619.2.182.10124123185175.1646284464.128677");
                optimg.SetMetaData("0020|0010", "223331417X01");
                optimg.SetMetaData("0008|0020", "20230107");
                optimg.SetMetaData("0008|0030", "135745.312000");
                optimg.SetMetaData("0008|0050", "223331417X01");
                optimg.SetMetaData("0008|0060", "MR");
                optimg.SetMetaData("0008|0031", "145118.859000");
                optimg.SetMetaData("0008|0021", "20220303");
                optimg.SetMetaData("0008|0008", "DERIVED\\SECONDARY");
                optimg.SetMetaData("0008|0012", "145118.859000");
                optimg.SetMetaData("0008|0013", "20220303");
                optimg.SetMetaData("0020|000e", "");
                optimg.SetMetaData("0020|0032", (image.TransformContinuousIndexToPhysicalPoint(new VectorDouble() {0,0,i })[0]).ToString()+";"+ (image.TransformContinuousIndexToPhysicalPoint(new VectorDouble() { 0, 0, i })[1]).ToString()+";"+ (image.TransformContinuousIndexToPhysicalPoint(new VectorDouble() { 0, 0, i })[2]).ToString());
                optimg.SetMetaData("0020|0013", (i+1).ToString());
                //optimg.SetMetaData("0020|0037", image.GetDirection()[0].ToString()+","+ image.GetDirection()[3].ToString()+","+ image.GetDirection()[6].ToString()+"," + image.GetDirection()[1].ToString() + "," + image.GetDirection()[4].ToString() + "," + image.GetDirection()[7].ToString());
                optimg.SetMetaData("0020|0037", "0.993076233113407,0.0244321788116105,-0.1149028453251,-0.0242702610276,0.999701489765079,0.00280816508323");

                ImageFileWriter writer = new ImageFileWriter();
                writer.SetFileName(@"E:\CT_Dicom\Re\image_" + i.ToString() + ".dcm");