None
Original Image | Salt & Pepper Noise | Gaussian Noise | Brightness

Brief Introduction

Collection of any type of data can be expensive, tedious, but most importantly, it may not be feasible to acquire data that is needed to train the model. Why? It could be due to many reasons like privacy (medical data) or physical limitations (astrology). If our ability to collect data is limited like so, then we can use these data augmentation techniques to build a more robust dataset, reduce overfitting, and increase the amount of training data.

If you are here just for the code, skip to the sections below!

Medical Image Data Augmentation

It is always important to remember that in a clinical setting, medical images are used by healthcare professionals to make critical decisions. Thus, most research regarding deep learning with medical images are started with a goal to accurately detect or diagnose harmful objects (e.g. tumors), so any image augmentations should prioritize patient safety and the integrity of the medical data.

With the above information, it is very important to keep in mind that augmentations should not introduce artifacts or features that do not occur naturally in medical images. The augmented images should remain clinically relevant and representative of actual patient cases.

Thus, I will be going over these four techniques that are most often used for medical image augmentation:

  • Brightness & Contrast
  • Noise (Gaussian, Salt and Pepper)
  • Blur (Gaussian filter)
  • Acoustic Shadowing

Brightness & Contrast

None
Original Image | Brightness = 0.9~1.1 | Brightness = 0.5~1.5
None
Original Image | Contrast = 0.8~1.2 | Contrast = 0.5~1.5

Brightness and contrast is probably the most basic type of data augmentation technique that you can choose. For medical images, changes in brightness and contrast should be subtle to avoid distorting the image's diagnostic value. The goal is to enhance visual perception without introducing artificial features.

According to IMAIOS, a well-known medical image tool database company for health practitioners, changes to brightness should be in random intervals of [-0.1 : 0.1] and changes in contrast should range between [0.8 : 1.2]. What this means is that if the default value of the brightness and contrast of the image is 1, then data augmentation for brightness should be done in the range of [0.9 : 1.1] and contrast should be done as written above.

To acquire and implement this technique in a python setting, we will use PyTorch and it's torchvision library. Key component to takeaway from this code is the transforms.ColorJitter function. It applies color jitter transformations to the image, which can be brightness, contrast, hue, and saturation. In our case, it randomly adjusts the brightness within the specified range of (0.9, 1.1) and contrast between (0.8, 1.2).

Brightness

from PIL import Image
from torchvision import transforms

# Load the original image
img = Image.open('/your/own/path/cancer_image.png')

data_transform = transforms.Compose([
    transforms.ColorJitter(brightness=(0.9, 1.1)),
    # additional transformations here
])

# Apply the defined data transformation to the image
img = data_transform(img)

display(img)

Contrast

from PIL import Image
from torchvision import transforms

# Load the original image
img = Image.open('/your/own/path/cancer_image.png')

data_transform = transforms.Compose([
    transforms.ColorJitter(contrast=(0.8, 1.2)),
    # additional transformations here
])

# Apply the defined data transformation to the image
img = data_transform(img)

display(img)

Noise

In medical imaging, where precision is crucial, noise should be carefully considered to avoid obscuring important diagnostic details. There are different types of noise modalities that can be introduced, however I'll only be going over the gaussian noise and impulse noise (salt & pepper) in this post.

Gaussian Noise

Gaussian noise is a type of random noise that follows a Gaussian distribution (also known as a normal distribution). In medial images, it can be used to simulate random variations that might occur due to imaging equipment imperfections, sensor noise, or other sources of randomness.

None
Variance = 5 | Variance = 40 | Variance = 70
from PIL import Image
import numpy as np
import random

def gaussian_noise(img,var):
    """Add guassian noise to image"""

    noise = np.random.normal(0,var,size=img.shape).astype("float32")

    noisy_img = img + noise
    noisy_img = np.clip(noisy_img, 0, 255).astype("uint8") 
    return noisy_img

img = Image.open('/your/own/path/cancer_image.png')

variance = 5 #5, 20, 40, etc
noisy_img = gaussian_noise(np.array(img), variance)
noisy_pil_img = Image.fromarray(noisy_img)

display(noisy_pil_img)

Impulse Noise

Impulse noise is a type of noise that affects individual pixels by setting them to either the maximum intensity (salt) or the minimum intensity (pepper) hence the name "Salt and Pepper Noise". In medical imaging, salt-and-pepper noise can simulate abrupt signal drops (black pixels) or spikes (white pixels) that might result from sensor malfunctions, motion artifacts, or other irregularities in the imaging process.

None
Probability = 0.05 | Probability = 0.1 | Probability = 0.5
from PIL import Image
import numpy as np
import random

def sp_noise(img,prob=0.1):
    """Add salt and pepper noise to image"""

    height,width,channels = np.shape(img)
    img = np.array(img)

    #Iterate over all pixels
    for i in range(height):
        for j in range(width):
            #Randomly change pixel values
            if random.random()<prob:
                if random.random() < 0.5:
                    img[i][j] = np.array([255,255,255]) #white
                else:
                    img[i][j] = np.array([0,0,0]) #black

    img = Image.fromarray(img)

    return img

img = Image.open('/your/own/path/cancer_image.png')

noisy_img = sp_noise(img, prob=0.1)
display(noisy_img)

Blur (Gaussian filter)

A Gaussian blur is a type of image blurring that involves convolving the image with a Gaussian distribution. This process smoothens the image by reducing high-frequency noise and fine details, which results in a 'blurred' like appearance. The extent of blurring is controlled by the standard deviation (sigma) of the Gaussian distribution. A larger sigma value leads to a stronger blur effect, and a smaller value results in milder blurring.

Much like other augmentation methods, in medical imaging, blur augmentation can simulate the effects of motion or imperfections in imaging devices. For instance, when capturing an MRI or CT scan, patient movement or slight vibrations can cause blurring in the resulting images.

To apply the Gaussian blur, you can use the gaussian_filter() function within the scipy library.

None
from scipy.ndimage.filters import gaussian_filter

# Convert the image to a NumPy array
img_array = np.array(img)

# Apply Gaussian blur using scipy's gaussian_filter function
sigma = 2.0  # Adjust the sigma value for different levels of blurring
blurred_img = gaussian_filter(img_array, sigma=sigma)

plt.imshow(blurred_img)

Acoustic Shadowing

None
Source

In the context of medical image augmentation, "shadowing" refers to the simulated appearance of shadows or shading effects within an image. It's an augmentation technique that aims to introduce variations similar to what might be observed in real-world medical images due to lighting conditions or tissue properties.

This paper by R. Singla et al. points out the acoustic shadowing as "one of the most often encountered artifacts in ultrasound." According to the paper, shadows in an image may be caused by insufficient contact between the transducer and the surface or locations with large acoustic impedance variations between tissue interfaces.

One downside is that most acoustic shadows are and should be manually segmented due to the fact that areas of focus may be different for each image. I won't be going over the code in this article, but it is important to note that some sort of shadowing or "black-out" technique is useful for medical image augmentation.

Conclusion

Understanding the techniques of medical image data augmentation techniques is crucial for enhancing the efficiency of model training. Data collection for medical purposes can be expensive and restricted due to privacy concerns or equipment limitations, making these types of augmentation invaluable. However, like I have mentioned several times in this article, the application of augmentation must be carefully considered in medical contexts where the accuracy of image interpretation can have critical consequences.

Finding new ways to augment data is going to be one of the most crucial aspects when it comes to medical deep learning models. Unless there is a way to get millions and millions of medical data (which is very unlikely due to patient privacy), we will forever have to result to augmentation techniques to build a robust model.

Hopefully this article gave you some insight on image data augmentation as a whole as well as a narrower view into medical image augmentation!

A Message from AI Mind

None

Thanks for being a part of our community! Before you go: