We run our labs with Python 3.6+. For Windows, you might want to use Conda.
- OpenCV is a library framework for developing computer vision solutions.
- It is widely used in industry and research.
- It is (mainly) free for both academic and commercial use.
- It has C++, C, Python and Java interfaces and supports Linux, Windows, Mac OS, iOS and Android.
- OpenCV is designed for computational efficiency and with a strong focus on real-time applications.
- Your first task is to setup OpenCV on the lab machines or your own machine. We use Python throughout all labs.
- Start familiarising yourselves with the basics of OpenCV, such that after the lab you should be able to compile and run OpenCV programs, to create, draw into, load, save and display images, and to manipulate pixels.
- Get familiar with basic representation of the image: pixels, channels, and colours
- Open a terminal and create virtual environment:
$ python -m venv myproject
, orconda create -n myproject
- Activate your environment:
$ source myproject/bin/activate
, orconda activate pytorch-env
- Then it shows:
(myproject) [username@it0number ~]$
- Might need to update pip:
$ pip install --upgrade pip
- Install OpenCV packages:
$ pip install numpy opencv-python
, orconda install numpy
thenconda install -c menpo opencv
- To get out from the environment (when you finish your work):
$ deactivate
, orconda deactivate
There are two ways to use the lab sheet, you can either:
- create a new repo from this template - this is the recommended way
- download a zip file
Study hello.py
and run it using python hello.py
.
Try to understand pixel representation in the image
display.py
to load and display an imagedraw.py
to create, draw and savepixels.py
to access and set pixel values
- Now that you are able to handle images, your next task is to write an OpenCV-based program that loads the
mandrill.jpg
greyscale image and that, pixel by pixel, sets all pixels above a certain value (maybe start with 128) to white (255) and all pixels equal or below the value to black (0). - Experiment with different thresholding values and examine the resulting images. Can you highlight certain parts of the face (e.g. the nose, the eyes) with one or more specific thresholds?
- Compare your results to the output of the inbuilt OpenCV function
threshold
.
Hint
Sample answers are available at `thr.py` if you are stuck.- Whilst in greyscale images the brightness of a pixel is usually represented as a single byte in 2D array, colour images use three bytes to store information for one pixel, become 3D array. Bytes represent the BLUE, GREEN and RED channel in this order.
- Now, try to implement thresholding of the red, green and/or blue channels to highlight facial components in mandrillRGB.jpg, which now contains colour information.
- Also check the OpenCV function
inRange
.
Hint
Sample answers are available at `colourthr.py` if you are stuck.Your task is to recover as much of the original mandrill colour image (3 channels, 8 bits per channel) from corrupted images, mandrill0.jpg
, mandrill1.jpg
, mandrill2.jpg
and mandrill3.jpg
.
- View the corrupted images and, for each image, make a prediction of what has happened to it. Maybe look at individual colour channels or histograms to investigate your hypothesis.
- Write a small OpenCV program for each image which can reconstruct the original mandrill colour image from it as well as possible.
Some images cannot be fully reconstructed.
- Original mandrill
mandrillRGB.jpg
- Corrupted images,
mandrill0.jpg
,mandrill1.jpg
,mandrill2.jpg
andmandrill3.jpg