Brain Tumor MRI Detection Using Matlab

By: Madhumita Kannan, Henry Nguyen, Ashley Urrutia Avila, Mei Jin

This MATLAB code is a program to detect the exact size, shape, and location of a tumor found in a patient’s brain MRI scans. This program is designed to originally work with tumor detection in brain MRI scans, but it can also be used for cancer diagnostics in other organ scans as well.

The following instructions will first describe the methods for image analysis through filtering and cleaning up the MRI scan, through binarizing, median filtering, and sliding windows. Next, it will instruct on how to isolate the tumor using a pre-generated elliptical mask, and filtering it further to outline the perimeter of the shape of the tumor.

Once the tumor is detected, the instructions will further describe how to incorporate this program into a graphical user interface (GUI). Throughout these instructions, the appropriate code and files will be attached to help explain how this MRI scan analysis works.

Some things to know, download, and have ready before you proceed with this instructable:
1. Make sure to have the latest version of MATLAB downloaded. You can install R2018b here:

2. In order to run this program, you need to have access to MRI brain scan files. Although some can always be found from Google images, thorough and accurate analysis can be performed from proper images of various layers of brain scans for each patient. You can access the files for 20 different patients with glioblastoma pre and post treatment from this database:

3. The focus of this program and the various methods that guide this project are outlined in this research paper:

Step 1: Initiate Graphical User Interface (GUI)

The first step would be to create and initiate the graphical user interface, GUI. This could be done by typing guide into the command window, pressing enter, and creating a new GUI. Once this step is completed you can begin to create functions such as axes, static text, edit text, and push buttons that will be displayed once the program is run and the user can interact with. These functions can be edited and manipulated through the property inspector, however the most important feature that must be altered when creating these functions is the Tag name. It is important to change the Tag name of each function that is implemented because it will allow us to create a distinguishable callback function. Once you are satisfied with the layout of your GUI you can move on to loading the DICOM files that will be displayed within the GUI.

Step 2: Loading and Reading MRI Images in MATLAB

In order to load the DICOM files you would have to correctly initialize the call back function that would be executed when pressing the button “Load MRI Image.” Once this is completed, you must create a global variable that would display the image on the handles axes where you want the Original MRI Image to be displayed. The MRI scan images downloaded from the database are all DICOM formatted files that need to be loaded into your MATLAB directory. Locate the file using imgetfile in order to load them into the program. The images are read using the built in MATLAB function ‘dicomread’, and the first raw image for each file is imbedded into the left GUI axes using imshow.

The built in MATLAB function ‘dicominfo’ is also extremely useful in addressing all the information of each MRI dicom file. We utilized this function to extract all the descriptive information of the patients, such as their sex, age, weight, and height. This function also provides you with the stack order which is useful for implementation of the program within the graphical user interface. We created variables for each of the descriptive information of the patients which will be used for the GUI when the detect button is pressed.

Step 3: Image Filtering

Once the DICOM file of the raw image has been loaded and read, the image needs to be converted from grayscale into a binarized form consisting of only black and white pixels We used the function ‘imbinarize’ to create a binary image from the raw image by controlling aspects of adaptive thresholding at sensitivity value of 0.59. The default threshold sensitivity factor, 0.5 was low and unable to detect the brighter blobs and spots from the image, so we increased it to 0.59.

The binarized image is then processed through a median filter using the function ‘medfilt2’ because the binarized image is two-dimensional. We set each output pixel to contain the median value in the 5 x 5 neighborhood around the corresponding pixel in the input binarized image. This reduces the noise and preserves the edges in a 5 x 5 square around each pixel. Next, we apply a sliding window using ‘strel’, to create a disk shaped flat structuring element with a neighborhood radius of 2 to identify each central, origin pixel, in each disk neighborhood. We utilized a disk structuring element because we are analyzing each circular spot and the pixels within each spot, so a disk shape element is more useful.

Once the image has been filtered, it can be cleaned using the ‘imclose’ function to remove the black spots in between the filtered white pixels in the image, and closes all the gaps around it. The completely processed image can then be plotted in the second subplot of the pre-allocated figure, allowing a comparison between the raw and filtered image.

Step 4: Tumor Isolation Through Elliptical Mask

The bright spots of the tumor can then be isolated from the main filtered image through a pre-generated elliptical mask. To create this mask, you should know the size of the original, raw MRI scan image, and using it’s row and column length, as x and y- coordinates respectively, allocate the center coordinates for the elliptical. We set the y-axis as a major axis with a radius of 50 units from the center, and the minor axis with a radius of 40 units from the center.

We used the MATLAB function ‘meshgrid’ to generate a cartesian plane with two dimensional grid coordinates based on the coordinates contained in vectors from 1 to the length of the x-axis, and from 1 to the length of the y-axis of the image. Col is a matrix where each row is a copy of the x-axis, and Row is a matrix where each column is a copy of the y-axis. The cartesian grid represented by the coordinates Col and Row has length(1:Y_Size) rows and length(1:X_Size) columns. Use the indices of Col and Row generated by the cartesian grid to determine the equation of ellipse depending on the predetermined radius and center coordinates. The elliptical outline can now be filled with the white pixels found from tumor spots.

Utilizing the pre-generated elliptical mask we can crop out the specific tumor you wish to analyze from the filtered image. The elliptical mask detects which spots logically fit within the outline of the ellipse and accepts this as a spot on the filtered image to be acceptable as a tumor. The function ‘bwareafilt’ then filters out all other objects outside this detected tumor from the image. We used a specific window of 500 by 4000 empirically based on the dimensions of all the images. We then applied another sliding window with ‘strel’ as a flat disk shaped structuring element of a greater neighborhood radius of 6, to close the gaps between each central white pixel within the detected tumor. The detected tumor spot is further cleaned up using ‘imclose’ to further eliminate the black pixels and fill in all the holes with ‘imfill’. This processed tumor can then be displayed in the third subplot in the preallocated plot to provide a comparison between the isolated tumor and the original and filtered images of the MRI scan.

Step 5: Tumor Outlining

Now that the tumor is isolated with the mask, it can be be outlined and displayed on the original image, to show its exact location. To do this, we used the function ‘bwboundaries’ to trace the previously detected tumor with an outline. We specified the outline to not include the holes within the tumor object as it is being outlined. This can be plotted onto the original, raw image, using a ‘for’ loop that plots the outline around the tumor using the indices of the line with a line width of 1.5 pixels. This outline is then plotted onto the raw image, showing the exact size and location of the tumor, relative to the original MRI scan.

Step 6: Analyzing the Physical Properties of Tumors

The isolated and outlined spot can provide us with useful information about the size, area, and location of the tumor. We used the ‘regionprops’ function to detect the properties of the tumor that pertain to area, perimeter, centroids, and the pixel index value. This pixel index value gives us the real world units for each pixel of each image, unique to each scan. These properties can then be converted into real world units of millimeters. The empirical information the program provides us with is unique for each MRI scan and is extremely useful in determining the size, location, and type of tumor, that the users can analyze and incorporate into the graphical user interface.



    • Build a Tool Contest

      Build a Tool Contest
    • Organization Contest

      Organization Contest
    • Tape Contest

      Tape Contest