A step-by-step Malaria Parasite Detection using CNN and Image Processing
This is my first medium blog. Hope you have fun reading it as much as I enjoyed writing it

Table of Contents:
- Introduction
- Motivation
- Challenges Identified
- Problem definition
- Prerequisites
- Demo
- Information about the dataset
- Workflow of the system
- Algorithmic view with Implementation details
- GUI
- Performance evaluation parameters
- Results
- Conclusion
- Future scope
- References
1. Introduction
Malaria remains a significant burden on the global health system which is the major cause of death in many developing countries due to a shortage of equipments and medical expertise. Thus, specialized technology proves essential to combat this problem. Machine learning(ML) is a subset of AI which processes and helps find patterns in large datasets. Machine learning in the healthcare domain has recently made headlines. The combination of machine learning, predictive analytics and health informatics offers opportunities to improve healthcare processes which helps improve patient outcomes. Machine learning approaches are successful in image based analysis and diagnosis. Machine learning can improve the accuracy of the treatment protocols and healthcare outcomes through algorithmic processes.
2. Motivation
We must first understand how important this problem is in the real world. Malaria is a true endemic in some parts of the world which means that the disease is regularly found in the region. Every year about 3.2 billion people are at risk of malaria globally. The World Health Organization (WHO) has released crucial facts about malaria which you can check out here. A delay in the treatment may lead to several complications and even death in some cases. Thus, an early testing and detection of malaria is necessary to save lives. This gives us the motivation to make malaria diagnosis effective and fast.
3. Challenges Identified
- For Malaria diagnosis, RDT and microscopic diagnosis are the two widely used methods.
- RDT is an effective tool and provides the results within 15 minutes. It does not require presence of any trained professional. However, according to WHO, RDT has drawbacks like higher cost compared to light microscope, and susceptibility to damage by heat and humidity.
- Microscopic diagnosis systems do not suffer from these shortcomings but it requires the presence of a trained **** microscopist.
4. Problem Definition
The objective of this system is to address the challenges and issues in the existing system by automating the process of detection of malaria using ML and image processing.
Here we will apply two well known techniques:
- Bins Algorithm
- Convolutional Neural Network
The results of these techniques will give us a concrete proof of the potential of Machine Learning and AI in Malaria Detection.
5. Prerequisites
This post assumes familiarity with Python libraries, Data structures, Tkinter, Image Processing, Linear Algebra, Probability, Statistics, Random Forest algorithm, SVM, Convolution Neural Networks etc.
6. Here’s a short video of the application
Please refer to my GitHub repository here for the codes.
7. Information about the dataset
Before we dive deep into building the application, let us just have a brief look at the dataset. There are many datasets available for malaria detection. But for the purpose of this project, I have used the dataset provided by ‘National Institutes of Health’ consisting of 27558 RGB cell images in .png format which you can download from here.

The dataset contains 2 folders: train folder and test folder with the training and testing images respectively. Further there are 2 more folders -parasitized folder with the malaria infected cell images and uninfected folder with the normal cell images.

These images are bifurcated as follows:
- Parasitic- 13779 images
- Uninfected- 13779 images
Since we have an equal distribution of images so we don’t have to deal with the problem of imbalanced data and it has less chances of being biased towards a particular class.
8. Workflow of the system

- The input RGB cell image is first processed to remove noise from the image
- The preprocessed image is then segmented to extract the Region of interest(ROI) and we get the segmented image
- Input to the feature extraction stage is the segmented image and output will be the feature vectors
- Next stage is classification stage where the input will be the feature vectors and output is the classified label as parasitic and non-parasitic.
9. Algorithmic view with Implementation details
Approach 1: Using Bins Method
In this approach, we will be using Image Processing techniques and ML classification algorithms. The steps are as follows:
Step 1: Image Preprocessing
- Training a model on raw images may lead to bad classification performances.
- The main aim of the pre-processing is to clean the images
- It makes sure that the information in the images is well accessible.
kernel = np.ones((9,9),np.uint8)
clean = cv2.morphologyEx(image, cv2.MORPH_OPEN, kernel)

Step 2: Image Segmentation
Segmentation process is carried out to obtain the region of interest (ROI) from the image. Following techniques are used for segmentation:
- Otsu Segmentation
It is used to perform automatic image thresholding. It returns a single intensity threshold which separate pixels into two classes – background and foreground. Automatic global thresholding algorithms usually have the following steps:
Step 1: Process the input image
Step 2: Obtain image histogram (distribution of pixels)
Step 3: Compute the threshold value
Step 4: Replace image pixels into white in those regions, where saturation is greater than and into the black in the opposite cases.

2. Watershed Segmentation
- It segments an image into several catchment basins or regions.
- Any grayscale image can be interpreted as a 3D topological surface.
- Image can be segmented into regions where conceptually rainwater would flow into same lake.
- So we identify various local minima (pixel intensity is low) in the image. Flood the landscape from local minima and prevent merging of water from different minima.
- This results in partitioning the image into catchment basins and watershed lines.

Step 3: Feature extraction
Input to this stage will be the segmented image and output will be the feature vectors. Here we will be extracting the following features:
- Statistical Features
- Color Features
- Texture Features
Extracting statistical features(mean, std. deviation etc.) using color moments:
Statistical features like Mean gives us the average of data points, Standard deviation gives information about the spread of the data around the mean and Skewness value gives us the measure of asymmetry of a distribution. Following are the steps for extracting statistical features:
Step 1: Read the image file
Step 2: Find the mean value

Step 3: Find the Standard Deviation

Step 4: Find the Skewness value

Step 6: Finally store the mean, standard deviation and skewness value in a 1D array and repeat steps 1–4 for every image in the database.
Extracting color features using Bins Method:
- In Bins Approach, we form the bins so that color details of the image will be separated properly
- Using Bins Approach, we are focusing on the use of image histograms to extract the image features and also trying to reduce the size of the feature vector.
Following are the steps for extracting color features using Bins Method:
Step 1: Spilt the RGB cell image into 3 planes- R, G and B planes.

Step 2: Obtain the histogram for each of the planes.

Step 3: Partition the R, G and B histograms in two parts by calculating the Center of gravity (CG).

Step 4: Process each pixel Pi (ri ,gi ,bi). Compare its r, g and b values with respective CG values of three planes and assign a flag (either ‘0’ OR ‘1’) to each pixel based on the partition of respective histograms in which it falls. For e.g.- Pi (1,1,0)- flag assigned for pixel Pi- is (110- Bin no 5). This leads to the generation of 8-Bin address for each pixel (000 to 111).
Step 5: Count of pixels in the bin: All image pixels will get segregated into 8 bins based on its r, g, b intensities.

This process extracts all color contents of the image and represents each cell image as bins feature vector of 8 components only.
Extracting texture features using GLCM:
- Gray-level co-occurrence matrix(GLCM) is used to extract second order statistical texture features of images
- It is also known as Gray-level spatial dependence matrix
Following are the steps for extracting texture features using GLCM:
Step 1: Compute the co-occurrence matrix

Step 2: Once this co-occurrence matrix is computed, we have to make this co-occurrence matrix as symmetric.

Step 3: This symmetric GLCM matrix has to be normalized

Co-occurrence matrices capture properties of texture like Entropy, Correlation, Contrast, Variance, Angular Second Moment etc.
Step 4: Classification
The input for this stage will be the feature vectors, and output is the classified label. The images are classified by applying the ML algorithms like Random Forest Algorithm and SVM.
Support Vector Machine:

- In this algorithm, we first plot out data item in the n-dimensional space where n is the number of features.
- There are many hyperplanes that separate the positive points from negative points so which the one which separates positive points from negative points as far away as possible will be preferred. This hyperplane is called the margin maximizing hyperplane.
- If we go parallel to the hyperplane and touch a positive point we get the positive hyperplane and if we go parallel to the hyperplane and touch a negative point we get the negative hyperplane.
- The points through which positive hyperplane or negative hyperplane passes are called support vectors.
- The distance between the positive hyperplane and negative hyperplane is call the margin.
- This algorithm finds a hyperplane which maximizes the margin.

- Alternative approach is to construct a convex hull for positive and negative points separately.
- Find the shortest line connecting these hulls.
- Bisect the line.
- The plane which bisects the line is the hyperplane.
Random Forest algorithm:

- Decision tree is a Nested if-else classifier.
- At every Non-leaf nodes we make decisions and at every leaf nodes we have the class labels.
- Corresponding to every decision, we have a hyperplane.
- Decision tree is a set of axis parallel hyperplanes that divides your whole region.
- Random Forest Algorithm consists of a number of individual decision trees that operate as a group. Each tree gives a class prediction.
- The more number of trees in the forest, the more robust the prediction will be and higher the accuracy.
- The class with the maximum number of votes will be our model’s prediction.
Output

The output of the classification stage will be the image with its class label as parasitized for malaria infected cell image and uninfected for normal cell image.
Approach 2: Using CNN
- CNN helps find patterns in an image. It is a model which is designed to process arrays of data such as images.
- Here the input image in our dataset have different dimensions. Since CNN cannot train images of different sizes so first step will be to resize all images.
- For this we will plot the histogram and compute average for both the dimensions(width, height) and resize all the images.

- From the jointplot, we can see the various dimensions ranging from 50 x 60 to 200 x 200. Computing the averages of both the dimensions we set the image shape as (130,130). So we will be dealing with images of size 130 x 130 x 3= 50700 datapoints.
- We are not going to be able to feed in everything at once, instead we’ll have to select batches for our images.
- The other idea that we want the model to be able to overcome is the fact that it should be robust enough to deal of images that are pretty different from images that it’s seen before. And when we can do that is by manipulating and performing transformations on our images things like rotation resizing and scaling.
- We have less than 30,000 images, now we can use random transform and double the dataset. So, we will be using Data Augmentation here using Keras. It transforms the images like flipping, shifting, zoom etc. where xi(image) changes but yi(target class) remains the same.
image_gen = ImageDataGenerator(rotation_range=20,
width_shift_range=0.10,
height_shift_range=0.10,
rescale=1/255,
shear_range=0.1,
zoom_range=0.1,
horizontal_flip=True,
fill_mode='nearest'
)
plt.imshow(image_gen.random_transform(para_img))
Building our first baseline model

- A Sequential model is appropriate here with a plain stack of layers.
- In the Convolution layer, we will place the filter matrix on top of image matrix and compute the value for the cell. Then we will be doing stride jump of 1. This extracts features like edges and their position. Below is an example which shows a convolutional layer’s operation with a kernel having size of 3 and stride of 1.

- We can also used Padding if sometimes filter does not fit perfectly fit the input image. So we pad the picture with zeros (zero-padding) so that it fits. The operation is as follows:

- For each and every field(cell) apply Relu activation function. Rectified linear unit (ReLU) is a linear function that will output the input if it is positive, else it will output zero.
ReLU(x)=max(0,x)
- Next is Max pooling which selects the maximum element, thus resulting in extraction of most prominent features. Example of a maxpool layer’s operation with a kernel having size of 2 and stride of 1 is as follows:

So like this we can stack any no of convolution operations.

- Last is the fully connected layer. The input to the fully connected layer will be the output from the Max Pooling Layer and is flattened, that is converted to a 1D-array and then fed into the fully connected layer.
- Dropout layer is used to reduce the overfitting.
- A loss function evaluated how well the model models our malaria dataset. Since our problem is of type binary class classification (2 classes- parasitic and non-parasitic), we will use binary cross entropy as our loss function which compares each predicted probabilities to actual class output (either 0 or 1).
- Now for training the model we have to select a batch size, here batch size is 16 images at a time. It first accepts a batch of the dataset, then performs backpropagation on it, and then updates the weights in our model. For the number of epochs specified(20 in our case) the process is repeated.

- The predict_generator function generates predictions for the input samples from a data generator. It returns Numpy array(s) of probabilities.
- We can then set threshold like if probability >0.5 then parasite is detected else not detected. In this way we can make predictions.
my_image = image.load_img(para_cell,target_size=image_shape) #input
my_image = image.img_to_array(my_image)
my_image = np.expand_dims(my_image, axis=0)
- For a single image, load the image and pass it to the model.
- Use img_to_array which transforms specialized image object to an array.
- The model.predict function will generate output predictions for the input samples.
s=model.predict(my_image)
if(s==[[1.]]):
self.label.configure(text="non parasitic")
else:
self.label.configure(text="parasitic")

10. Graphical User Interface
The Graphical User Interface for Malaria Detection System was created using Tkinter library for which we first have to install the Tkinter library:
sudo apt-get install python3-tk
Now import the required libraries and start creating the window where we will be adding our elements like label, button etc for a good GUI.
from tkinter import *
from tkinter import ttk
from tkinter import filedialog
from PIL import Image, ImageTk
import tkinter.font as font
class Root(Tk):
def __init__(self):
super(Root, self).__init__()
self.title("Python Tkinter Dialog Widget")
self.minsize(640, 400)
self.labelFrame = ttk.LabelFrame(self, text = "Open File")
self.labelFrame.grid(column = 0, row = 1, padx = 20, pady = 20)
self.button()
self.button1()
def button(self):
self.button = ttk.Button(self.labelFrame, text = "Browse A File",command = self.fileDialog)
self.button.grid(column = 1, row = 1)
def fileDialog(self):
self.filename = filedialog.askopenfilename(initialdir = "/PROJECT/test samples", title = "Select A File", filetype =
(("png files","*.png"),("all files","*.*")) )
self.label = ttk.Label(self.labelFrame, text = "")
self.label.grid(column = 1, row = 2)
self.label.configure(text = self.filename)
img = Image.open(self.filename)
photo = ImageTk.PhotoImage(img)
self.label2 = Label(image=photo)
self.label2.image = photo
self.label2.grid(column=1, row=5)
def button1(self):
self.button = ttk.Button(self.labelFrame, text = "Submit", command = self.get_prediction)
self.button.grid(column = 1, row = 20)
def get_prediction(self):
s=model.predict(my_image)
if(s==[[1.]]):
#self.label.configure(text="Non Parasitic")
text = Label(self, text="Non Parasitic", font=("Helvetica", 17))
text.place(x=50,y=200)
else:
#self.label.configure(text="Parasitic")
text = Label(self, text="Parasitic", font=("Helvetica", 17))
text.place(x=70,y=200)
root = Root()
root.mainloop()

11. Performance evaluation parameters
Performance is evaluated using parameter like Accuracy, precision, recall and F1-score.

12. Results

- SVM works reasonably well with Bins approach during classification.
- The above table also shows that Random forest with Bins is the best classifier for this approach as it achieved 96.3% accuracy which is much better than the other methods.
- Moreover, CNN had the least accuracy of 94%; however, owing to its drawbacks, it holds a relatively more substantial accuracy. Although CNN model achieved a good accuracy but there are a few drawbacks. CNN takes more time for training the model. Also loading a model and performing necessary operations is more expensive.
13. Conclusion
- The best model that we have got so far has reached an accuracy of 96.3% on unseen data
- Using data augmentation in CNN approach has reduced the chances of overfitting and the number of false negatives.
- Using Dropouts in CNN proved to be good as it helped us to reduce the overfitting of the model.
- Bins Technique proved to be an effective technique which deals with the actual color image contents and represents them in the form of a compact feature
- Bins Approach with Random Forest produced better results than CNN
- Bins Method also reduces the complexity to a great extent as the feature vector is of 8 components.
- After comparing the performances of all the approaches, it can be concluded that Bins Approach plays a significant role in feature extraction
- The Malaria classification system proved to be faster than the traditional techniques. The system is user-friendly and easy to use.
14. Future Scope
- As a scope of future work, we can prepare a fusion feature vector that combines the features of bins, color moments, and GLCM parameters all in one feature dataset and can also do feature selection using the Recursive Feature Elimination (RFE).
- I also wish to experiment with more Machine Learning algorithms like KNN, AdaBoost etc.
15. References
[1] Olugboja, and Z. Wang, "Malaria parasite detection using different machine learning classifiers," 2017 International Conference on Machine Learning and Cybernetics (ICMLC), Ningbo, 2017, pp. 246–250.
[2] H. B. Kekre, Kavita Sonawane, " Image Retrieval Using Histogram Based Bins of Pixel Counts and Average of Intensities" (pp. 74–79, (IJCSIS) International Journal of Computer Science and Information Security, Vol. 10, №1, 2012
[3] K. Silamut, N.H. Phu, C. Whitty, et al. "Image analysis and machine learning for detecting malaria "Am J Pathol, 155 (2017), pp. 395–410
[4] Vinayak K. Bairagi and Kshipra C. Charpe, "Comparison of Texture Features Used for Classification of Life Stages of Malaria Parasite," International Journal of Biomedical Imaging, vol. 2016, Article ID 7214156, 9 pages, 2016.
[5] X. Liu, T. Kawanishi, X. Wu and K. Kashino, "Scene text recognition with high performance CNN classifier and efficient word inference," 2016 IEEE International Conference on Acoustics, Speech and Signal Processing (ICASSP), Shanghai, 2016, pp. 1322–1326, doi: 10.1109/ICASSP.2016.7471891.
[6] Zulpe, Nitish & Pawar, V. (2012). GLCM textural features for Brain Tumor Classification. IJ CSI. 9. 354–359
Hope you enjoyed reading this blog and Thanks for reading!
Follow me for more such articles on different real world problems on data science!
You can also connect with me on LinkedIn.