...

Escrito por Aarón Espasandín

2 minutos de lectura

Images as Grids of Pixels

import numpy as np
import matplotlib.image as mpimg # for reading in images
import matplotlib.pyplot as plt
import cv2

%matplotlib qt 
# Read in the image
imgUrl = "../../assets/1.Image-Representation-Classification/"
image = mpimg.imread(imgUrl + "tesla-roadster.jpg")

# Print out the image dimensions
print("Image dimensions: ", image.shape)
Image dimensions:  (750, 1500, 3)
# Change from color to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)

plt.imshow(gray_image, cmap="gray")
<matplotlib.image.AxesImage at 0x7f1b3a0cae80>

# Print specific grayscale pixel values

# max_val = np.amax(gray_image)
# min_val = np.amin(gray_image)
x = 190
y = 375
pixel_val = gray_image[y, x]
print(pixel_val)
101

RGB Channels

# Isolate RGB channels
r = image[:,:,0]
g = image[:,:,1]
b = image[:,:,2]

# Visualize the individual color channels
f, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize=(20,10))
ax1.set_title('R channel')
ax1.imshow(r, cmap='gray')
ax2.set_title('G channel')
ax2.imshow(g, cmap='gray')
ax3.set_title('B channel')
ax3.imshow(b, cmap='gray')
<matplotlib.image.AxesImage at 0x7f1b381fc1d0>

¿Quieres contactar conmigo?

Reporta un bug

Para cualquier error en la web o en la escritura, porfavor abre un issue en Github.

Github
Mándame un mensaje

Siéntete libre de mandarme un tweet con cualquier recomendación o pregunta.

Twitter