Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
WORKSHOP
DAY 1
What is Computer Vision
• Computer Vision is the field of Computer Science which works
on enabling the computer to see, identify and process an
image in the same way as human eye does.
• The final objective is to extract some meaningful information
or output a decision.
• eg. :Is there an obstacle in front of the robot?
Does the image contain a shark?
How many humans are present in the given image?
Are there moving cars in the image?
What is Image Processing
• Image processing is a method to perform some operations on an
image, in order to get an enhanced image or to extract some useful
information from it.
• It is a type of signal processing in which input is an image and
output may be image or characteristics/features associated with
that image.
• Image processing basically includes the following three steps:
– Importing the image via image acquisition tools;
– Analysing and manipulating the image;
– Output image
What’s the difference
• Computer vision is related to image processing in the sense
that the computer vision front-end is comprised of image
processing techniques such as noise reduction, whitening or
image enhancement.
Image Processing : Computer Vision :
• The goal of image processing is to • The goal of computer vision is to extract
enhance or compress image/video meaningful information from
information. images/videos.
• Uses pixel-wise operations such as • Computer vision is not limited to pixel-
transforming one image into another. wise operations; it can be complex, like
For example applying a rotation on image classification, object detection
pixels. etc.
• There is no extraction of meaningful • The basic step of Computer Vision is
information from those pixel-wise processing of the image. In other words,
operations. img proc is a subset of Computer vision.
Components of an Image Processing System
Fundamental steps in Image Processing
Generating a digital image. (a) Continuous image. (b) A scaling line from A to B in the continuous image, used to
illustrate the concepts of sampling and quantization. (c) sampling and quantization. (d) Digital scan line.
128 128 128 200 200 255 255 200 200 200
75 75 75 35 35 35 0 0 0 35
35 35 35 0 0 0 35 35 35 75
32
64
128
256
512
1024
Sampling
128 64 32
PYTHON INTRODUCTION
What is Python
• A readable, dynamic, flexible, fast and powerful language
• Multi-purpose (Web, GUI, Scripting)
• Object oriented
• Cross Platform
• Python cares about Indentation
Indentation
One of the distinctive features of Python is its use of indentation to
mark blocks of code
In this snippet, the else part In this snippet, the else part
belongs to the 1st if statement belongs to the 2nd if statement.
Comments
• Single line comments are created simply by adding a hash
character (#) at the beginning of the line.
eg.
String are immutable in python i.e after defining a string variable you can explicitly replace or change
that variable i.e.
s = “Hello”
s[0] = “A” is not permitted in python.
String can be appended using ‘+’ operator
for example:
a = “Hello”, b = “World”, c = a + b, then c becomes “Hello World”
Data Types in Python
• List – A list can contain a series of values. It can be heterogeneous i.e. it can store values of different
types, such as [“A”, 1, 2, “3”, 4.0]
a) Operation b) Result
Data Types in Python
• All lists are zero-based indexed by default i.e. the first
element is present at index 0 .
•
Input via keyboard
• There are cases where input from the keyboard is required for program execution, for this purpose
python provides input() function, as soon as the input() function is encountered the program
execution pauses until an input is provided by the keyboard.
• Consists of one argument, the prompt text, the text that appears on the screen when the program
requests for an input.
• Default type of input is string so to convert it into another type, explicit conversion is required.
a) Program b) Result
NUMPY INTRODUCTION
What is NumPy
• NumPy is a library for Python language, standing for Numerical Python.
• It adds support for large multi-dimensional arrays and matrices; and a large
collection of high-level mathematical functions to operate these arrays.
• Operarations using NumPy :
– Mathematical and logical operations on arrays.
– Fourier transforms and routines for shape manipulation.
– Operations related to linear algebra. NumPy has inbuilt functions for linear algebra and random
number generation.
• NumPy is often used along with packages like SciPy (Scientific Python) and
Mat-plotlib (plotting library).
How to use Numpy with Python
• Standard Python distribution doesn't come bundled with
NumPy module.
• We need to install NumPy using Python Package Installer :
pip install numpy
All the OpenCV array structures are converted to-and-from Numpy arrays.
Color Spaces in OpenCV
• The purpose of a color model (color space) is to facilitate specification of colors
in some standard way.
• Each color model represents a color by a single point in a coordinate system and
its subspace.
• RGB (red, green, blue) model is for color monitors and a broad class of color
video cameras.
• CMY (cyan, magenta, yellow) and CMYK (cyan, magenta, yellow, black) models
are for color printing.
• HSI (hue, saturation, intensity) model, which corresponds closely with the way
humans describe and interpret color. The HSI model also has the advantage that
it decouples the color and gray-scale information in an image, making it suitable
for many of the gray-scale techniques
Color Spaces in OpenCV
• Color space, also known as the color model, is an abstract mathematical model which simply
describes the range of colors as tuples of numbers.
• Some of the basic color spaces available in OpenCV are :
– BGR : In BGR, an image is treated as an additive result of three base colors (blue, green and red). The BGR ranges are:
• 0 < B < 255
• 0 < G < 255
• 0 < R < 255
Color Spaces in OpenCV
– HSV : HSV stands for Hue, Saturation and Value (Brightness). We can say that HSV is a
rearrangement of RGB in a cylindrical shape, where HUE gives the angular dimension.
– SATURATION: It tells how saturated a colour is. For e.g.- a Crimson red colour will have a high value
– of saturation compared to light red.
– VALUE: It is a component which varies with lighting condition. A particular colour place in a bright
light will have a higher VALUE component than the same colour placed in a dimmer lighting
condition.
– The HSV ranges are:
• 0 > H > 360 ⇒ OpenCV range = H/2 (0 > H > 180)
• 0 > S > 1 ⇒ OpenCV range = 255*S (0 > S > 255)
• 0 > V > 1 ⇒ OpenCV range = 255*V (0 > V > 255)
Basic Operations in OpenCV
• Reading an image
• Displaying an Image
• Writing an Image
• Reading a video stream
• Pixel operations
• Thresholding
Reading an image
• Use the function cv2.imread() to read an image.
• The image should be in the working directory or a full path of image
should be given in the first argument.
• Second argument is a flag which specifies the way image should be read.
– cv2.IMREAD_COLOR : Loads a color image. Any transparency of image will be
neglected. It is the default flag.
– cv2.IMREAD_GRAYSCALE : Loads image in grayscale mode
– cv2.IMREAD_UNCHANGED : Loads image as such including alpha channel
– Instead of these three flags, you can simply pass integers 1, 0 or -1 respectively.
• eg.: >>> import numpy as np
>>> import cv2
• If you are using a 64-bit machine, you will have to modify k = cv2.waitKey
(0) line as cv2.waitKey (0) & 0xFF
• cv2.destroyAllWindows() simply destroys all the windows we created.
– If you want to destroy any specific window, use the function
cv2.destroyWindow() where you pass the exact window name as the argument.
Writing an Image
• Use the function cv2.imwrite() to save an image.
• First argument is the file name.
• Second argument is the image you want to save.
>>> cv2.imwrite('messigray.png',img)
Capturing Video from Camera
• You need to create a VideoCapture object.
• Its argument can be either the device index or the name of a video file.
• Device index is just the number to specify which camera. Normally one
camera will be connected. So pass 0 (or -1).
• You can select the second camera by passing 1 and so on.
Playing Video from File
Pixel Operations
• You can access a pixel value by its row and column coordinates.
• For BGR image, it returns an array of Blue, Green, Red values.
• For grayscale image, just corresponding intensity is returned.
>>> px = img[100,100]
>>> print px
[157 166 200]
• https://docs.opencv.org/3.1.0/d6/d00/tutorial_py_root.html - OpenCV
• https://python-course.eu/ - Python
• Digital Image Processing – Rafael C. Gonzalez, Richard E. Woods