Dive into Medical Imaging: X-ray Image Augmentation

Elias Hossain
7 min readAug 22, 2021

--

Early in my research career, I had to make significant contributions to artificial intelligence (AI) based medical imaging research, and from there, my journey towards medical imaging evolved. This article will discuss the theory and practical implementation of medical image augmentation.

What will you know after exploring this article?

  • Artificial Intelligence (AI) for medical imaging
  • The theory behind the medical image augmentation
  • Practical implementation on Covid-19 image dataset
Figure 1: Sample dataset for experimenting (Covid-19 based medical image data)

Let’s dive into the data-driven world

Having the rise of technology, the types of work are also constantly changing; for example, if we look a little better, we will observe that the application of artificial intelligence is becoming more and more ubiquitous.

Do you know how powerful the computer is now? 🤨 🤨 🤨

Computers are now becoming a powerful weapon, and it has the potential application in the medical sector. Several advanced algorithms have been discovered for research in the healthcare sector that can read hard images such as X-rays, CT scans, etc. We don’t have to depend on doctors too much, as the application of artificial intelligence (AI) is making our life a lot easier. Let’s talk about AI for medical imaging.

Artificial Intelligence (AI) for medical imaging

The application of artificial intelligence (AI) in diagnostic medical imaging is undergoing thorough consideration. For example, a computer-aided algorithm could examine x-rays and other images for signs of opacities that indicate pneumonia, then alert doctors to the potential diagnoses to allow for faster treatment When a pneumothorax is suspected, AI can help to recognize high-risk individuals, especially when radiologists are not present.

The theory behind the medical image augmentation

In order to dive into image augmentation, it is required to understand the challenges we may have to experience when working with medical image data. To put it more simply, finding medical image data is relatively difficult, so accessibility is a matter of great concern in this case. Secondly, getting a massive amount of dataset is quite challenging owing to the fact that this type of sensitive data is not usually publicly available. It is certainly true that Machine learning requires a lot of data to work; otherwise, it becomes very difficult to create a model as robust, and if this is the case with medical image data, then the data size must be increased. Suppose you have 1000 image data, but somehow if you can successfully expand it to 2000 samples, it will be more interesting, right?

So, image augmentation is such an approach from which you can increase your data sample; in that way, you will be able to train your machine learning model productively.

To acquire accurate results using deep learning methods, a large amount of data is needed. Images can be augmented in a variety of ways to broaden the dataset and sample size. This is a very dependable strategy when working with critical medical images because, in most circumstances, the model’s accuracy doesn’t improve if the amount of data is insufficient. In addition to reducing over-fitting, augmentation can also improve the precision of your proposed models.

Practical implementation on Covid-19 image dataset

Step 01: Clone the Covid-19 image dataset

!git clone https://github.com/casperbh96/COVID-19-Detection.git dataset

Step 02: Import essential library

import pandas as pdimport matplotlib.pyplot as plt%matplotlib inlineimport numpy as npimport cv2, timeimport tensorflow as tftf.__version__

Step 03: Load the Covid-19 dataset

covid_path = 'dataset/covid_dataset.csv'covid_image_path = 'dataset/covid_adjusted/'normal_path = 'dataset/normal_xray_dataset.csv'normal_image_path = 'dataset/normal_dataset/'covid_df = pd.read_csv(covid_path, usecols=['filename', 'finding'])normal_df = pd.read_csv(normal_path, usecols=['filename', 'finding'])normal_df = normal_df.head(99)covid_df.head()

Now, we shall perform some additional works

covid_images = []covid_labels = []for index, row in covid_df.iterrows():filename = row['filename']label = row['finding']path = covid_image_path + filenameimage = cv2.imread(path)image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)covid_images.append(image)covid_labels.append(label)normal_images = []normal_labels = []for index, row in normal_df.iterrows():filename = row['filename']label = row['finding']path = normal_image_path + filename# temporary fix while we preprocess ALL the imagesif filename == '4c268764-b5e5-4417-85a3-da52916984d8.jpg':breakimage = cv2.imread(path)image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)normal_images.append(image)normal_labels.append(label)# normalize to interval of [0,1]covid_images = np.array(covid_images) / 255# normalize to interval of [0,1]normal_images = np.array(normal_images) / 255

Step 04: We have to make a function for visulizing the images

def plot_images(images, title):nrows, ncols = 3, 8figsize = [10, 6]fig, ax = plt.subplots(nrows=nrows, ncols=ncols, figsize=figsize, facecolor=(1, 1, 1))for i, axi in enumerate(ax.flat):axi.imshow(images[i])axi.set_axis_off()plt.suptitle(title, fontsize=24)plt.tight_layout(pad=0.2, rect=[0, 0, 1, 0.9])plt.show()plot_images(covid_images, 'Positive COVID-19 Cases')plot_images(normal_images, 'Negative COVID-19 Cases')

After executing step 04, you will observe the following images →

Figure 2: Negative and Positive Covid-19 cases images

Step 05: Splitting dataset into test and train

from sklearn.model_selection import train_test_splitfrom sklearn.preprocessing import LabelBinarizerfrom tensorflow.keras.utils import to_categorical# split into training and testingcovid_x_train, covid_x_test, covid_y_train, covid_y_test = train_test_split(covid_images, covid_labels, test_size=0.2)normal_x_train, normal_x_test, normal_y_train, normal_y_test = train_test_split(normal_images, normal_labels, test_size=0.2)X_train = np.concatenate((normal_x_train, covid_x_train), axis=0)X_test = np.concatenate((normal_x_test, covid_x_test), axis=0)y_train = np.concatenate((normal_y_train, covid_y_train), axis=0)y_test = np.concatenate((normal_y_test, covid_y_test), axis=0)# make labels into categories - either 0 or 1y_train = LabelBinarizer().fit_transform(y_train)y_train = to_categorical(y_train)y_test = LabelBinarizer().fit_transform(y_test)y_test = to_categorical(y_test)

Step 06: In this phase, we shall make some functions for augmenting our Covid-19 images. As you know, several traditional methods can be found in terms of image augmentation. Still, in this article, Contrast, Saturation, Flip-up down and Rotations have been considered because these are the most common approaches using frequently.

Method 01: Contrast

X_train_contrast = []for x in X_train:contrast = tf.image.adjust_contrast( x, 2 ) #It takes two parameter: Images and constrast_factor.#Images:Images to adjust. At least 3-D.#Constrast_factor:A float multiplier for adjusting contrast.X_train_contrast.append(contrast.numpy())plot_images(X_train_contrast, 'Adjusted Contrast')

After executing Method 01, you will observe the following images →

Figure 3: Applying the image augmentation approach, e.g. Contrast

Method 02: Saturation

X_train_saturation = []for x in X_train:saturation = tf.image.adjust_saturation( x, 3 )X_train_saturation.append(saturation.numpy())plot_images(X_train_saturation, 'Adjusted Saturation')

After executing Method 02, you will observe the following images →

Figure 4: Performing the image augmentation approach, e.g. Saturation

Method 03: Flip up-down

X_train_flipped_up_down = []for x in X_train:flipped = tf.image.flip_up_down(x)X_train_flipped_up_down.append(flipped.numpy())plot_images(X_train_flipped_up_down, 'Flipped Up Down')
Figure 5: Visualizing the augmented images, e.g. Flipped up-down

Method 04: Applying rotations technique. Here I have considered 45 degrees, 135 degrees, 225 degrees and 315 degrees.

import tensorflow_addons as tfafrom math import radiansX_train_rot_45_deg = []X_train_rot_135_deg = []X_train_rot_225_deg = []X_train_rot_315_deg = []for x in X_train:deg_45 = tfa.image.transform_ops.rotate(image, radians(45))deg_135 = tfa.image.transform_ops.rotate(image, radians(135))deg_225 = tfa.image.transform_ops.rotate(image, radians(225))deg_315 = tfa.image.transform_ops.rotate(image, radians(315))X_train_rot_45_deg.append(deg_45)X_train_rot_135_deg.append(deg_135)X_train_rot_225_deg.append(deg_225)X_train_rot_315_deg.append(deg_315)plot_images(X_train_rot_45_deg, 'Rotated 45 Degrees')plot_images(X_train_rot_135_deg, 'Rotated 135 Degrees')plot_images(X_train_rot_225_deg, 'Rotated 225 Degrees')plot_images(X_train_rot_315_deg, 'Rotated 315 Degrees')
Figure 6: Visualizing the augmented images, e.g. Rotated 135 degrees, 45 degrees, 225 degrees and 315 degrees

To conclude, this article has demonstrated the theory and practical implementation behind medical image augmentation. For experimenting, we have taken the Covid-19 image dataset and explained how the medical image can be augmented. I firmly believe that the information provided in this article will be very helpful for those who are thinking of starting work in the healthcare sector.

Find the source code from my Github repository:

References

  1. Shorten, C., & Khoshgoftaar, T. M. (2019). A survey on image data augmentation for deep learning. Journal of Big Data, 6(1), 1–48.
  2. Punn, N. S., & Agarwal, S. (2021). Automated diagnosis of COVID-19 with limited posteroanterior chest X-ray images using fine-tuned deep neural networks. Applied Intelligence, 51(5), 2689–2702.

Find me at →

Researchgate : https://www.researchgate.net/profile/Elias-Hossain-2

LinkedIn: https://www.linkedin.com/in/elias-hossain-b70678160/

--

--

Elias Hossain
Elias Hossain

Written by Elias Hossain

CS PhD Student at Mississippi State University

Responses (1)