Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Computational Methods in Medical Imaging Files, DICOM, Geometry 06/04/2011 09:38 Outline • Patient Imaging. • File formats. • DICOM Reading Image Data Files DICOM Geometry & Coordinate Systems David Atkinson, University College London. [email protected] – – – – – – header info info. reading images, anonymisation, viewing packages, database, Enhanced DICOM format Resources • Geometry – Coordinate systems, – Radiological image presentation, – homogenous coords. – geometrical transforms, – DICOM geometry information, – displaying images in 3D. Imaging Workflow Web page to accompany these lectures http://cmic.cs.ucl.ac.uk/staff/david_atkinson/cmimi/ • Image data digitised at scanner – MRI typically 12 bit ADC, stored as 2 bytes per pixel. Reference data and presentations http://dicom.nema.org/ ftp://medical.nema.org/medical/Dicom/Multiframe/ • Transmitted as a series of bytes, saved as a file along with other information e.g. Patient name. – Image Pixel data and “header” information. – Network protocols Example datasets (Geneva): http://pubimage.hcuge.ch:8080/ Geometrical Transforms and homogeneous coordinates. Foley, van Dam, Feiner, Hughes. Computer Graphics. Chapter 5 • Displayed as 2D images or 3D representation. – Hanging protocols. – Geometry information • Radiologist reports, surgeon plans operation, radiotherapy dose plan calculated. – Patient records Medical Image File Formats Single file Two files • Information and image in one file. • DICOM, gipl, nifti, ... • Information in one file, image in another. • Mayo Analyze .img & .hdr • Nifti • Need to know the file format.... • Image easy to read. • Potential to loose one file. DICOM is a vast set of standards Focus here on format for medical image files, especially geometry information. [NEMA documentation] David Atkinson, UCL 1 Computational Methods in Medical Imaging Files, DICOM, Geometry 06/04/2011 09:38 Non DICOM formats Reading an Unknown File Format • Find the size N of the file in bytes. • Assume the size is dominated by the data from a square y x y image and this data is at the end. • The approx. number of pixels is N/b, where b is the bytes per pixel (typically b=2). b=2) • Take the square root and round down to y, a power of 2, e.g. y=256 • Image is the last y.y.b bytes. 1 • Gipl – Fixed 256 byte header – Originates from Guy’s Hospital – Poorly documented • NifTi – http://nifti.nimh.nih.gov/ – Either dual files .hdr & .img, or single, .nii N Header Image Data y.y.b Simplified DICOM File Structure • Many other formats • Good web site is David Clunie’s and his Medical Image Format FAQ. – http://www.dclunie.com/ htt // d l i / information pixel values P ti tN PatientName ImageOrientationPatient ImagePositionPatient PixelSpacing • Medical imaging dominated by DICOM … PrivateFields DICOM Tags • ACR-NEMA origin • Public and Private Fields DICOM in MATLAB • dicominfo returns the information in a structure • dicomread returns the image data PatientName ImageOrientationPatient ImagePositionPatient info = dicominfo(‘dicomfile’) ; X = dicomread(info) ; PixelSpacing … X2 = dicomread(‘dicomfile’) ; PrivateFields David Atkinson, UCL 2 Computational Methods in Medical Imaging Files, DICOM, Geometry MATLAB example 06/04/2011 09:38 imshow(dicomread('CT-MONO2-16-ankle.dcm'),[]) IEEE 754 float binary representation Binary Representation of an Integer − 1sign × 2exponent −bias × 1 ⋅ mantissa 23 22 21 20 8 4 2 1 1 0 1 1 binary (4 bits) sign exponent bias mantissa single 1 bit 8 bits +127 23 bits double 1 bit 11 bits +1023 52 bits 8 + 0 + 2 + 1 = 11 (decimal) uint16 16 bits = 2 bytes, 216 = 65,536 represents 0 = 65,536 212 = 4096 Calculating original pixel values • Pixels often stored as uint16. • Use rescale slope and intercept to convert to original values. • !!!!! Convert to double first or you may loose negative numbers e.g. Hounsfield units, MR phase values, inversion recovery !!!!! info = dicominfo(‘myfile.dcm’) ; X = dicomread(info) ; S = info.RescaleSlope * double(X) David Atkinson, UCL 32 bits 64 bits Anonymising DICOM How do you effectively anonymise? Is it the “right” right thing to do? – good for data protection / patient confidentiality – leads to data and results that cannot easily be linked back to clinical outcomes etc. – Identification / recognition still possible. + info.RescaleIntercept ; 3 Computational Methods in Medical Imaging Files, DICOM, Geometry 06/04/2011 09:38 Personal data in DICOM header • Supplement 55: Attribute Level Confidentiality (including De-identification) outlines tags and issues ftp://medical.nema.org/medical/dicom/final/sup55_ft.pdf Copying only required info. • See doc for dicomanon in R2010a or higher. • Use my dinfostrip • Name and birthdate are obvious, obvious but there can be others hidden that are not even in this list, or protected by the MATLAB dicomanon function e.g. patient address. • Safest approach is to copy only the image and attributes that you specify – resulting file may no longer be fully DICOM compliant. Managing DICOM Files • Problem: Thousands of files with meaningless names. How do you store, browse and retrieve the ones you want? • Tools – OsiriX: Mac based viewer. – K-PACS: Viewer working under Windows – DCM4CHE: Database and web viewer. – Many others – see David Clunie’s pages David Atkinson, UCL OsiriX • • • • • • Mac based only. Free. Good functionality. Popular with clinicians. FDA approved versions for sale. Allows user to add and write plugins. 4 Computational Methods in Medical Imaging Files, DICOM, Geometry 06/04/2011 09:38 K-PACS • • • • Easy to download and install. http://www.k-pacs.net Free. Can read a CD or connect to a database e.g. DCM4CHEE K-PACS screenshot DCM4CHE • Used ~clinically. Many features. • Supports a web browser. • Can receive from equipment such as MR scanners and interfaces with viewers such as OsiriX and K-PACS. • Free. • Tricky to set up with limited documentation. • Web browser only shows single images. K-PACS screenshot DCM4CHE Installation Notes • Took me about 1 day from scratch to put it on a laptop. • Follow these instructions: http://www.dcm4che.org/confluence/display/ee2/Installation • You will also need (free) : – – – – a database server e.g. mySQL Java Development Kit JBoss Patience and Google to get around problems • Comments on mySQL installation – – – – Use the default settings in installation Allow mySQL in f-SECURE and repeat installation. Needed(?) to allow port 3360 in f-secure firewalls settings. Get error in mySQL installation (installer doesn’t set password apparently) – for fix see http://www.rad89.com/mysql_install.html Dcm4chee web front-end David Atkinson, UCL 5 Computational Methods in Medical Imaging Files, DICOM, Geometry DICOM old vs enhanced 06/04/2011 09:38 Enhanced attributes for MRI • Old style – one file per slice – huge numbers of files. – Important parameters e.g. diffusion weighting, hidden in non-standard Private Fields. • Enhanced DICOM – multi-frame, – better information about 3D and time, – many more parameters in Public Fields (was 2 for MRI, now 94) – raw data archive possible. • Separate gradient and RF echo train lengths • Out-of-plane phase encoding steps • Flow compensation • Spectrally selective excitation & suppression • Blood signal nulling • Tagging • Diffusion values and direction • Spatial saturation slabs l b • Velocity encoding • Chemical shift imaging (metabolite maps) [medical.nema.org] Enhanced DICOM Organizational Features • Multi-frame pixel data • Shared and per-frame functional groups – Each functional group contains attributes that likely vary as a group, e.g. Pixel Measures, Plane Orientation, Velocity Encoding, etc. – Compact & makes explicit what doesn’t change • Dimensions – a priori hints as to how the frames are organized – Specify intended order of traversal, such as space, then time (e.g., for cardiac cine loops) • Stacks – Groups of spatially-related slices, repeatable • Temporal positions [SPIE 2003] [medical.nema.org] Organization of Data • Goal is to reduce the work that the receiving application has to do to “figure out” – How the data is organized – Why it is organized that way • Without preventing use of the data in unanticipated ways – E.g. 3D on a dataset not intended as a volume • Two levels – The detailed shared & per-frame attributes – The overall dimensions, stacks and temporal positions [SPIE 2003] David Atkinson, UCL [medical.nema.org] 6 Computational Methods in Medical Imaging Files, DICOM, Geometry 06/04/2011 09:38 Trigger Temporal Delay Position Time Index Dimensions Stack ID = 1 Start with a dimension of space. A set of contiguous slices through the heart. Add dimension of time (delay time from R-wave). 48 ms 5 2 Sets of contiguous slices throughout cardiac cycle. 4 3 2 1 In-Stack Position Stack ID = 1 Stack ID = 1 Time 0 ms 5 1 5 4 4 3 3 2 2 1 [medical.nema.org] Space In-Stack Position [medical.nema.org] Trigger Temporal Delay Position Time Index Trigger Temporal Delay Position Time Index Stack ID = 1 1\5\2 48 ms 1 Space In-Stack Position Dimension Index Values 5 2 Stack ID = 1 1\5\2 48 ms 4 5 2 4 3 3 2 2 1 1 In-Stack Position Stack ID = 1 1\5\2 1\4\2 1\3\2 1\2\2 1\1\2 In-Stack Position Dimension Index Pointers: 1. Stack ID 2. In-Stack Position 3. Temporal Position Index Dimension Index Pointers: 1. Stack ID 2. In-Stack Position 3. Temporal Position Index Stack ID = 1 Time (2) 0 ms 1 Time (2) 0 ms 5 1 5 4 4 3 3 2 2 1 [medical.nema.org] 1\5\1 1\4\1 1\3\1 1\2\1 1\1\1 Trigger Temporal Delay Position Time Index Stack ID = 1 2\1\5 5 2 4 3 2 1 Stack ID = 1 Stack ID = 1 Dimension Index Values 2\1\5 2\1\4 2\1\3 2\1\2 2\1\1 In-Stack Position 2\1\5 48 ms 5 2 4 3 2 1 In-Stack Position Dimension Index Pointers: 1. Temporal Position Index 2. Stack ID 3. In-Stack Position 1 5 4 3 2 1 In-Stack Position [medical.nema.org] David Atkinson, UCL 1\1\5 1\1\4 1\1\3 1\1\2 1\1\1 Dimension Index Values 2\1\5 2\1\4 2\1\3 2\1\2 2\1\1 Stack ID = 1 Time (1) 0 ms Space (1) In-Stack Position [medical.nema.org] Trigger Temporal Delay Position Time Index 48 ms 1 Space (1) In-Stack Position Dimension Index Values Dimension Index Pointers: 1. Trigger Delay Time 2. Stack ID 3. In-Stack Position Time (1) 0 ms 1 5 4 3 2 1 Space (2) In-Stack Position 1\1\5 1\1\4 1\1\3 1\1\2 1\1\1 Space (2) [medical.nema.org] 7 Computational Methods in Medical Imaging Files, DICOM, Geometry 06/04/2011 09:38 Dimension features • Description of dimensions separate from their indices – Dimensions are described once – Indices within dimensions are encoded per-frame • May be multiple sets of dimensions in one object – E.g., Set S 1 1: space then time, S Set 2 2: time then space • Receiving application only needs to follow the index values – Does NOT need to select or sort by attribute value – Dimensions can be entire functional groups – Dimensions can be private attributes or functional groups [medical.nema.org] Dimension applications • Selection of sort order for simple viewing • Partitioning of frames for “hanging” • Selection of frames that constitute a – volume in space – temporal sequence – contrast administration phase – physiological parameter, e.g. diffusion b value [medical.nema.org] Diffusion Appears to be structure for recording either the diffusion gradient direction, or the full b-matrix in the patient coordinate system. Multi-frame Diffusion: MATLAB • Example Philips file • 13Mb in size, 374 frames • (13010976 bytes, 128x128 images) dinfo.NumberOfFrames 374 X = dicomread(dinfo); size(X) 128 128 1 374 montage(X,’DisplayRange’,[0 200]) Using the dimension feature? >> dinfo.DimensionIndexSequence.Item_1 ans = DimensionOrganizationUID: '1.3.46.670589.11.0.5' DimensionIndexPointer: [32 36950] FunctionalGroupPointer: [32 37137] David Atkinson, UCL 8 Computational Methods in Medical Imaging Files, DICOM, Geometry 06/04/2011 09:38 Using the perFrame... Dinfo.PerFrameFunctionalGroupsSequence ans = Item_1: [1x1 struct] Item_2: [1x1 struct] Item_3: [1x1 struct] Item_4: [1x1 struct] Item_5: [1x1 struct] Item_6: [1x1 struct] ... Item_374: [1x1 struct] Complicated info structure >>dinfo.PerFrameFunctionalGroupsSequence.Item_234.M RDiffusionSequence.Item_1.DiffusionGradientDirectionS equence.Item_1.DiffusionGradientOrientation ans = 0.2626 -0.9548 -0.1389 Unclear how to resolve this issue. Geometrical Information in DICOM Siemens Mosaic • Unofficial (?) format. • Each file is a mosaic (montage) of slices. • One mosaic file per diffusion direction, for example. l • Relatively easy to handle. • Relatively few files. Geometrical Transforms in Therapy • • • • • • Introduction Coordinate systems Homogeneous coordinates Geometrical transforms DICOM file information Display. Overall registration transform is composed of a series of stages Calibration + tracking during intervention M1 Scanner Space 3D Image Space X-ray Table Space λ λT M2 [VisionRT] David Atkinson, UCL Dicom image header M1 Calibration M2, T, P Tracking M3 Patient imaging λ, R R*P X-ray C-arm Space M3 2D Image Space [Kawal Rhode, KCL] 9 Computational Methods in Medical Imaging Files, DICOM, Geometry 06/04/2011 09:38 Right Handed Coordinate System and display x y thumb 1st finger z Example Scanner and Patient Coordinate Systems 2nd finger Radiologist vs Neurosurgeon !!Left-Right swap in image!! L Left P Posterior H Head Z/H X/L For a supine (face-up) patient Neurosurgeon Head = superior (S) Foot = inferior (I) Y/P [www.itk.org] Radiologist Radiographic Presentations Head Coronal Right Head Sagittal Left Anterior Posterior Foot Anterior Right MRI: phase, read, slice Foot Gradient directions can be reversed e.g. to change the direction of a fat shift. This means the prs coordinate system could be left or right handed! phase slice Left Readout (frequency encoding) Axial / Transverse Posterior David Atkinson, UCL View patient from Left foot 10 Computational Methods in Medical Imaging Files, DICOM, Geometry DICOM helps in the face of confusion 06/04/2011 09:38 How do we place DICOM file in 3D? • DICOM LPH and Cartesian axes are both right handed. • Vectors, matrices, trigonometry and coordinates all work “as as expected” expected in any right handed system. • DICOM standard enforces standard geometry information in the file. Machinery: •Coordinate Transforms •Affine transforms •Homogeneous Coordinates •Matrix vector products •Cross product Matrices for Coordinate Transforms • Rotation matrix transforms coordinates of a point by a rotation. b yb ya a θ xb = xa cosθ − ya sin θ xb xa yb = xa sin θ + ya cosθ ⎡ xb ⎤ ⎡cosθ ⎢ y ⎥ = ⎢ sin θ ⎣ b⎦ ⎣ − sin θ ⎤ ⎡ xa ⎤ cosθ ⎥⎦ ⎣⎢ ya ⎥⎦ • In 2D, rotations and scales can be expressed by 2x2 matrices. • However, translations require a “trick”: Homogeneous Coordinates. Coordinates – Add another dimension with value 1 to the coordinates. – Add another row and column to the transform matrix. Homogeneous Coordinates • A convenient way of representing coordinates that allows for geometric transformations including translations. • Add an extra dimension. w P ⎡ xb ⎤ ⎡cos θ ⎢ y ⎥ = ⎢ sin θ ⎣ b⎦ ⎣ − sin θ ⎤ ⎡ xa ⎤ cos θ ⎥⎦ ⎢⎣ ya ⎥⎦ ⎡ xb ⎤ ⎡cos θ ⎢ y ⎥ = ⎢ sin θ ⎢ b⎥ ⎢ ⎢⎣ 1 ⎥⎦ ⎢⎣ 0 − sin θ cos θ 0 0 ⎤ ⎡ xa ⎤ 0⎥⎥ ⎢⎢ ya ⎥⎥ 1⎥⎦ ⎢⎣ 1 ⎥⎦ Rotation matrix Rotation applied to homogeneous coordinate xb = xa + t x y b = ya + t y 1 x [Foley] y David Atkinson, UCL Translations represented this way. ⎡ xb ⎤ ⎡1 0 t x ⎤ ⎡ xa ⎤ ⎢ y ⎥ = ⎢0 1 t ⎥ ⎢ y ⎥ y ⎥⎢ a ⎥ ⎢ b⎥ ⎢ ⎢⎣ 1 ⎥⎦ ⎢⎣0 0 1 ⎥⎦ ⎢⎣ 1 ⎥⎦ 11 Computational Methods in Medical Imaging Files, DICOM, Geometry 06/04/2011 09:38 Alternative representation: (row vector) * matrix Comments • Homogeneous coordinates allow representation of affine transformations. • Transforms can be concatenated into a series of matrix multiplications p ((in the correct order). ⎡ cosθ ya ] ⎢ ⎣− sin θ [xb y b ] = [ xa [xb y b 1] = [xa • Take care assembling the matrix: (row vector)*matrix, or, matrix*(column vector) Affine Transforms • An affine geometrical transform keeps parallel lines parallel. • Includes: – translations, t l ti – rotations, – scale changes, – skews. 0 ⎡1 ⎢0 cosθ Rx = ⎢ ⎢0 sin θ ⎢0 0 ⎣ ⎡cosθ ⎢ sin θ Rz = ⎢ ⎢ 0 ⎢ 0 ⎣ y b 1] = [xa 0 − sin θ cosθ 0 0⎤ 0⎥ ⎥ 0⎥ 1⎥⎦ 0 sin θ 1 0 0 cosθ 0 0 0⎤ 0⎥ ⎥ 0⎥ ⎥ 1⎦ − sin θ cosθ 0 0 0 0 1 0 0⎤ 0⎥ ⎥ 0⎥ 1⎥⎦ Note matrix transpose! ⎡ cosθ ya 1] ⎢ − sin θ ⎢ ⎣⎢ 0 cosθ ⎡⋅ ⎢ ya 1] ⎢ ⋅ ⎢⎣t x 0⎤ ⎥ 0⎥ 1 ⎥⎦ ⋅ ⋅ ty sin θ 0 0⎤ 0⎥ ⎥ 1⎥⎦ Convention used by MATLAB maketform but makehgtform uses the matrix vector convention! 3D Affine Transformation Matrices matrix * (column vector) format ⎡sx ⎢0 scale ⎢ ⎢0 ⎢0 ⎣ 0 sy 0 0 0 0 sz 0 0⎤ 0⎥ ⎥ 0⎥ 1⎥⎦ ⎡1 ⎢λ 2D skew or shear ⎢ y ⎢0 ⎢0 ⎣ … 3D rotations ⎡ cosθ ⎢ 0 Ry = ⎢ ⎢− sin θ ⎢ 0 ⎣ [xb sin θ ⎤ cosθ ⎥⎦ λx 1 0 0 0 0 1 0 0⎤ 0⎥ ⎥ 0⎥ 1 ⎥⎦ ⎡1 ⎢0 translation ⎢ ⎢0 ⎢ ⎣0 0 0 tx ⎤ 1 0 t y ⎥⎥ 0 1 tz ⎥ ⎥ 0 0 1⎦ Applying transforms to coordinates • transform applied to multiple coordinates to transform an object. • in homogeneous coordinates, matrices can be multiplied to compose transforms Positive rotation in a RH coordinate system: when looking towards origin from positive axis, a 90 degree counterclockwise rotation transforms one positive axis into the other. e.g. rotating around x-axis, positive rot is y to z. David Atkinson, UCL 12 Computational Methods in Medical Imaging Files, DICOM, Geometry 06/04/2011 09:38 MATLAB code • Compose transforms into one matrix. • Arrange multiple coordinates into one Transform matrix ⎡ xb ⎤ ⎡ ⋅ ⋅ ⋅ ⎤ ⎡ xa ⎤ ⎢ y ⎥ = ⎢ ⋅ ⋅ ⋅⎥ ⎢ y ⎥ ⎢ b⎥ ⎢ ⎥⎢ a ⎥ ⎣⎢ 1 ⎥⎦ ⎢⎣0 0 1⎥⎦ ⎢⎣ 1 ⎦⎥ ⎡ xb ⎢y ⎢ b ⎣⎢ 1 ub L vb L 1 1 • coord_transform.m One coordinate ⎤ ⎡ ⋅ ⋅ ⋅ ⎤ ⎡ xa ⎥ = ⎢ ⋅ ⋅ ⋅⎥ ⎢ y ⎥ ⎢ ⎥⎢ a ⎦⎥ ⎣⎢0 0 1⎦⎥ ⎣⎢ 1 ua L va L 1 1 ⎤ ⎥ ⎥ ⎦⎥ Multiple coordinates Geometry Information in DICOM DICOM definitions ImagePositionPatient PixelSpacing(1) • DICOM uses a right handed LPH coordinate system. • Relates to patient, not scanner, but relies on radiographer g p correctly y entering g supine/prone. p p • Origin is arbitrary (not isocentre) but fixed. • Nifti uses RAH (also right handed) • Analyze uses LAH (left handed!!) • DICOM provides public fields that relate a 2D image to 3D patient space. Quiz. ImageOrientationPatient(1:3) PixelSpacing(2) ImageOrientationPatient(4:6) PixelSpacing and ImagePositionPatient are in mm ImageOrientationPatient are two unit vectors (direction cosines) Height and Width give number of rows and columns. Finding an Image Pixel Coordinate in LPH IOP(1:3) IOP(4:6) • What are the ImageOrientationPatient vectors for these images (radiological presentation)? P [0 1 0 0 0 -1] [1 0 0 0 0 -1] [1 0 0 0 1 0] H L IPP + 3 * IOP(1:3) * PixelSpacing(2) + 1 * IOP(4:6) * PixelSpacing(1) IPP = ImagePositionPatient IOP = ImageOrientationPatient David Atkinson, UCL 13 Computational Methods in Medical Imaging Files, DICOM, Geometry 06/04/2011 09:38 MATLAB Default Image Coordinates Placing a 2D DICOM image in 3D • [0.5 0.5] To use MATLAB surf, we need the 3D coordinates of the pixel vertices. • Two possible approaches: 1. Starting with ImagePositionPatient, use vector addition 2. Define image coordinates in MATLAB default system, then apply transformation matrices to get 3D patient system. [0.5 1.5] [1 1] Row numbers increase going DOWN [4 1] Image coordinates in [row column] order Pixel centres are integer coordinates. Displaying a 2D image in 3D using surf(X,Y,Z,img) • 2D matrices X,Y and Z contain patient coordinates of the vertices of the patches. • 2D matrix img contains patch “colours”. • Sizes of X,Y and Z are one greater than img in each dimension X(1,1) X(1,2) Y(1,1) img(1,1) g( ) img(1,2) img(2,1) img(2,2) Y(3,1) recap 1) Vector addition 1. Find the 3D patient coord of the top left vertex. 2. Using vector additions, calculate the 3D coordinates of all other vertices vertices. P H L IPP + 3 * IOP(1:3) * PixelSpacing(2) + 1 * IOP(4:6) * PixelSpacing(1) IPP = ImagePositionPatient IOP = ImageOrientationPatient David Atkinson, UCL 14 Computational Methods in Medical Imaging Files, DICOM, Geometry ipp − 0.5 × (arˆ + bcˆ ) Top left vertex in patient coord a = ps(2) b = ps(1) 06/04/2011 09:38 2) Matrix transform from Image to Patient Coords Building transformation matrices: • ImageOrientationPatient – rotation ipp • ImagePositionPatient rˆ = iop(1 : 3) – translation • PixelSpacing – scaling cˆ = iop(4 : 6) • Compose matrices and apply to image coordinates iop = ImageOrientationPatient ipp = ImagePositionPatient ps = PixelSpacing Direction Cosines Direction Cosines x̂ a L θx θy rˆ = [a b] rˆ = axˆ + byˆ r̂ b r̂ rˆ = a 2 + b 2 = 1 ŷ rˆ = cos(θ x )xˆ + cos(θ y )yˆ Rotate Re-label The components of a unit vector (a & b here) are the cosines of the angles the vector makes with the basis directions Direction Cosines and DICOM L r̂ 0 rˆ = [a b] cˆ = [−b a] P Image to Patient Transform: direction cosines as matrix columns rˆ = [a b] cˆ = [−b a] r̂ (0,0) ⎡a ⎤ ⎡a ⋅⎤ ⎡1⎤ rˆ : ⎢ ⎥ = ⎢ ⎥⎢ ⎥ ⎣b ⎦ LP ⎣b ⋅⎦ ⎣0⎦ xy P David Atkinson, UCL x ĉ ⎡− b⎤ ⎡⋅ − b ⎤ ⎡0⎤ cˆ : ⎢ ⎥ = ⎢ ⎥⎢ ⎥ ⎣ a ⎦ LP ⎣⋅ a ⎦ ⎣1⎦ xy y ĉ (1,0) ⎡a − b⎤ ⎢ ⎥ = [rˆ : cˆ :] ⎣b a ⎦ Simplified Image (here pixel sizes 1, origin at top left) 15 Computational Methods in Medical Imaging Files, DICOM, Geometry Composing the overall transform from Image to Patient Direction Cosines in 3D with homogeneous coordinates Rotation matrix composed from row, col. and slice direction cosines as columns sˆ = rˆ × cˆ 06/04/2011 09:38 Image coordinate M = TIPP R S T0 Shift image to make top left voxel centre at (0,0,0) Patient system ⎡ xp ⎤ coordinate ⎡ rdcx cdcx sdcx 0⎤ ⎡ xim ⎤ ⎢ y ⎥ ⎢rdcy cdcy sdcy 0⎥ ⎢ y ⎥ p ⎢ ⎥=⎢ ⎥ ⎢ im ⎥ ⎢ z p ⎥ ⎢ rdcz cdcz sdcz 0⎥ ⎢ zim ⎥ ⎢ ⎥ ⎢ ⎥⎢ ⎥ 0 0 1⎦ ⎣ 1 ⎦ ⎣1⎦ ⎣ 0 Scale using PixelSpacing Rotate into Patient coordinate system using Direction Cosines from ImageOrientationPatient Translate to put top left pixel centre at ImagePositonPatient [See http://www.electromagnetics.biz/DirectionCosines.htm] Applying the transform to multiple coordinates “at once” ⎡ l1 ⎢p ⎢ 1 ⎢ h1 ⎢1 ⎣ l2 p2 h2 1 L⎤ ⎡ x1 ⎥ ⎢y L ⎥ = M⎢ 1 L⎥ ⎢ z1 ⎥ ⎢ ⎦ ⎣1 x2 L⎤ y2 L⎥ ⎥ z 2 L⎥ ⎥ 1 ⎦ Stacking Slices Problem: Multiple 2D slices, each stored as a separate DICOM file – how do you assemble into a 3D matrix? y on file naming. g • Do not rely • Find the through-slice direction using the vector product n = IOP(1:3) x IOP(4:6) • For each file, compute the component of IPP in this through-slice direction (n.IPP) and sort. David Atkinson, UCL Having calculated the coordinates of the vertices, use surf to display. Allows interactive spinning of 3D plots. plots (Camera toolbar) Cross Product gives slice normal • Cross product of IOP vectors gives through slice direction, correctly in a RH coordinate system. +ve ? 16 Computational Methods in Medical Imaging Files, DICOM, Geometry Geometry Fields in Multi-frame DICOM ImageOrientationPatient SharedFunctionalGroupsSequence.Item_1.PlaneOrientationSequence.Item_1.ImageOrientationPatient I ImagePositionPatient P iti P ti t PerFrameFunctionalGroupsSequence.Item_168.PlanePositionSequence.Item_1.ImagePositionPatient (ignore specific Item numbers here) Comments • ImageOrientationPatient, ImagePositionPatient and PixelSpacing are enough to specify a slice in 3D. • Take care putting slices into a 3D file format such as gipl, Analyze, Nifti. Compute the through-slice direction from the cross product of the row and column directions. Sort slices along this direction. • Ignore file alphabetical ordering e.g. from dir 06/04/2011 09:38 Diffusion Gradient in multi-frame dinfo = dicominfo(filename); dinfo.PerFrameFunctionalGroupsSe quence.Item_10.MRDiffusionSeque 10 iff i S nce.Item_XX.DiffusionGradientDi rectionSequence.Item_1.Diffusio nGradientOrientation Coordinate and Rotation Origins • Coordinate origin usually does not coincide with image centre. • Affine R matrix assumes rotation about g coordinate origin. • Recipe: Translate image such that origin is at centre, apply rotation, translate back. These can be composed, note order is important. Fibre orientation in diffusion • Easiest is to stick in the patient system and use vector addition to IPP. • In this scheme, you do not need to worry about MATLAB vs image coordinates coordinates, vertices, rotations etc David Atkinson, UCL • Compute tensor from Diffusion weighted images and gradient directions. • Compute principal eigenvectors. • Display or track these vectors. 17 Computational Methods in Medical Imaging Files, DICOM, Geometry 06/04/2011 09:38 Fibre orientation in diffusion • The eigenvectors are in the gradient coordinate system. – In DICOM coords for newer files. – Otherwise, could be physical gradients or phase, read, slice which rotate with the slice orientation.. • The image is probably displayed in the DICOM coordinate system. • The next pixel in the direction of the principal eigenvector has to be taken from the image volume (stored in the image coordinate system). • Check by following fibres around known corners. Case Study • Clinician gives you a CD of files, mostly DICOM but also some image files, notes etc. • DICOM files have meaningless names, names may not be in slice order and span a number of scans. • How do you process these? Example code 1. Take an initial look using DICOM viewing software to note scan numbers. 2. In MATLAB, create a structure array with each element corresponding p g to a DICOM file. 3. Use find on the array to locate the relevant entries for the scan you require. 4. Read in the corresponding images. 5. Using the geometry information, sort slices into the correct order. David Atkinson, UCL • dicomprocess.m processes a DICOM dir • isdicom_da.m modification to MATLAB’s MATLAB s isdicom.m isdicom m which can fail for very small files. • dicom2vol.m put slices in order • Note, this is working code, not optimised for teaching. 18 Computational Methods in Medical Imaging Files, DICOM, Geometry David Atkinson, UCL 06/04/2011 09:38 19 Computational Methods in Medical Imaging Files, DICOM, Geometry 06/04/2011 09:38 Tutorial • Download and unzip COMUNIX.zip PET and CT DICOM files from David Atkinson’s course web page. • Look through the CT and PET datasets datasets. A possible way to do this is: dirout = dir('*.dcm') ; fns = {dirout.name}' ; montage(fns,'DisplayRange',[]) David Atkinson, UCL 20 Computational Methods in Medical Imaging Files, DICOM, Geometry • Are the CT and PET slices in the same order? • Are the CT and PET slices parallel? • Is I the th tumour t on the th patient ti t left l ft or right? i ht? • Note the PET slice number containing the approximate centre of the tumour. • Note its anatomical location in the head foot direction and find the CT slice containing the tumour. 06/04/2011 09:38 • Estimate the location of the tumour centre in patient coordinates. • Read through the file petct_display.m, modify to produce an image where the tumour is at the intersection of slices: PET montage Geometry: Coordinates and Vectors y (4,2,1) r x z r = 4xˆ + 2yˆ + zˆ r = 4ˆi + 2ˆj + kˆ CT montage David Atkinson, UCL 21 Computational Methods in Medical Imaging Files, DICOM, Geometry 06/04/2011 09:38 Projections Scalar Product (dot product, inner product) a = a aˆ b = b bˆ ĵ r.ˆj = r ˆj cos(θ ) a ⋅ b = a b cosθ r θ a cosθ b r length of vector r ˆj = 1 (unit vector with length 1) θ a b cosθ dot (or scalar) product is length of projection of r onto unit vector j [MathWorld] Scalar product. notation Vector Length: Norm u = u1ˆi + u2 ˆj + u3kˆ r = 4ˆi + 2ˆj + kˆ v = v1ˆi + v2 ˆj + v3kˆ r ⇒ r 2 = 4 2 + 2 2 + 12 ˆj = 0 2 + 12 + 0 2 = 1 2 3 u ⋅ v = ∑ ui vi = u1v1 + u2v2 + u3v3 i =1 Vector Cross product • Cross product of two vectors u and v is a third vector perpendicular to u and v. • E.g. z axis is cross product of x and y. u× v Vector product u× v u u = u1ˆi + u2 ˆj + u3kˆ v = v1ˆi + v2 ˆj + v3kˆ θ v u × v = ˆi (u y v z − u z v y ) + ˆj(u z v x − u x v z ) + kˆ (u x v y − u y v x ) u u × v = ( u v sin θ )nˆ θ v David Atkinson, UCL 22