In first instance, load the image from file into a numpy matrix
from typing import Union,List
import numpy
import cv2
import os
def load_image(image: Union[str, numpy.ndarray]) -> numpy.ndarray:
# Image provided ad string, loading from file ..
if isinstance(image, str):
# Checking if the file exist
if not os.path.isfile(image):
print("File {} does not exist!".format(imageA))
return None
# Reading image as numpy matrix in gray scale (image, color_param)
return cv2.imread(image, 0)
# Image alredy loaded
elif isinstance(image, numpy.ndarray):
return image
# Format not recognized
else:
print("Unrecognized format: {}".format(type(image)))
print("Unrecognized format: {}".format(image))
return None
Then you can plot multiple image using the following method:
import matplotlib.pyplot as plt
def show_images(images: List[numpy.ndarray]) -> None:
n: int = len(images)
f = plt.figure()
for i in range(n):
# Debug, plot figure
f.add_subplot(1, n, i + 1)
plt.imshow(images[i])
plt.show(block=True)
The show_images
method take in input a list of images that you can read iteratively using the load_image
method.