From 4182a108a01df3aa28009716472f0ef291704866 Mon Sep 17 00:00:00 2001 From: Sudipto Mallick Date: Sat, 10 Feb 2024 12:49:55 +0000 Subject: Complete DIP assignments --- opencv/code/a10.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 opencv/code/a10.py (limited to 'opencv/code/a10.py') diff --git a/opencv/code/a10.py b/opencv/code/a10.py new file mode 100644 index 0000000..f54a024 --- /dev/null +++ b/opencv/code/a10.py @@ -0,0 +1,39 @@ +import cv2 +import numpy as np +import matplotlib.pyplot as plt + +images = [cv2.imread(path, cv2.IMREAD_GRAYSCALE) for path in ["9i1.jpg", "9i2.jpg"]] +assert all( + image.shape == images[0].shape for image in images +), "Input images must have the same dimensions" + +operations = [ + ("Addition", lambda x, y: cv2.add(x, y)), + ("Subtraction", lambda x, y: cv2.subtract(x, y)), + ("Multiplication", lambda x, y: cv2.multiply(x, y)), + ( + "Division", + lambda x, y: cv2.divide( + x.astype(np.float32), np.where(y == 0, 1, y).astype(np.float32) + ), + ), +] + +plt.figure(figsize=(8, 6)) + +for i, image in enumerate(images, start=1): + plt.subplot(3, 2, i) + plt.imshow(image, cmap="gray") + plt.title(f"Image {i}") + plt.axis("off") + +for i, (operation_name, operation_function) in enumerate( + operations, start=len(images) + 1 +): + result_image = operation_function(*images) + plt.subplot(3, 2, i) + plt.imshow(result_image, cmap="gray") + plt.title(operation_name) + plt.axis("off") + +plt.savefig("10.svg") -- cgit 1.4.1-2-gfad0