From 3877090cbf2d772f82cb76db2470a11a248daae7 Mon Sep 17 00:00:00 2001 From: Gasper Spagnolo Date: Sat, 15 Oct 2022 18:20:50 +0200 Subject: [PATCH] Gre na git --- assignment1/UZ_utils.py | 11 ++++++----- assignment1/solution.py | 22 ++++++++++++++-------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/assignment1/UZ_utils.py b/assignment1/UZ_utils.py index ef5ebd8..6dca506 100644 --- a/assignment1/UZ_utils.py +++ b/assignment1/UZ_utils.py @@ -11,9 +11,10 @@ import cv2 as cv2 from matplotlib import pyplot as plt from PIL import Image from typing import Union +import numpy.typing as npt -def imread(path: str) -> np.float64: +def imread(path: str) -> npt.NDArray[np.float64]: """ Reads an image in RGB order. Image type is transformed from uint8 to float, and range of values is reduced from [0, 255] to [0, 1]. @@ -21,10 +22,10 @@ def imread(path: str) -> np.float64: I = Image.open(path).convert('RGB') # PIL image. I = np.asarray(I) # Converting to Numpy array. I = I.astype(np.float64) / 255 - return np.float64(I) + return I -def imread_gray(path: str) -> np.float64: +def imread_gray(path: str) -> npt.NDArray[np.float64]: """ Reads an image in gray. Image type is transformed from uint8 to float, and range of values is reduced from [0, 255] to [0, 1]. @@ -32,10 +33,10 @@ def imread_gray(path: str) -> np.float64: I = Image.open(path).convert('L') # PIL image opening and converting to gray. I = np.asarray(I) # Converting to Numpy array. I = I.astype(np.float64) / 255 - return np.float64(I) + return I -def imshow(img: Union[np.float64, np.uint8], title=None) -> None: +def imshow(img: Union[npt.NDArray[np.float64], np.uint8], title=None) -> None: """ Shows an image. Image can be of types: - type uint8, in range [0, 255] diff --git a/assignment1/solution.py b/assignment1/solution.py index 82d036c..484f03a 100644 --- a/assignment1/solution.py +++ b/assignment1/solution.py @@ -1,5 +1,6 @@ import UZ_utils as uz import numpy as np +import numpy.typing as npt ####################################### # EXCERCISE 1: Basic image processing # @@ -9,23 +10,28 @@ def excercise_one() -> None: image = one_a() one_b(image) - # Display image -def one_a() -> np.float64: +def one_a() -> npt.NDArray[np.float64]: image = uz.imread('./images/bird.jpg') - print(image.shape) - print(image.dtype) uz.imshow(image, 'Birdie') - return np.float64(image) + return image # Convert loaded image into grayscale -def one_b(image: np.float64) -> None: - print(image) +def one_b(image: npt.NDArray[np.float64]) -> None: + grayscale_image = np.zeros(image.shape[:2]) + for i in range(image.shape[0]): + for j in range(image.shape[1]): + grayscale_image[i, j] = (image[i, j, 0] + image[i,j, 1] + image[i, j, 2]) / 3 + uz.imshow(grayscale_image, 'Birdie grayscale') + +def one_c(): + #todo + assert(1 == 1) + def main() -> None: excercise_one() - if __name__ == "__main__": main()