Revision 44f070504b5a4aa4f326cfcb9f048c0e1e10aa79 (click the page title to view the current version)

Colour

Changes from 44f070504b5a4aa4f326cfcb9f048c0e1e10aa79 to 24d93c199e0553a7107bb5acd03ac958bfc92b9a

---
title: Colour Models
categories: notes
---

+ [Briefing](http://www.hg.schaathun.net/talks/maskinsyn2023/colour/)


# Exercises

Please note that the goal of the three first exercises is to 
familiarise yourself with the colour space and with APIs to
detect colours.  You should make sure that you understand
what you are doing.  I have tried to design the exercises so
that you can use them to test your own understanding.

## Exercise 1. Explore the HSV colour space

1.  Try a couple of distinctly recognisable colours,
    such as red $(0.0,255)$, green $(0,255,0)$ and
    yellow $(0,255,255)$, and pale blue $(100,0,0)$.
    The vectors are given in eight-bit BGR as is standard in OpenCV.
2.  Look at the colour circles in the slides. 
    What would you (approximately) expect the HSV values to be?
3.  Use OpenCV to convert from BGR to RGB.  The following snippet
    considers this for green.

```
import cv2 
import numpy as np
greenBGR = np.uint8([[[0,255,0 ]]])

hsv_green = cv2.cvtColor(greenBGR,cv2.COLOR_BGR2HSV)
print (hsv_green)
```

4.  Do the colours match your expectation?

## Exercise 2.

1.  Take a real image and convert it to HSV.
    You will need one with bright colours.
2.  Plot each colour channel as a grey scale image.
    (Remember that hue has to be scaled into the appropriate range;
    probably multiply by $255/360$.)
3.  Compare the original image to each colour channel view.
    + Can you recognise colour features from the original image in
      the individual colour channels?
    + Based on the views, how would you describe the meaning of
      *hue*, *saturation*, and *value*?

## Exercise 3.


1.  Identify an object with an easily recogniseable colour.
    Try to find bounds for this colour in the HSV space.
    + For instance, orange could be defined as
      $$H\in[10, 25]; S\in[100, 255]; V\in: [20, 255]$$
2.  Mask out the pixels with colour within the bounds.
    + Remember that you can calculate boolean matrices
      in numpy like this:

```
A = I[:,:,0] < 25
B = I[:,:,0] > 10
orange = A & B
```

3.  Do the masked areas match what you would visually identify as the
    right colour?

There are many ways to visualise the result.  
One could be to replace areas in other colours with greyscale
(set saturation to zero) and plot only interesting areas in colour.


## Exercise 4.

1.  Make a scene with a few distinctly coloured objects.
2.  Take a picture with your own colours. 
3.  Try to build a prototype identifying the objects using colour.
    You may want to combine your colour detector with 
    *connected components* from the last session.
    + You may want to combine your colour detector with 
      *connected components* from the last session.
    + Ignore single pixels and very small coloured areas.

## Project tracker

+ Set up a moving scene with a moving object
    + No constraints, but keep it simple for your own sake
    + Brightly coloured box sent sliding across a smooth table
+ Record a video using your own camera
+ Program a prototype to
    + Locate the object in a single frame
    + Track the object from one frame to the next
+ To make a robust solution it is wise to combine techniques
    + Detect brightly coloured faces
    + Detect corners - are they adjacent to the face?
    + Track features differentially (potentially high precision)
    + Redect features in new frame and match with SIFT.
      Do you find the features where they should be?


# Notes

+ [HSV versus RGB](https://discover.hubpages.com/technology/Color-spaces-RGB-vs-HSV-Which-one-to-use)
+ [Colour Detection](https://medium.com/@gowtham180502/how-to-detect-colors-using-opencv-python-98aa0241e713)