W2 Flashcards
How are images stored in a computer?
- An image is composed of pixels stored in a 2-dimensional grid.
- Each pixel has only one colour.
How is data stored in each pixel?
- Each pixel contains finite, discrete quantities of numeric representation for its
intensity
EXAMPLE: Each number represents the intensity of the black colour of the
corresponding pixel.
What infomation is needed to represent a greyscale image in python?
- Information needed:
- Pixel values
- Locations
- We can use:
- An integer to represent the grey level of each pixel in a nested sequence (like list of lists to represent the 2-dimensional grid), and we can specify each element by their location
- EXAMPLE:
zero = [[ 0, 0, 5,13, 9, 1, 0, 0],
[ 0, 0,13,15,10,14, 5, 0],
[ 0, 3,15, 2, 0,11, 8, 0],
[ 0, 4,12, 0, 0, 8, 8, 0],
[ 0, 5, 8, 0, 0, 9, 8, 0],
[ 0, 4,11, 0, 1,12, 7, 0],
[ 0, 2,14, 5,10,12, 0, 0],
[ 0, 0, 6,13,10, 0, 0, 0]]
How do you ensure you import the relevant module for visualising colours?
import matplotlib.pyplot as plt
plt.figure(figsize=(2,2))
plt.imshow(zero, cmap=plt.cm.binary)
plt.gca().get_xaxis().set_ticks([])
plt.gca().get_yaxis().set_ticks([]);
How can you locate values in a list of list?
list [list 1][index 2]
EXAMPLE:
zero[1][2] # “row” with index 1 and “column” with index 2 (2nd “row” and 3rd “column”)
What is the RGB colour model?
RGB is an additive colour model where colours are created by combining varying intensities of primary colours (red, green, and blue) light.
- By mixing red, green, and blue colour light with different intensities, we can form a broad range of colours.
How can you represent each colour in the RGB colour model?
We can represent each colour by 3 numbers in the RGB24 format (colour channels), which correspond to the intensities of red, green, and blue.
*Each colour can be represented by 3 integers (r, g, b), with each integer in the range of [0, 255]
- 0 means the lowest intensity and 255 the highest intensity
- Each colour intensity is stored in a byte, so a colour is represented by 24 bits
- There are discrete combinations of R, G, and B values are allowed
- They can represent millions of different colours with different hue, saturation, and brightness.
- EXAMPLE:
(255, 127, 80) represents an orange colour. It has relatively high red, medium green, and low blue intensity - EXAMPLE:
- Black is rgb(0, 0, 0)
- White is rgb(255, 255, 255)
How can we represent a greyscale image in Python?
Using a nested sequence like a list of lists to represent a grid and then for each pixel, assigning a number to represent its grey level.
gray_img = [[0, 128],
[256, 0]]
plt.imshow(gray_img, cmap=plt.cm.gray)
plt.gca().get_xaxis().set_ticks([]);plt.gca().get_yaxis().set_ticks([]);
NOTE: Lower values represent darker colours so it behaves similar to the RGB model
How can we represent a colour image in Python?
For colour images, each pixel has 3 numbers to represent its colour. Therefore, now we need a nested sequence like a list of list of list to represent an image.
EXAMPLE:
colour_img = [[[255, 127, 80], [222, 49, 99]],
[[159, 226, 191], [64, 224, 208]]]
plt.imshow(colour_img)
plt.gca().get_xaxis().set_ticks([]);plt.gca().get_yaxis().set_ticks([]);
- NOTE: To locate each value, 3 indexes are required. For instance: “row” with index 0,
“column” with index 1,
The “B” value of the pixel is given by colour_img[0][1][2]
What is an extension of the RGB model with a fourth channel?
RGBA (red green blue alpha) is an RGB colour model supplemented with a fourth-channel alpha
- Alpha indicates how opaque (or transparent) each pixel is, with 0 being fully transparent and 255 being fully opaque.
- EXAMPLE: With the last integer representing how opaque each pixel is:
colour_img = [[[255,0,0,0], [255,0,0,64], [255,0,0,128], [255,0,0,192], [255,0,0,255]]]
What is the HSB colour model?
HSB (hue, saturation, brightness) is another colour model.
- It describes colours based on three psychological dimensions of colours:
- Hue: “Type” of colour like red, green
- Hue can typically be represented quantitatively by a single number, like an angular position around a colour wheel - Saturation (or chroma):
- “Colourfulness”, “vividness” of a colour
– 0%: grayscale image
– 100%: full saturated - Brightness (or value):
- 0%: black
- 100%: full intensity
NOTE: HSB is a cylindrical-coordinate representation of colours:
How do you import HSB and display: 1. Different hues (same saturation and brightness), 2. Different saturations (same hues and brightness), 3. Different brightnesses (same hues and saturations)?
from matplotlib.colors import hsv_to_rgb
- Different hues (same saturation and brightness)
hsv_1 = [[[h, 1, 1] for h in np.arange(0, 1, 0.125)]]
rgb_1 = (hsv_to_rgb(hsv_1)*255).round().astype(np.uint8)
plt.imshow(rgb_1)
plt.gca().get_xaxis().set_ticks([]);plt.gca().get_yaxis().set_ticks([]);
** 2. Different saturations (same hues and brightness)
hsv_2 = [[[0, s, 1] for s in np.linspace(0, 1, 9)]]
rgb_2 = (hsv_to_rgb(hsv_2)*255).round().astype(np.uint8)
plt.imshow(rgb_2)
plt.gca().get_xaxis().set_ticks([]);plt.gca().get_yaxis().set_ticks([]);
*** 3. Different brightnesses (same hues and saturations)
hsv_3 = [[[0, 1, v] for v in np.linspace(0, 1, 9)]]
rgb_3 = (hsv_to_rgb(hsv_3)*255).round().astype(np.uint8)
plt.imshow(rgb_3)
plt.gca().get_xaxis().set_ticks([]);plt.gca().get_yaxis().set_ticks([]);
How is digital audio saved in a computer and what information is needed?
In digital audio, the sound wave of the audio signal is typically encoded as numerical samples in a sequence.
- Two types of information are needed:
1. Samples, which is represented as a sequence of numbers - It can also be 2 sequences (for left and right ears)
- Sample rate (how many samples to take per second)
How can you import sound data in python?
from scipy.io import wavfile
samplerate, sound_data = wavfile.read(‘data/meow.wav’)
NOTE: You can then check the samplerate and sound_data by using them as commands
How can you play sound in python?
import sounddevice
sounddevice.play(sound_data, samplerate=samplerate)
NOTE. You may need to install ‘sounddevice’ first. You can then read the sound_data and samplerate
How can you visualise a sound wave on Python?
import numpy as np
import matplotlib.pyplot as plt
time = np.arange(0, len(sound_data))/samplerate
plt.plot(time, sound_data); plt.xlabel(“Time [s]”); plt.ylabel(“Amplitude”);
How are videos saved in a computer?
In a computer, video is composed of a sequence of still images (“frames”) with audio.
- Given that images and sounds are sequential data, shuffling an image or audio will mean that the video returned is not synced
What are modules?
Module: a collection of useful functions and variables for some specific purpose
EXAMPLE: You typically install a package and then import/use modules contained within it:
from scipy.io import wavfile
samplerate, sound_data = wavfile.read(‘data/meow.wav’)
import sounddevice
sounddevice.play(sound_data, samplerate=samplerate)
What is the Python standard library?
The Python standard library provides a wide range of modules, with many useful for data science.
- You do NOT need to install the library or the modules from it, as they come with Python
What are some EXAMPLES of the modules from the Python standard library?
- math: provide mathematical functions and constants
- random: generate pseudo random number from various distributions
- os: provide functions for interacting with the operating system
- datetime: represent and manipulate dates and times
- pickle: serialising and de-serialising Python objects
NOTE: More modules can be found here: https://docs.python.org/3/library/index.html
How do you use a module?
- To use a module, one needs to first import it.
- Once a module is imported, it can be used for the rest of the script:
EXAMPLE:
import random
random.random()
How can you use the random module?
- Generate a “random number” in the range [0.0, 1.0)
random.random()
- Generate a ‘random integer’ in the range (1, 6)
random.randint(1, 6)
What command will provide more information on the purpose of a module or a specific function?
help()
EXAMPLE:
* For a whole module: help(random)
- For a specific function:
help(random.random)
What are the 4 common ways to import a module, or objects from a module?
- import random
* Creates a reference to the module ‘random’ in the current namespace - import random as r
* Creates a reference to the module random in the current namespace via the alias ‘r’ - from random import randint
* Creates references to the specified object ‘randint’ in the module in the current namespace - from random import *
* Creates references to all public objects (e.g. randint, randrange, sample, seed) defined by the module in the current namespace