Pages

Tuesday, December 19, 2023

PART 1: ANALYSE IMAGE WITH OPENCV (PYTHON) - DETECT EDGES / OUTLINE IN IMAGE

Hi! I'm writing this post in my blog because currently I'm learning Computer Vision and Deep Learning with OpenCV and Python. So basically, I post my progress here so I can refer for myself.

I'm using https://colab.research.google.com/ to run codes.. but you can use any Python platforms.

ANALYSE IMAGE WITH OPENCV (PYTHON) - DETECT EDGES / OUTLINE IN IMAGE

First install OpenCV:

!pip install opencv-python

Or if you already install it, you can just upgrade into latest version:

!pip install opencv-contrib-python --upgrade

Then import image, you can input any image links:

import cv2

!wget "https://www.supercoloring.com/sites/default/files/vif/2017/10/elephant-logo- mascot-template-vector-vector.png"

Create image variable. Saving to: 'Image Name':

image = cv2.imread("elephant-logo-mascot-template-vector-vector.png")

Load image using matplotlib:

import matplotlib.pyplot as pyplot
pyplot.rcParams["figure.figsize"] = (5, 5)    #set parameters - size of image

pyplot.imshow(image)


Detect the edges of the image (Create canny object passing in image):

detected_edges = cv2.Canny(image, 100,100)

pyplot.imshow(detected_edges)


Or.. can use CV library to show image:

from google.colab.patches import cv2_imshow

cv2_imshow(detected_edges)


Invert the image colour:

inverted_image = 255 - detected_edges

cv2_imshow(inverted_image)


Save new image:

cv2.imwrite("Edges.png", cv2.cvtColor(inverted_image, cv2.COLOR_RGB2BGR))

The end! Now you have the edges of the image!