Examples of EDA for Image Analysis

Some code snippets for analyzing your image dataset

A different kind of EDA

EDA with the Alzheimer's data set

First up is showing the average image for each class. This is a great way to see the average pixel distribution in each image and see if there are any major differences between the classes.

fig, ax = plt.subplots(nrows=1,ncols=4,figsize=(20,5))

NonD_mean = find_mean_img(NonD_images, ax[0], 'NonDemented')
veryMild_mean = find_mean_img(veryMild_images, ax[1], 'VeryMildDemented')
mild_mean = find_mean_img(mild_images, ax[2], 'MildDemented')
Moderate_mean = find_mean_img(moderate_images, ax[3], 'ModerateDemented')


Here you can see clear deterioration in the most extreme progression of Alzheimer’s. The large areas of missing tissue became very visible as the disease progresses. To visualize the difference between a healthy brain and the fully progressed Alzheimers brain we plot the contrast between the average images like so.

contrast_mean = NonD_mean - Moderate_mean
plt.title(f'Difference Between NonD & ModerateDemented Mean')

The white segments of the this image show the large sections of difference between a healthy brain and a fully demented brain.

Similar to average images we can also plot the pixel standard deviation

fig, ax = plt.subplots(nrows=1,ncols=4,figsize=(20,5))

NonD_std = find_std_img(NonD_images, ax[0], 'NonDemented')
veryMild_std = find_std_img(veryMild_images, ax[1],'VeryMildDemented')
mild_std = find_std_img(mild_images, ax[2],'MildDemented')
Moderate_std = find_std_img(moderate_images,ax[3], 'ModerateDemented')


Just like the average image, the std image shows clear deterioration in the the moderate demented class.

For the last example of image eda I will show how to do a histogram oriented gradient, or hog. This visualization demonstrates the difference of the brightness and the direction of change in the pixels. It is a great method for seeing important features between two classes.

#histogram of oriented gradients
fd_NonD, NonD_hog = hog(NonD_mean, orientations=8, pixels_per_cell=(8,8),
fd_moderate, moderate_hog = hog(Moderate_mean, orientations=8, pixels_per_cell=(8,8),

NonD_hogs = exposure.rescale_intensity(NonD_hog, in_range=(0,0.04))
moderate_hogs = exposure.rescale_intensity(moderate_hog, in_range=(0,0.04))

fig = plt.figure(figsize=(16,16))
fig.suptitle('Histogram of Oriented Gradients', x=.5,y=0.92,fontsize=16)
ax1 = fig.add_subplot(2,2,1)
ax1.imshow(NonD_hogs, cmap='binary')
ax2 = fig.add_subplot(2,2,2)
ax2.imshow(moderate_hogs, cmap='binary')
ax2.set_title('Moderately Demented')


In the moderately demented class you can see a huge concentration of dark pixels in the grey matter and hippocampus.

Happy Coding

Data Scientist With A Background in Biology