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?
thanks.
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 });
extract.SetDirectionCollapseToStrategy(ExtractImageFilter.DirectionCollapseToStrategyType.DIRECTIONCOLLAPSETOSUBMATRIX);
newimg = extract.Execute(image);
var origin1 = newimg.GetOrigin();
var direction1 = newimg.GetDirection();
var pixelspace = newimg.GetSpacing();
CastImageFilter castImageFilter = new CastImageFilter();
castImageFilter.SetOutputPixelType(PixelIDValueEnum.sitkInt16);
Image optimg = castImageFilter.Execute(newimg);
optimg.SetMetaData("0010|0010","HYMA");
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", "1.3.12.2.1107.5.2.38.51042.202203031451095637489111.0.0.0");
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.KeepOriginalImageUIDOn();
writer.SetFileName(@"E:\CT_Dicom\Re\image_" + i.ToString() + ".dcm");
writer.Execute(optimg);
}