<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Title" content="">
<meta name="Keywords" content="">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:Arial;
panose-1:2 11 6 4 2 2 2 2 2 4;}
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
margin-bottom:.0001pt;
font-size:11.0pt;
font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
{mso-style-priority:99;
color:#0563C1;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:#954F72;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal-reply;
font-family:"Calibri",sans-serif;
color:windowtext;}
span.msoIns
{mso-style-type:export-only;
mso-style-name:"";
text-decoration:underline;
color:teal;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:10.0pt;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style>
</head>
<body bgcolor="white" lang="EN-US" link="#0563C1" vlink="#954F72">
<div class="WordSection1">
<p class="MsoNormal">Hello Po-Yu Kao,<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">The step you are missing is that you need to explicitly copy the meta-data information from your original image, the function is CopyInformation(). This assumes that you did not change any spatial information (padded the numpy array as
part of your processing which implies a change in the spatial region occupied by the image etc.).<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Your code should look something like this:<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">ct_img = sitk.ReadImage(ct_scan_path)<o:p></o:p></p>
<p class="MsoNormal">nda = sitk.GetArrayFromImage(ct_img)<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"># Do some work on the numpy array nda<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">img_preprocessed = sitk.GetImageFromArray(nda_preprocessed)<o:p></o:p></p>
<p class="MsoNormal">img_preprocessed.CopyInformation(ct_img)<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">If you did change some spatial aspects of the image in your numpy code then you will need to set them manually:<o:p></o:p></p>
<p class="MsoNormal">img_preprocessed.SetSpacing(new_spacing)<o:p></o:p></p>
<p class="MsoNormal">img_preprocessed.SetOrigin(new_origin)<o:p></o:p></p>
<p class="MsoNormal">img_preprocessed.SetDirection(new_direction)<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">We are moving away from the mailing to a discourse forum: https://discourse.itk.org/<o:p></o:p></p>
<p class="MsoNormal">Please post future questions on that forum.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"> regards<o:p></o:p></p>
<p class="MsoNormal"> Ziv<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-size:12.0pt;color:black">From: </span></b><span style="font-size:12.0pt;color:black">Po-Yu Kao <poyu_kao@ucsb.edu><br>
<b>Date: </b>Tuesday, November 28, 2017 at 9:41 PM<br>
<b>To: </b>"community@itk.org" <community@itk.org><br>
<b>Subject: </b>[ITK] How to using SimpleITK to access and change the voxel value without touch the metadata<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<div>
<div>
<div>
<div>
<div>
<p class="MsoNormal">Hello,<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">I am using SimpleITK library in python 2.7. <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<p class="MsoNormal">I want to do some basic preprocessing on CT scans with nii.gz. format. For example, I use img = sitk.ReadImage(ct_scan_path) to read the image, and then use nda = sitk.GetArrayFromImage(ct_img) to access n-dimensional numpy array. Then,
I do some preprocessing steps on this n-dimensional numpy, and save it back to the image format by using img_preprocessed = sitk.GetImageFromArray(nda_preprocessed). However, when I check the metadata of these two images, it gives me different values which
are showed below.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Does anyone know how to copy all the metadata from the original image to another image? or Does anyone know how to access and change the voxel value of the image without touching the metadata?<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<p class="MsoNormal">Original Image:<o:p></o:p></p>
</div>
<p class="MsoNormal">print img<br>
<o:p></o:p></p>
<div>
<div>
<p class="MsoNormal">Image (0x4237b40)<br>
RTTI typeinfo: itk::Image<float, 3u><br>
Reference Count: 1<br>
Modified Time: 3601<br>
Debug: Off<br>
Object Name: <br>
Observers: <br>
none<br>
Source: (none)<br>
Source output name: (none)<br>
Release Data: Off<br>
Data Released: False<br>
Global Release Data: Off<br>
PipelineMTime: 3578<br>
UpdateMTime: 3600<br>
RealTimeStamp: 0 seconds <br>
LargestPossibleRegion: <br>
Dimension: 3<br>
Index: [0, 0, 0]<br>
Size: [512, 512, 37]<br>
BufferedRegion: <br>
Dimension: 3<br>
Index: [0, 0, 0]<br>
Size: [512, 512, 37]<br>
RequestedRegion: <br>
Dimension: 3<br>
Index: [0, 0, 0]<br>
Size: [512, 512, 37]<br>
Spacing: [0.488281, 0.488281, 5]<br>
Origin: [-114, 294.012, 21.82]<br>
Direction: <br>
1 0 0<br>
0 -1 0<br>
0 0 1<br>
<br>
IndexToPointMatrix: <br>
0.488281 0 0<br>
0 -0.488281 0<br>
0 0 5<br>
<br>
PointToIndexMatrix: <br>
2.048 0 0<br>
0 -2.048 0<br>
0 0 0.2<br>
<br>
Inverse Direction: <br>
1 0 0<br>
0 -1 0<br>
0 0 1<br>
<br>
PixelContainer: <br>
ImportImageContainer (0x398e780)<br>
RTTI typeinfo: itk::ImportImageContainer<unsigned long, float><br>
Reference Count: 1<br>
Modified Time: 3597<br>
Debug: Off<br>
Object Name: <br>
Observers: <br>
none<br>
Pointer: 0x7f6ef9225010<br>
Container manages memory: true<br>
Size: 9699328<br>
Capacity: 9699328<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Preprocessed Image:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal">Image (0x3857e60)<br>
RTTI typeinfo: itk::Image<float, 3u><br>
Reference Count: 1<br>
Modified Time: 3605<br>
Debug: Off<br>
Object Name: <br>
Observers: <br>
none<br>
Source: (none)<br>
Source output name: (none)<br>
Release Data: Off<br>
Data Released: False<br>
Global Release Data: Off<br>
PipelineMTime: 0<br>
UpdateMTime: 0<br>
RealTimeStamp: 0 seconds <br>
LargestPossibleRegion: <br>
Dimension: 3<br>
Index: [0, 0, 0]<br>
Size: [512, 512, 37]<br>
BufferedRegion: <br>
Dimension: 3<br>
Index: [0, 0, 0]<br>
Size: [512, 512, 37]<br>
RequestedRegion: <br>
Dimension: 3<br>
Index: [0, 0, 0]<br>
Size: [512, 512, 37]<br>
Spacing: [1, 1, 1]<br>
Origin: [0, 0, 0]<br>
Direction: <br>
1 0 0<br>
0 1 0<br>
0 0 1<br>
<br>
IndexToPointMatrix: <br>
1 0 0<br>
0 1 0<br>
0 0 1<br>
<br>
PointToIndexMatrix: <br>
1 0 0<br>
0 1 0<br>
0 0 1<br>
<br>
Inverse Direction: <br>
1 0 0<br>
0 1 0<br>
0 0 1<br>
<br>
PixelContainer: <br>
ImportImageContainer (0x39856c0)<br>
RTTI typeinfo: itk::ImportImageContainer<unsigned long, float><br>
Reference Count: 1<br>
Modified Time: 3606<br>
Debug: Off<br>
Object Name: <br>
Observers: <br>
none<br>
Pointer: 0x7f6ef4823010<br>
Container manages memory: true<br>
Size: 9699328<br>
Capacity: 9699328<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><o:p> </o:p></p>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>