To remove background noise from MRI images in MATLAB

To remove background noise from MRI images in MATLAB, follow these steps using thresholding, morphological operations, and connected component analysis:

Step-by-Step Solution:

  1. Read the MRI Image:
    Load the image, convert to grayscale if necessary, and prepare it for processing.
  2. Thresholding:
    Use Otsu’s method to automatically determine the optimal threshold to separate foreground (anatomy) from background noise.
  3. Morphological Cleaning:
    Remove small noise artifacts and fill holes in the mask using morphological operations.
  4. Largest Connected Component:
    Identify the main anatomical structure by selecting the largest connected region in the mask.
  5. Apply Mask:
    Multiply the original image by the cleaned mask to set background pixels to zero.

MATLAB Code:

matlab
Copy
% Read the MRI image (replace with your image)
mri_image = imread('mri_image.jpg'); % Use dicomread for DICOM files

% Convert to grayscale if RGB
if size(mri_image, 3) == 3
    mri_image = rgb2gray(mri_image);
end

% Convert to double for processing and normalize
image_double = im2double(mri_image);

% Step 1: Apply Gaussian smoothing to improve thresholding
smoothed_image = imgaussfilt(image_double, 2);

% Step 2: Calculate Otsu's threshold and create initial mask
level = graythresh(smoothed_image);
mask = imbinarize(smoothed_image, level);

% Step 3: Clean the mask using morphological operations
mask = imopen(mask, strel('disk', 5)); % Remove small noise
mask = imfill(mask, 'holes'); % Fill holes

% Step 4: Keep the largest connected component
cc = bwconncomp(mask);
[~, idx] = max(cellfun(@numel, cc.PixelIdxList));
mask = false(size(mask));
mask(cc.PixelIdxList{idx}) = true;

% Step 5: Apply the mask to the original image
denoised_image = mri_image .* cast(mask, class(mri_image));

% Display results
figure;
subplot(1,2,1), imshow(mri_image, []), title('Original Image');
subplot(1,2,2), imshow(denoised_image, []), title('Background Removed');

Explanation:

  • Thresholding: Otsu’s method (graythresh) separates foreground and background by maximizing inter-class variance.
  • Morphological Operationsimopen removes small noise, and imfill fills gaps in the mask.
  • Largest Component: Ensures only the main anatomical structure is retained.
  • Mask Application: Sets background pixels to zero, effectively removing noise.

Notes:

  • DICOM Images: Replace imread with dicomread for DICOM files.
  • Parameter Tuning: Adjust the disk size in imopen based on noise level.
  • Advanced Cases: For non-uniform backgrounds or intensity bias, consider advanced methods like active contours or bias correction.

This approach effectively isolates the anatomical structure and eliminates background noise.