NumPy för bilder

Bildbehandling i Python

Rebeca Gonzalez

Data Engineer

NumPy för bilder

  • Grundläggande bildbehandlingstekniker
    • Spegling
    • Extrahera och analysera särdrag

Söt hund som promenerar i parken

Söt hund speglad horisontellt

Bildbehandling i Python

Bilder som NdArrays

Madridbyggnad

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

type(madrid_image)
<class 'numpy.ndarray'>
Bildbehandling i Python

Färger med NumPy

Druvor ovanför tre bilder som visar färgkanalerna

Bildbehandling i Python

Färger med 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]

Bildens kanaler visade separat

Bildbehandling i Python

Färger med NumPy

Kanaler för en ung kvinnas bild visade separat i gråskala

plt.imshow(red, cmap="gray")    
plt.title('Red')
plt.axis('off')
plt.show()
Bildbehandling i Python

Former

Madridbyggnad

# Accessing the shape of the image
madrid_image.shape
(426, 640, 3)
Bildbehandling i Python

Storlekar

Madridbyggnad

# Accessing the shape of the image
madrid_image.size
817920
Bildbehandling i Python

Spegla bilder: vertikalt

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

show_image(vertically_flipped, 'Vertically flipped image')

Vertikalt speglad Madridbild

Bildbehandling i Python

Spegla bilder: horisontellt

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

show_image(horizontally_flipped, 'Horizontally flipped image')

Madridbild speglad horisontellt

Bildbehandling i Python

Vad är ett histogram?

Histogram för en mörk bild Histogram för en ljus bild

Bildbehandling i Python

Färghistogram

Histogram för en ung kvinnas RGB-bild i tre färger, med ett histogram per färg

Bildbehandling i Python

Användningsområden för histogram

  • Analys
  • Tröskling
  • Ljusstyrka och kontrast
  • Utjämna en bild

Trösklad bild

Bildbehandling i Python

Histogram i Matplotlib

Histogram för röd färg

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

# Obtain the red histogram plt.hist(red.ravel(), bins=256)
Bildbehandling i Python

Visualisera histogram med Matplotlib

blue = image[:, :, 2]

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

Histogram för blå färg i en bild

Bildbehandling i Python

Nu kör vi en övning!

Bildbehandling i Python

Preparing Video For Download...