Article image
Anderson Junior
Anderson Junior11/01/2024 02:14
Share

Image Colorization

  • #Python

# Colorização de Imagem com OpenCV:

Este código tem tudo a ver com a colorização de uma imagem em tons de cinza usando um modelo de rede neural profunda pré-treinado. Vamos detalhar passo a passo:

1. **Importação de bibliotecas:**

  - «numpy» para operações numéricas.

  - 'matplotlib.pyplot' para plotagem de imagens.

  - 'cv2' (OpenCV) para tarefas de visão computacional.

2. **Carregando a imagem de teste:**

  - Lê uma imagem em tons de cinza especificada pelo caminho do arquivo usando OpenCV.

  - Verifica se a imagem foi carregada com sucesso.

3. **Exibindo Imagem Original:**

  - Se a imagem for carregada com sucesso, ela é exibida usando Matplotlib com um colormap cinza e interpolação bicúbica.

4. **Definindo caminhos para modelo e dados:**

  - Especifica caminhos para o modelo Caffe, arquivo prototxt e arquivo numpy.

5. **Carregando o modelo Caffe:**

  - Lê o modelo Caffe e configura algumas camadas para colorização.

6. **Pré-processamento de imagem:**

  - Lê a imagem de teste novamente e converte-a de BGR para escala de cinza e, em seguida, para o formato RGB usando OpenCV.

  - Normaliza os valores da imagem.

  - Converte a imagem RGB em formato LAB.

7. **Extraindo o canal L:**

  - Redimensiona a imagem LAB.

  - Extrai o canal L (luminância) e subtrai 50 dos seus valores.

8. **Prevendo canais a e b:**

  - Define o canal L como entrada para a rede pré-treinada.

  - Prevê os canais 'a' e 'b' para colorização.

9. **Combinação de canais:**

  - Combina o canal L original com os canais 'a' e 'b' previstos para criar uma imagem LAB colorida.

10. **Convertendo LAB para RGB:**

  - Converte a imagem LAB de volta para o formato RGB.

11. **Ajustando os valores de pixel:**

  - Clipa e dimensiona valores de pixel para o intervalo [0, 255].

12. **Exibindo imagem colorida:**

  - Exibe a imagem colorida usando Matplotlib.

13. **Salvando a imagem colorida:**

  - Converte a imagem RGB para o formato BGR (convenção OpenCV) e salva-a no caminho de saída especificado.

Em resumo, o código pega uma imagem em tons de cinza, processa-a através de uma rede neural pré-treinada para prever canais de cores e, em seguida, exibe e salva a imagem colorida resultante.

# Importing libraries

import numpy as np

import matplotlib.pyplot as plt

import cv2

# Name of testing image

image_path = 'whale.jpg'

# Load the image

test_image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)

# Check if the image is loaded successfully

if test_image is not None:

  # Display the image with gray colormap and bicubic interpolation

  plt.imshow(test_image, cmap='gray', interpolation='bicubic')

  plt.axis('off')

  plt.show()

else:

  print("Error: Unable to load the image.")

# Path of our caffemodel, prototxt, and numpy files

prototxt = "colorization_deploy_v2.prototxt"

caffe_model = "colorization_release_v2.caffemodel"

pts_npy = "pts_in_hull.npy"

test_image = "test_samples/" + image_path

# Loading our model

net = cv2.dnn.readNetFromCaffe(prototxt, caffe_model)

pts = np.load(pts_npy)

 

layer1 = net.getLayerId("class8_ab")

print(layer1)

layer2 = net.getLayerId("conv8_313_rh")

print(layer2)

pts = pts.transpose().reshape(2, 313, 1, 1)

net.getLayer(layer1).blobs = [pts.astype("float32")]

net.getLayer(layer2).blobs = [np.full([1, 313], 2.606, dtype="float32")]

# Converting the image into RGB and plotting it

# Read image from the path

test_image = cv2.imread(test_image)

# Convert image into gray scale

test_image = cv2.cvtColor(test_image, cv2.COLOR_BGR2GRAY)

# Convert image from gray scale to RGB format

test_image = cv2.cvtColor(test_image, cv2.COLOR_GRAY2RGB)

# Check image using matplotlib

plt.imshow(test_image)

plt.axis('off')

plt.show()

# Converting the RGB image into LAB format

# Normalizing the image

normalized = test_image.astype("float32") / 255.0

# Converting the image into LAB

lab_image = cv2.cvtColor(normalized, cv2.COLOR_RGB2LAB)

# Resizing the image

resized = cv2.resize(lab_image, (224, 224), interpolation=cv2.INTER_CUBIC)

# Extracting the value of L for LAB image

L = cv2.split(resized)[0]

L -= 50  # OR we can write L = L - 50

# Predicting a and b values

# Setting input

net.setInput(cv2.dnn.blobFromImage(L))

# Finding the values of 'a' and 'b'

ab = net.forward()[0, :, :, :].transpose((1, 2, 0))

# Resizing

ab = cv2.resize(ab, (test_image.shape[1], test_image.shape[0]))

# Combining L, a, and b channels

L = cv2.split(lab_image)[0]

# Combining L,a,b

LAB_colored = np.concatenate((L[:, :, np.newaxis], ab), axis=2)

# Checking the LAB image

plt.imshow(LAB_colored)

plt.title('LAB image')

plt.axis('off')

plt.show()

## Converting LAB image to RGB

RGB_colored = cv2.cvtColor(LAB_colored, cv2.COLOR_LAB2RGB)

# Limits the values in array

RGB_colored = np.clip(RGB_colored, 0, 1)

# Changing the pixel intensity back to [0,255], as we did scaling during pre-processing and converted the pixel intensity to [0,1]

RGB_colored = (255 * RGB_colored).astype("uint8")

# Checking the image

plt.imshow(RGB_colored)

plt.title('Colored Image')

plt.axis('off')

plt.show()

# Saving the colored image

# Converting RGB to BGR

RGB_BGR = cv2.cvtColor(RGB_colored, cv2.COLOR_RGB2BGR)

# Saving the image in the desired path

outImage = 'outImg.jpeg'

cv2.imwrite(outImage, RGB_BGR)

https://github.com/demoner21/Pixel_and_Poetry

https://medium.com/@Pixel_and_Poetry

Share
Comments (0)