This is an introduction to Computer Vision using Python. I will be using online resources and examples whenever it is available. I'll provide inline reference to the source code, discussions and inspiration.
Lesson 1: Computer Vision Basics
- 1D, 2D and 3D arrays
- Read in a picture
- Grayscale a picture
- Subset an array and picture
Lesson 2: Blurring
- Primary Colors in computer vision is Red, Green and Blue
- Subtractive versus Additive color processes
- Why blur a picture
Lesson 3: Edge Detection
- How to write a function in Python
- What is a gradient (in the x-direction)
- Write your own gradient function (for the y-direction) for a grayscale picture
Lesson 4: Color Detection
- Bitwise operations
- Error Handling in Python
- Another color coding system (HSV = Hue, Saturation and Value)
- Finding contours
Lesson 5: Shape Detection
- Shapes
- SMore on contours
- SCentroid and Area from Contours
- Occulsion
Lesson 6: Shape Detection
- Angles
- Distance
- Distortions
The instructions below have been tested on Python 3.7. I mainly use Anaconda https://www.anaconda.com/products/individual > Products > Individual Edition as my virtual environment and install the necessary packages.
Here are the steps in creating your environment:
-
Install Anaconda according to the instructions for your operating system.
-
Create a python 3.8 with the basic packages as follows in your terminal via command line for Mac OS. For Windows, you will need to use the "Anaconda Prompt" and not the generic Window's Prompt.
prompt> conda create -n py38cv python=3.8 conda numpy scipy
It will ask you to confirm the installation of package, you enter "y" to confirm.
- Activate your environment
prompt> conda activate py38cv
- Install image processing libraries
prompt> conda install -c conda-forge scikit-image
- Install image processing functions
prompt> conda install -c conda-forge imutils
- Install OpenCV if not already installed
prompt> conda install -c conda-forge opencv
- To verify your environment, invoke the python interactive command prompt by executing "python" in your terminal.
prompt> python
Python 3.8.5 | packaged by conda-forge | (default, Jul 31 2020, 02:18:36)
[Clang 10.0.1 ] on darwin
>>>
- Type the following package import at the prompt:
>>> from skimage.measure import compare_ssim
>>> import argparse
>>> import imutils
>>> import cv2
>>> import numpy as np
>>> print(cv2.__version__)
>>> 4.4.0 # this can return any 4.x.x version depending on your operating system and when you installed opencv
- To exit from the python interactive session, type exit() and hit return.
>>> exit()
You should encounter no errors. Now your environment is ready to go for the lessons.
If you have issues importing imutils, try the following to install imutils.
prompt> conda install pip
prompt> pip install imutils
Or, for Windows 10 64-bit, try:
prompt> conda install -c pjamesjoyce imutils
If you set up your python environment using Anaconda, you will be able to run the Jupyter Notebook IDE for python. To start the Notebook IDE, activate the py38cv environment. Your prompt will change to (py38cv) when your environment is activated and execute "jupyter notebook" as follows.
(base)> activate py38cv
(py38cv)> jupyter notebook
This will start the Notebook IDE in your preferred browser as http://localhost:8888/?token=xxxx . You can always grab the link and paste it in your browser if needed.
To exit your Notebook environment, just save your notebook and close all Jupyter notebook tabs in your browser. On the command prompt from where your notebook was launched, run Ctrl-C to stop the IDE and type "y" to confirm. To deactivate your python environment run "conda deactivate".
Note: DO NOT USE the Anaconda Navigator (the GUI) to launch your Juypter notebook as it will not initiate your py38cv environment as required for these lessons.
>>> Ctrl-C [enter]
>>> conda deactivate
If you have trouble running "jupyter notebook", try installing or re-installing jupyter package.
prompt> conda install -c anaconda jupyter
Jupyter Lab provides the same functionality as Jupyter notebook but with better navigation. To get Jupyter Lab, install the following in your environment.
prompt> conda install -c conda-forge jupyterlab