[NOISE]. Now that we've finished theme one an overview of the course, we're ready to start with theme two, data structures and operations. During this theme we're going to be talking about file types of neuro imaging, basic image visualization in R, basic data manipulate in R, and multi sequence structural MRI. Just to give a start of this lecture we're going to be covering different types of file types. The first one is DICOM and the second is NIfTI. These are the two most popular imaging types, and probably the ones you'll be working with most often. We'll also give a little background on PAR/REC images, Analyze images, and NRRD images. You wouldn't expect to see those as often, but they're good to know about in case they come up in your work. So let's dive in to the first lecture of Theme 2 on DICOM images. DICOM stands for Digital Imaging and Communications in Medicine. These are the is the standard representation of images and the format of files that you'd expect to get right of a scanner or from a hospital archive. These hospital archives are called PACS systems and that stands for picture archiving and communication systems. There are two components to a DICOM image, the first one is the image data which will be in pixels and the second is a header which is meta data about the image. Image, that'll contain different things about the scanning parameters used to acquire the image, some properties of the image, and it will also contain information about the patient and the hospital. So if you're not used to working with something like this, you can think of this as a JPEG image with a text file. Where the JPEG image will be your image data in the pixels and the text file will be your header. So let's go through the image file first. If you go into the data that you downloaded for the course. In the neurohacking data file, in the file called brain ex. And then in the file called DICOM, we have an example of four different DICOM images for you to work with. If you open up the first file, the T1, you'll see that this file contains 22 different images. Each one of these images is a slice of a whole brain image. And so if you put all of those together, you'll have one brain image. Each DICOM image consists of a file that has a matrix and one file, one DICOM file is one slice of the brain. We can use the package oro.dicom in R to read this data and this package is hosted on cran and can be easily downloaded. So the first thing we're going to do is go ahead and read in this DICOM image, a single slice of it, so that we can get started in R. So the first line of the code, shown on this slide, sets our working directory. So you'll need to set that to where you're keeping the data, and then put it in the BRAINIX DICOM FLAIR file path. We can use the readDICOM function from oro.dicom, to read in this file. So we're going to assign the variable slice to the output of readDICOM, and then we're going to put the 11th slice of this DICOM in. Now when we read in a DICOM image, these are stored as a list in R. So if we use the class function to see what type of object this is, we see that slice has a class of list. Now the list that we've read into R, will be composed of two separate lists. So it's a list of lists. We have the DICOM header and the image. And the header will be denoted as hdr, and the image is img. Each element of the list header is a data frame. And each element of the list image is a matrix. So let's go ahead and investigate this in R and we can see what I'm talking about. So we look at the names of our DICOM object, or the names of slice, we see that it has element header and an element image. And if we look at the class of slice header, which we can do by using slice$hdr, we see that that's also a list. And if we look at the first element of this list, which you can do using those double brackets that you see here, we see that's a data frame. Now we can do the same thing for the image, but you'll see that the class of slice$img, and if we look at the first element, again with the double brackets, we'll see that's now a matrix. So that's where our data's stored. And if we look at the dimension of this, we can see that we have 288 by 288 pixels. So now we can know that our data is a matrix of 288 by 288 observations. We probably don't just want to look at the objects we're using, we'd actually like to see these images that we're working with. A great function to use in R is the transpose function or the t, and that will just be used to take the transpose of a matrix, which is useful for plotting. So if we find the dimension of our image, and save that as d, we can plot using the image function, which we're going to go through in much more detail in a subsequent lesson. The dimensions in the x direction, the dimensions in the y direction, and then we can use the transpose of one slice of our DICOM image. Well, there was only one size that read in to begin with. And then we changed the color grayscale. And so here we have that subject that we were looking at before with the brain cancer. And one single size of that is plotted here. But as we were saying before, we want to work on analysis of these images so we'd like to see the numbers that correspond to the images. So here we have the same slice that we were showing before. But now we're going to look at the 101st to the 105th voxel and the 121st to the 125th in the y direction of the voxel. So now we have a small five by five matrix that has the numbers from that little box that's shown in the picture on the right. And so we can see, as we were mentioning in the overview lecture, that higher intensities correspond to higher numbers. And lower intensities correspond to lower numbers. Another way that we can work with the numbers in these images is using the histogram function. So in the bottom right-hand corner of this slide, we show a histogram of this entire slice of the image, and so the code to write that is shown to the left. We can use the hist function in R and then we just take the image and we're going to, not subset it like we did before, but plot the entire thing. And then we use 50 breaks in our histogram and we're going to plot prob=true, which makes this a density, instead of account histogram. You can also change the colors and have a lot of fun with these as well. I mean the code is there that you can play with. Okay, so now that we've looked at the image part of the DICOM, let's go to the header. The header contains many fields. Right now we're going to take the first element of the header list from our image that we called slice, and we're going to assign that to the variable header, or hdr. And so if we look at the names of hdr, we have group, element, name, code, link, value and sequence. A lot of these we won't pay attention to but we're very interested in name and value because this will be the name of the item in the list and the value that that takes. And so if you type in your computer, hdr$name, you'll see that there's a 162 items in this particular header. So we won't go to every single one. But a couple of that we might be interested in are pixel spacing and the FlipAngle. So we're going to subset in the next line of R code. We'll subset the header over the pixel spacing, and we'll pick up the value. And so here we see that we get two numbers that are outputted. We get 0.7986, and 0.7986. And what this tells us is that each pixel in this slice has a dimension of 0.7986 millimeters by 0.7986 millimeters. So this tells you the resolution of your image. Then we can also look at the FlipAngle. Now the FlipAngle is one of the image acquisition parameters that choose when you take the scan. And this is often something that you'll want to report if you're writing a paper in your experimental methods section. But you can pull that right out of the DICOM header using the code shown here. And so we could see here that we have a FlipAngle of 4 for this image. Okay, up until this point, we've only been dealing with one slice of the image. But if you want to do any kind of serious image analysis, you're probably going to want to read in the entire image into R at one time. But we can do this too, using the oro.dicom package. So we're going to set our working directory to the folder DICOM. So last time we set it all the way to the file FLAIR. This time we're going to read in the T1, but we're not going to set it to the The folder T1, we're going to send it to the folder DICOM. So now we're going to name the variable all_slices_T1, and we're going to readDICOM. And what we'll do, is we'll just set the input for readDICOM as the final name where this DICOM is stored. And this will read in the entire image. And so if we look at the 11th slice of this image again, last time we were looking at the FLAIR, this time we're looking at the T1. We see that the T1 has a lot larger dimension than our FLAIR image had. This is a 512 by 512 image. And if we look further into the header for this image we see that the pixel dimension is much smaller for this image. So, we have 0.4687 by 0.4687 millimeters. And so this image is acquired as a much higher resolution, so it is a much bigger file. Okay, so let's give a little summary about what we've learned on DICOM images. We know that DICOM files contain a lot of information and that the images are stored as a collection of 2D slice files. We know that different sequences can have different resolutions as we show with the T1 and the FLAIR image. And we know that we can access both the pixel data and the metadata in R. We know that the header from these can contain protected health data such as a patients social security number or information about the hospital. We also know that this kind of structure for the DICOM images can be cumbersome if we want to do analysis of the entire image. And so this sets the stage for a genuine 3D format, or the NIfTI format, that we're going to cover in the next lecture.