Starting with MatLab
Lets start with the basics of the basics i.e.
make an addition. Nothing could be simpler. We must consider that MatLab is a great calculator. In the command window,
we will ask to solve 36 + 44 (I know you've found the answer!).
You just have to type 36 + 44 in the command window and then "enter". The following should appear:
>> 36 + 44
ans =
80
You can also subtract,
multiply or divide.
Few observations at this level. MatLab is an english syntax based software.
The commas are replaced by dots. The sign of multiplication is the star *.
Examples:
>> 34 - 52
ans =
-18
>> 1.5 * 4
ans =
6
>> 12 / 4
ans =
3
You can combine operations.
Examples:
>> 1 - (2 + 5.2) * 10 / (5 ^ 3)
ans =
0.4240
The results should now be stored in memory and be reused. So we will store the results in what are called "variables". To understand the following,
we must treat two very simple concepts.
A variable has a unique case sensitive name. image,
Image and IMAGE are three different variables.
A variable has a type. It determines the content of the variable: it can contain integer,
floating point,
real,
boolean values or otherwise.
For example we will store in the variable named A the value of the operation right of the sign =. Note that ^ is the power setting.
>> A = 1 - (2 + 5.2) * 10 / (5 ^ 3)
A =
0.4240
Equal sign stores the right part into the left one.
We can then recall the contents of the variable A as follows.
>> A
A =
0.4240
A variable contains the value 0.4240.
This result can be reused to make another calculation and store the result in the variable B.
>> B = 2 * A
B =
0.8480
Reading DICOM image
Now that you know how variables work,
we will learn to read a DICOM image. In medical imaging,
images are generated by modalities in a particular format called DICOM. Each DICOM file has the particularity to contain two types of information systematically. The first one named header contains informations about your patient and the exam (name,
age,
modality,
date of examination,
etc.). The second part contains the image itself. It is stored in the form of a matrix and each pixel is encoded as a number. To read a full DICOM image,
we must read these two information.
Change to a folder containing DICOM files (typically those contained on the CD provided to patients).
Type the following command.
Your image is then decoded.
>> dicomread ('image_528.dcm')
Fig. 2: Reading a DICOM image displays the value of each pixel in the matrix.
The image is read but not stored. The exact same previous method works to store image.
The user defines a variable name,
"image1" for example.
>> image1 = dicomread ('image_528.dcm');
The sign ; is used to hide the calculation procedure and to make the Command Window more readable. Look in the Workspace at the top right,
you should see your image. It displays its name,
the matrix size and type.
Display DICOM image
You should now be able to view the contents of your image. There are several commands to do this.
In the example we will use the IMSHOW function.
>> imshow (image1 'DisplayRange',[])
>>
Your image is displayed using the widest possible window.
Fig. 3: User should see this windows opening when calling the function IMSHOW.
Making substraction
In this next session you will learn how to make a substraction computing.
Load two images. The first one should be T1 + gado,
the second one should be a T1 without gado.
Make sure you select the same slice positions.
>> image1 = dicomread ('T1gado.dcm');
>> image2 = dicomread ('T1.dcm');
The procedure is the exact same.
Make two variables with and without contrast.
We will simply perform a subtraction and store the result in variable named image3. We will then display the result on the screen.
>> image3 = image1 - image2;
>> imshow (image3 'DisplayRange',[])
Fig. 4: Load two images with and without constrast in two variables. Substraction is a very easy procedure.
A new windows opens with the substraction image
Computing ADC
On the same principle it is possible to perform more complex calculations. Let's try for example to calculate our own ADC (Apparent Diffusion Coefficient). This requires having a b0 sequence and b1000 sequence. We load the images in the same way as before.
Make shure images are at same slice.
>> imageb0 = dicomread ('b0.dcm');
>> imageb1000 = dicomread ('b1000.dcm');
As previous steps,
images are stored in memory.
We will compute the ADC by solving the formula :
S (b) = S (0).
Exp (-b.ADC)
With:
S(b),
the signal obtained at b1000
S(0),
the signal obtained at b0
Exp,
Exponential
b,
the factor of diffusion (here b = 1000s/mm²)
ADC,
the apparant diffusion coefficient
The computed image is stored in the variable named imageADC
>> imageADC =-log(imageb1000/imageb0) / 1000;
log is the neperian logarithm
Beware the operation can fail !
Indeed,
you can't divide by 0 value.
If a voxel of imageb0 has the value of 0,
the computing will fail.
At this point you must introduce a condition.
For example
>> imageb0(imageb0 = 0) = eps;
This trick replaces 0 value with epsilon (close to 0) value for each pixel concerned.
There are many others methods.
Print the result:
>> imshow (imageADC 'DisplayRange',[])
Fig. 5: Left side the imageb0, center the imageb1000, right side the own computed ADC without been thresholded.
Coloring images
Now,
let's give a little color to our parametric image.
>> imagesc (imageADC)
>> colormap (jet)
Fig. 6: Example of ADC map with colormap.
Thresholding images
We just define a threshold and replace values under threshold with 0.
>> imageb1000(imageb1000 < 300) = 0;
Here,
300 is an arbitrary value choosen for thresholding images.
Fig. 7: Example of thresholding an image with 300 value.
Saving your work
You should now be able to store the results in order to save your work. To do this,
return to the concept mentioned above. A DICOM file consists of two parts. The first one named header contains the information of the review,
the second contains the image. To generate a new DICOM file containing the information of our new image,
we will create a header for each row and fill it. The best way and especially the fastest,
is to retrieve the header of the initial image and change the relevant lines.
To retrieve the contents of the header of our initial DICOM file,
use the function DICOMINFO. This generates a variable format "struct". It is interesting here not to use the character ; at the end of the sentence in order to observe the contents of the header.
>>dicominfo ('b0.dcm')
Fig. 8: DICOMINFO function returns this king of table. Each line contains a the name of the info and its value(s).
We have blured data on our patient. Each line contains a small matrix that itself contains information about the examination or the patient.
We record this information in the variable named imageinfo:
>> imageinfo = dicominfo('b0.dcm');
To recall the content of a line of variable imageinfo,
just write the variable name followed by a dot followed by the name of the requested information.
For example,
expose the name of the sequence of this :
>> imageinfo.SequenceName
ans =
ep_b0
To change this information,
it is sufficient simply to assign a value with the character = as above and add ' and ' around the text.
For example:
>> imageinfo.SequenceName = 'ADC'
Fig. 9: Here we have changer the name of the sequence with ADC.
The last step is to create a new file containing information first and then the images and save all DICOM format so it can be read by many programs. The function DICOMWRITE is used as follows:
>> dicomwrite (imageADC,
'ADC.dcm',
imageinfo)
Here,
we have created the file containing the image imageADC in the file ADC.dcm generated by calculating ADC and the header file containing the information bo.dcm but the name of the sequence was changed.
To complete this presentation,
it is useful to add that all these functions can be automated using loops with FOR funcion and that your work can be saved in files called scripts.
Example:
>> For x = 1:3
'Hello'
end
ans =
hello
ans =
hello
ans =
hello