NumPy for images

Image Processing in Python

Rebeca Gonzalez

Data Engineer

NumPy for images

  • Fundamentals of image processing techniques
    • Flipping
    • Extract and analyze features

Cute dog walking in the park

Cute dog horizontally flipped

Image Processing in Python

Images as NdArrays

Madrid building

# Loading the image using Matplotlib
madrid_image = plt.imread('/madrid.jpeg')

type(madrid_image)
<class 'numpy.ndarray'>
Image Processing in Python

Colors with NumPy

GRapes image above 3 more images that show the color channels

Image Processing in Python

Colors with NumPy

# Obtaining the red values of the image
red = image[:, :, 0]

# Obtaining the green values of the image
green = image[:, :, 1]

# Obtaining the blue values of the image
blue = image[:, :, 2]

Channels of the image viewed separately

Image Processing in Python

Colors with NumPy

Channels of a young woman image viewed separately displayed with gray camp

plt.imshow(red, cmap="gray")    
plt.title('Red')
plt.axis('off')
plt.show()
Image Processing in Python

Shapes

Madrid Building

# Accessing the shape of the image
madrid_image.shape
(426, 640, 3)
Image Processing in Python

Sizes

Madrid Building

# Accessing the shape of the image
madrid_image.size
817920
Image Processing in Python

Flipping images: vertically

# Flip the image in up direction
vertically_flipped = np.flipud(madrid_image)

show_image(vertically_flipped, 'Vertically flipped image')

Vertically flipped Madrid image

Image Processing in Python

Flipping images: horizontally

# Flip the image in left direction
horizontally_flipped = np.fliplr(madrid_image)

show_image(horizontally_flipped, 'Horizontally flipped image')

Madrid image flipped horizontally

Image Processing in Python

What is a histogram?

Histogram of a dark image Histogram of a light image

Image Processing in Python

Color histograms

Histogram of young woman RGB-3 colored image, showing one histogram for each color

Image Processing in Python

Applications of histograms

  • Analysis
  • Thresholding
  • Brightness and contrast
  • Equalize an image

Thresholded image

Image Processing in Python

Histograms in Matplotlib

Histogram of red color

# Red color of the image
red = image[:, :, 0]

# Obtain the red histogram plt.hist(red.ravel(), bins=256)
Image Processing in Python

Visualizing histograms with Matplotlib

blue = image[:, :, 2]

plt.hist(blue.ravel(), bins=256)
plt.title('Blue Histogram')
plt.show()

Histogram of blue color of an image

Image Processing in Python

Let's practice!

Image Processing in Python

Preparing Video For Download...