Tutorial: OpenCv (Python) Basics

Tutorial: OpenCV (Python) Basics

Preparation

Basics of Python & Numpy

Skip this if you already know about Python programming

Python Basics

Numpy Basics

Configuration

Prepare the environment as

  1. Visual Studio Code

  2. Python Environment (>3.7)

  3. OpenCV Installation

Follow the tutorial for installation

Source code and images

Download

Download the tutorial source code and image files.

Project and Data Folder

The downloaded images should be saved in

  • Image Folder: C:\Users\yourID\source\repos\DLIP\Image\

The python opencv tutorial is operated under the project folder

  • Python File Folder: C:\Users\yourID\source\repos\DLIP\Tutorial\PyOpenCV\

The visual studio code open project at DLIP folder

  • Project Folder: C:\Users\yourID\source\repos\DLIP\

Running the source code

This tutorial code is based on Google Colab Notebook.

When running the code, you can select from two options

  1. (Recommended) Download the notebook file (*.ipynb) and run in VS.Code

  2. Run directly on Google Colab


Basic Image Processing

(*.py) Read / Write / Display Image and Video

You must Read Documentation!! link

  1. Download HGU logo image and rename HGU_logo.jpg

    • Image Link: HGU_logo

    • Image Folder: C:\Users\yourID\source\repos\DLIP\Image\

  2. Create a new python source file in Visual Studio Code

    • File Name: DLIP_Tutorial_OpenCV_Image.py and DLIP_Tutorial_OpenCV_Video.py

    • Project Folder: C:\Users\yourID\source\repos\DLIP\Tutorial\PyOpenCV

  3. Compile and run.


(*.ipyn) Read / Write / Display Image and Video

Import OpenCV Library


(for COLAB only) Upload Image Files in Colab server

Skip this if you are using Visual Studio Code

Read how to load image file in Colab

Other Option: Upload image file to Colab server from local drive

Read Image File

Display Image using matplot plt.imshow()

This tutorial will use matplotlib functions for *.ipyn files. This method is recommended for showing images. This works for both *.py and *.ipyn files.

matplotlib has different rgb order than OpenCV

  • matplot: R-G-B

  • OpenCV: G-B-R

Display Image: (for .py only) OpenCV imshow()

This is only for *.py file. Python files running on local drive supports OpenCV cv.imshow()

Notebook files such as Colab and Jupyter does NOT support OpenCV cv.imshow()

This does not work on *.ipyn file

Display Image: (for Colab only) cv2_imshow()

CoLAB provides a similar function called cv2_imshow(). But this is NOT recommended method.

Import

from google.colab.patches import cv2_imshow as cv_imshow

Capturing Video

Using webcam in notebook(colab, jupyter) requires more complex setup.

cv.VideoCapture(0) is NOT available in Colab.

Spatial Filter

Example Code

image

Thresholding

Manual Local Threshold

Example Code

image

Adaptive Threshold

Example code

image

Plot Histogram

Example Code

Morphology

Example Code

image

Color Segmentation (InRange)

Example code

image

Edge & Line & Circle Detection

Edge Detection

Example code 1

Example code 2

image

Circle Detection

HoughCircles

Example code

image

Line Detection

Example code

image

Exercise

Beginner Level Exercise

Exercise 1

Apply Blur filters, Thresholding and Morphology methods on given images for object segmentation.

download test image

image

Example 2

Choose the appropriate InRange conditions to segment only ' Blue colored ball'. Draw the contour and a box over the target object. Repeat for Red, and Yellow balls

download test image

Example 3

Detect Pupil/Iris and draw circles.

Intermediate Level Exercise

Exercise: Count number of coins and calculate the total amount

After applying thresholding and morphology, we can identify and extract the target objects from the background by finding the contours around the connected pixels. This technique is used where you need to monitor the number of objects moving on a conveyor belt in an industry process. Goal: Count the number of the individual coins and calculate the total amount of money.

image

Procedure:

  1. Apply a filter to remove image noises

  2. Choose the appropriate threshold value.

  3. Apply the appropriate morphology method to segment coins

  4. Find the contour and draw the segmented objects.

  5. Exclude the contours which are too small or too big

  6. Count the number of each different coins(10/50/100/500 won)

  7. Calculate the total amount of money.

Last updated

Was this helpful?