Absolute Guide for Contour Detection Model Using Python

by kanmingxian in Circuits > Computers

1176 Views, 25 Favorites, 0 Comments

Absolute Guide for Contour Detection Model Using Python

Contour_result.png

Hi, my name is Ming Xian! In this guide, we will be touching on how to make your contour detection model using Python codes. For starters, any Integrated Development Environment (IDE) that can run Python scripts such as Jupyter Notebooks will do the job while for simplicity I highly recommend using Microsoft Visual Studio Code throughout the course. The link to download Microsoft Visual Studio Code is given at the link below.


https://code.visualstudio.com/download

Supplies

Image 1.jpeg
  • Your computer with Wi-Fi connected

Download the Necessary Packages

First, download the following packages through your terminal using pip install "your package". Feel free to copy and paste the code below into the terminal. Make sure to include the libraries and packages in your machine's PATH environmental variables.

  • pip install python
  • pip install numpy
  • pip install matplotlib
  • pip install opencv-python

Not to mentioned to have your python extension installed and enabled if you are using Microsoft Visual Studio Code

Open a New File (.py)

photo_2024-03-19_19-42-19.jpg

Next, open a new file in the form of (.py). For instance, Test.py. This tells your IDE that you are configuring this file in the form of Python as shown above.

Import the Necessary Libraries

photo_2024-03-19_20-10-02.jpg

Then, import the necessary libraries such as the following:

  • opencv as in cv2
  • pyplot from matplotlib

OpenCV libraries serve as the foundation for image detection while matplotlib libraries serve as the foundation for graph plotting.

Create Necessary Variables

photo_2024-03-19_20-08-53.jpg

Referring to the text in red for my Microsoft Visual Studio Theme, the variables created are as listed below and stored with a specific value:

  • image
  • edges
  • BLedges
  • contours
  • hierarchy


image = image file is stored inside here! Feel free to insert your image file, make sure the file is in the same directory as your coding file.

edges = uses Canny sublibraries from OpenCV to detect edges in the image.

BLedges = convert and store the image from the variable "edges" into black and white (BL stands for Black and White!)

contours = detect and store the contours in the image from the variable "edges".

hierarchy = stores the information about the topology of the image from the variable "edges"


In this guide, I have used a sample file name known as "Car1.jpg". Feel free to change it to your desired image file name.

Setting Up the Plot Regime

photo_2024-03-19_20-14-20.jpg

At this point, we will use pyplot sublibraries from matplotlib to generate a diagram with a 2x2 layout to show different views of the original picture.

First, the figures are set with the aspect ratios of 16:9 using plt.figure("size") sublibrary.

Secondly, the first figure (original image) is designated at the top left corner using plt.subplot("location") sublibrary followed by plt.imshow("image") to display the image.

The two actions above are repeated to designate a normal edge image and a black-and-white edge image at their respective location.

Notice that argument

plt.subplot(XYZ), X = number of rows, Y = number of columns, Z = position of the diagram (From left to right)

plt.xticks() & plt.yticks() are added to remove the scale at both the horizontal and vertical axis.

Draw Contours on Original Image

photo_2024-03-19_20-35-08.jpg

At the last line of the diagram shown, use cv.drawContours() sublibrary to map and draw the contours on the original image. To draw all contours, put (-1) into the next argument and the remaining arguments are the color, and thickness of the contours.


The color used is green which is coded in RGB format!

Finishing Touches

photo_2024-03-19_20-39-34.jpg

Repeat step 5 to show the contour image.

Counting the Number of Contours Detected

photo_2024-03-19_21-00-07.jpg

The number of contours can be counted straight using the len() function by assigning the variable "contours" as arguments.

Finally!

photo_2024-03-19_20-46-48.jpg

The results are then saved to their respective names (You can give any names you want!) using plt.imsave() as well as their respective formats (".png" file is preferred due to their great compatibility with other platforms!)

Output

photo_2024-03-19_21-05-49.jpg

Your output should look something like this.

Codes for Free!

I have attached the codings we used throughout this journey. Feel free to download it and distribute it to others so that everyone can learn! Happy learning!

Downloads