From d314b542fa01a6944cf0ab43266daa6521a0cf82 Mon Sep 17 00:00:00 2001 From: "dorian.veloso" Date: Fri, 22 Nov 2024 08:50:58 +0100 Subject: [PATCH] First Code for image processing --- roboticsworkshopimageprocessing.py | 65 ++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 roboticsworkshopimageprocessing.py diff --git a/roboticsworkshopimageprocessing.py b/roboticsworkshopimageprocessing.py new file mode 100644 index 0000000..d04eb8d --- /dev/null +++ b/roboticsworkshopimageprocessing.py @@ -0,0 +1,65 @@ +import numpy as np +import cv2 as cv +from matplotlib import pyplot as plt + +imagepath = "E:\\ECAM\\2022-23\\Pathway Discovery Workshops\\images-20230626\\ecam.png" + +# Load Image +im = cv.imread(imagepath) +if im is None: + print("Error: Image not found at", imagepath) + exit() + +# Get Dimensions +dimensions = im.shape +height, width, channels = dimensions +print('Image Dimension :', dimensions) +print('Image Height :', height) +print('Image Width :', width) +print('Number of Channels :', channels) + +# Resize Image +rwidth, rheight = 700, 700 +rdim = (rwidth, rheight) +Resized_Image = cv.resize(im, rdim, interpolation=cv.INTER_AREA) +cv.imshow("Resized Image", Resized_Image) +cv.waitKey(0) +cv.destroyAllWindows() + +# Convert to Grayscale +Gray_Img = cv.cvtColor(Resized_Image, cv.COLOR_BGR2GRAY) +cv.imshow("Grayscale Image", Gray_Img) +cv.waitKey(0) +cv.destroyAllWindows() + +# Threshold Image +ret, Thresh_Img = cv.threshold(Gray_Img, 100, 255, 0) +cv.imshow("Threshold Image", Thresh_Img) +cv.waitKey(0) +cv.destroyAllWindows() + +# Plot Histogram +hist = cv.calcHist([Gray_Img], [0], None, [256], [0, 256]) +plt.figure() +plt.title("Grayscale Histogram") +plt.xlabel("Bins") +plt.ylabel("# of Pixels") +plt.plot(hist) +plt.xlim([0, 256]) +plt.show() + +# Find and Draw Contours on Resized Image +contours, hierarchy = cv.findContours(Thresh_Img, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) +contoured_image = Resized_Image.copy() +cv.drawContours(contoured_image, contours, -1, (0, 255, 0), 3) +cv.imshow("Contours on Resized Image", contoured_image) +cv.waitKey(0) +cv.destroyAllWindows() + +# Draw Contours on Threshold Image +contoured_thresh = cv.cvtColor(Thresh_Img, cv.COLOR_GRAY2BGR) +cv.drawContours(contoured_thresh, contours, -1, (0, 255, 0), 3) +cv.imshow("Contours on Threshold Image", contoured_thresh) +cv.waitKey(0) +cv.destroyAllWindows() +