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
Visual Studio Code
Python Environment (>3.7)
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
(Recommended) Download the notebook file (*.ipynb) and run in VS.Code
Run directly on Google Colab
Basic Image Processing
(*.py) Read / Write / Display Image and Video
You must Read Documentation!! link
Download HGU logo image and rename HGU_logo.jpg
Image Link: HGU_logo
Image Folder:
C:\Users\yourID\source\repos\DLIP\Image\
Create a new python source file in Visual Studio Code
File Name:
DLIP_Tutorial_OpenCV_Image.pyandDLIP_Tutorial_OpenCV_Video.pyProject Folder:
C:\Users\yourID\source\repos\DLIP\Tutorial\PyOpenCV
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()
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

Thresholding
Manual Local Threshold
Example Code

Adaptive Threshold
Example code

Plot Histogram
Example Code
Morphology
Example Code

Color Segmentation (InRange)
Example code

Edge & Line & Circle Detection
Edge Detection
Example code 1
Example code 2

Circle Detection
Example code

Line Detection
Example code

Exercise
Beginner Level Exercise
Exercise 1
Apply Blur filters, Thresholding and Morphology methods on given images for object segmentation.

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

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.

Procedure:
Apply a filter to remove image noises
Choose the appropriate threshold value.
Apply the appropriate morphology method to segment coins
Find the contour and draw the segmented objects.
Exclude the contours which are too small or too big
Count the number of each different coins(10/50/100/500 won)
Calculate the total amount of money.
Last updated
Was this helpful?