Lab 1 (Python Setup and Image I/O)

Computer Vision I (CSCI 3240U)

Faisal Z. Qureshi

Faculty of Science, Ontario Tech University

http://vclab.science.ontariotechu.ca

Check Canvas for Due Date


Introduction

The goal of this lab is twofold: (1) set up the Python environment that you will use for every lab in this course, and (2) get comfortable reading, writing and displaying images and video with OpenCV.

Everything we do for the rest of the term sits on top of this. Take the time to get it working properly now.

Reading

Computer Vision: Algorithms and Applications (2nd ed.), Szeliski — Sec. 1.1, 2.1.

Introduction; geometric primitives and image formation.

Data

Everything you need is beside this handout: the image test.jpg and the video traffic-short.mp4, a clip from a fixed traffic camera on an Ontario highway in winter. No download is required.

Installation steps

The following steps assume that you are using a Linux/OSX distribution.

Installing Python

To see if you have Python3 installed, use the following command:

$ python3 --version

If Python3 is not found, you can install it as follows:

$ sudo apt-get update
$ sudo apt-get install python3

Installing pip3

Next you will need pip3 to install the packages needed for this course. You can see if pip3 is already installed by using the following command:

$ command -v pip3

If pip3 is not installed, you can install it using the following command:

$ sudo apt-get install python3-pip

In any case, it is a good idea to upgrade pip3 to the latest version. This can be done using the following command:

$ python3 -m pip install --upgrade pip

Using a virtual environment

I strongly recommend that you install this course’s packages into a virtual environment rather than into your system Python. This keeps the course setup isolated from anything else you may be working on.

$ python3 -m venv cv-venv
$ source cv-venv/bin/activate

You will need to activate this environment (the source line above) every time you start work on a lab.

Installing requirements

Now install the packages needed for this course using the requirements.txt file available here:

$ pip3 install -r requirements.txt

If all goes as planned, you should be able to open the following Jupyter Notebook: setup-workbook.ipynb.

Tasks

Complete the tasks in the workbook setup-workbook.ipynb [html]. The workbook asks you to do the following.

1. Images

  1. Load and display the image test.jpg.
  2. Print the image height, width and the number of channels.
  3. Convert the image to grayscale and display it.
  4. Print the height, width and the number of channels of the grayscale image. Explain any difference from what you printed in step 2.
  5. Save the grayscale image to test_gray.jpg.

2. Video from a file

Display the first frame of the video traffic-short.mp4.

  1. Open a video stream.
  2. Read in the first frame.
  3. Read in the second frame.
  4. Find the difference between the two frames.
  5. Display the first frame, the second frame and the difference as seen below.
  6. Close the video stream.

3. Video from a webcam

Repeat the exercise above, this time using your webcam as the source.

  1. Open a camera stream.
  2. Read in one frame.
  3. Read in another frame.
  4. Find the difference between the two frames.
  5. Display the first frame, the second frame and the difference (your frames will of course be different from mine).
  6. Close the camera stream.

If you do not have a webcam, say so in your notebook and skip this task. You will not lose marks for it.

Deliverables

Your notebook must contain the following.

Submission

Via Canvas. Please submit a single executed Jupyter notebook — one that has been run top to bottom, so that every figure and number listed above is visible in the submitted file. Code that has not been executed cannot be marked.

Parting thoughts