49
Computer Vision Pictures as Functions Image Function (I) maps the values at pixels I (x, y) -> Returns value for pixel at x,y position in the image Color Images: I(x, y,) -> Returns array [r, g, b] for pixel BW Images I(x,y) -> Returns single value for pixel

Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Computer Vision

Pictures as Functions

Image Function (I) maps the values at pixels

I (x, y) -> Returns value for pixel at x,y position in the image

Color Images: I(x, y,) -> Returns array [r, g, b] for pixel

BW Images I(x,y) -> Returns single value for pixel

Page 2: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Pictures as Functions: Python

Page 3: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Pictures as Functions and numpyarrays: Python Color Images• Mathematical

Expression:

I(100, 50) = {61, 150, 184}

• Python Expression

img[50][100]

# Returns:

array([ 61, 150, 184], dtype=uint8)

NOTE! In Mathematical expressions we write I(x, y)

In programming (most languages) we write image arrays as:img_array[row][column]

-That becomes img_array[y][x]

Page 4: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Pictures as Functions and numpyarrays: Grayscale images• Mathematical

Expression:

I(100, 50) = 150

• Python Expression

img_g[50][100]

# Returns value of 150

NOTE! In Mathematical expressions we write I(x, y)

In programming (most languages) we write image arrays as:img_array[row][column]

-That becomes img_array[y][x]

Page 5: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Numpy Arrays in Python

• Numpy Arrays can be written two ways:

• Nested:• img[50][100][3]

• Means Row Index 50, Column Index 100, Blue Channel

• Vectorised• img[50, 100, 3]

Page 6: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Why Vectors? Can call range of Values without For Loops• Slow Way: Iteration

# Nested For Loop

part_img = [[]]

fill = []

for r in range (50, 100):

fill = []

for c in range(60, 80):

fill.append(img_g[r][c])

part_img.append(fill)

• Fast Way: Vectors and Numpy

part_img = img_g[50:100, 60:80]

# Yes – this one line is equal to

# the nested for loop on the left!

# And, this is much faster to

# to process – needed for real time

# Movie processing

Page 7: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Some Python Examples:

Page 8: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Vectorised Array: Set Green and Blue to 0

Page 9: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Vectorised Array: Divide entire picture by 2

Page 10: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Vectorised Array: Get the Horizontal Derivative

Page 11: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Picture Values

• Usually two choices:• 0 to 255: 0 is dark, 255 is completely bright

• 0 to 1: 0 is dark. 1 is Bright. This is sometimes called “Normalized Value” Usefull in statistical analysis.

• Data Types• Be CAREFUL! Images by default are unsigned 8 bit

numbers (0 to 255 with no decimals)

• When doing processing and calculations – convert the image array to a float 32 bit number before the math. Or, the values will be lost in calculations!

Page 12: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Python Language Guide

• Interpreted: Does not need to be compiled

• Sequential – Define functions before using

• Object Orientated (Like Java)• Both Java and Python are built on C and C++

• Can code short ideas in real time in Interpretter

• Write Longer Text Files and Packages (Called Modules)

Page 13: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Python Language Guide

# Single Line comment

“”” Multi line

Comment “””

# No semicolons in Python!

(But they are OK to use . . .)

Page 14: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Python Variables

• Same datatypes as Java (Boolean, int, float, double, String . . .)

• Do not need to declare type of variable before creating

• Java:int age = 14;

• Pythonage = 14

Page 15: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Python (and Numpy) Lists / Arrays

• Pure Python Arrays are mutable (Like ArrayList or List in Java)

students = [“John”, “Frank”, “George”]

# Note use of brackets for array.

students.append(“Mr. Michaud”)

# Adds “Mr. Michaud” to List

Page 16: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Python Lists(Arrays)

• Python Indexing of Lists/Arrays is like java.

• Zero based Indexing

students = [“John”, “Frank”, “George”]

# Note use of brackets for array.

students[0] # Returns “John”

Page 17: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Python List Functions

• len(array) will return length of array

numbers = [3, 8, 7]

len(numbers) # returns 3

# Numpy: min, max, sum

numbers.min() # Returns 3

numbers.max() # Returns 8

sum(numbers) # Returns 18

Page 18: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Numpy Array Operations

import numpy as np #Import numpy library as np

img = cv2.imread(path, 0)

# Grayscale image as numpy array

len(img) # returns number of rows in np array

len(img[0]) # returns number of columns in np array

np.where(array == value)

# Returns Array object with point location of value

Page 19: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Control Structures in Python

• Iteration:

• Java

for (int i = 0; i < 10: i++) {

System.out.println(i);

}

• Python

for i in range(10):

print (i)

Page 20: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

If Statements in Python# Standard If statement

if (x > 200):

pin9.write(120)

# Else Statements

if (x > 200):

pin9.write(190)

else if(x < 150):

pin9.write(60)

else:

pin9.write(90)

Page 21: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

While Statements

# While Statement: 100 Frames shown

while(x < 100):

ret, frame = cap.read()

if (ret == true):

cv2.imshow(“Picture”, frame)

x = x + 1

Page 22: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Defining Functions in Python

• Do not need to specify return types

• Keyword is “def”

def addTwoNumbers(x, y):

return x + y

Page 23: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Longer Function Example

# Function to return dx

def getDX(frame):

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

diff = np.float32(gray[:, 2:len(gray[0])]) - np.float32(gray[:, 1:len(gray[0])-1])

out = diff + -1*diff.min()

out = out / out.max()

return out

Page 24: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Python and Tabs

• Python uses Tabs to encapsulate code. (No curly brackets in Python)

• For loops, conditionals, while loops, and functions all use :’s and tabs to encase code.

• Be very careful with tabs in Python. If you get a parse error – your tabs are not consistent.

Page 25: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Open Computer Vision 2

• Library to handle image Processing

• Uses Numpy and Scipy for graphics and computer vision.

• Provides access to cameras

• Reading and writing image files

• Creating Image arrays

• Filters, Harr Cascades, and many other features used in Computer Vision.

• Standard Library for Research at University and Industry applications.

Page 26: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Selected Open CV and numpy API commandsimport cv2 as cv2

import numpy as np

#Imports CV2 Library as object cv2

cv2.imread()

cv2.cvtColor()

cv2.matchTemplate()

cv2.imshow

cv2.rectangle

cv2.VideoCapture()

cv2.waitKey()

cv2.destroyAllWindows()

np.copy()

Page 27: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Open CV File Operationscv2.imread(pathToFile, channels)

pathToFile: Address in file system to image

channels: Number of colors

3: R, G, B Full Color

0: Grayscale (One Channel)

Returns an Image array

Example:

#Path to image

path = "C:\Users\michaudc\OneDrive\BCT 440 Computational Perception\images\lisa.png"

#Open Image and assign to np array. Color Image

img = cv2.imread(path, 3)

Page 28: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Open CV Image Operations

cv2.cvtColor(image, ConversionType)

Converts between color and grayscale images.

Returns an Image array

Example:

frame = cv2.imread(path, 3) # color image

gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

Page 29: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Open CV Image Operations

cv2.rectangle(image, (x1,y1), (x2, y2), color, thickness)

Draws a rectangle on an image file. Color is (B, G, R) tuple. x1 and y1 are upper left corner. x2 and y2 are lower right corner.

Example:cv2.rectangle(image, (x, y), (x+50, y+50), (0, 0, 255), 2)

Page 30: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Open CV Image Output

cv2.imshow(‘WindowName’, image)

‘WindowName’: Name of Window

image: numpy array object

Example

cv2.imshow(‘My Picture’, image)

Page 31: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Writing images to files

Example:

• Convert image array to 0-255 np.uint8 datatypeimg = img * 255

img = np.uint8(img)

• Use the cv2.imwrite(path, image) to write to filecv2.imwrite(‘output\name.png’, img)

cv2.imwrite(pathToFile, imageArray)

Page 32: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Template Matching (More on this later . . .)cv2.matchTemplate(img_g, patch, type)

img_g: Grayscale image to be searched

patch: Image patch to search for

type: Type of of search (Normalized correlation, Sum of Squared Differences . . .)

Returns a map of values from the filter

Example:f = cv2.matchTemplate(gray, patch, cv2.TM_CCORR_NORMED)

Page 33: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Using the Video Camera

cv2.VideoCapture(camera)

Returns a Camera object for video image capture.

camera: Number assigned to camera in system. Usually ‘0’ is the internal camera and ‘1’ is an external USB camera.

Example:

cap = cv2.VideoCapture(0)

Page 34: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Using the Video Camera

cameraObject.read()

Returns two objects to program:

Boolean value if the camera is working

Numpy Image array of captured image

Example

ret, frame = cap.read()

Page 35: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Create Numpy Array

np.array(arrayData)

Creates a Numpy array

Example: Creates a 2 x 4 array of integers

img = np.array([[4, 3, 5, 6],

[3, 5, 7, 8]])

Page 36: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Convert Datatype in Numpy Array to 32 bit floating pointnp.float32(arrayData)

Converts incoming data to floating point 32 bit numbers. Returns the an array with the converted and copied data

Example:

patch = np.float32(img)

Page 37: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Copy Numpy Arrays (image data)

np.copy(numpyArray)

Takes input of numpy array and creates a new numpyarray and copies the data.

Example:

# img is an existing numpy image array

img_copy = np.copy(img)

Page 38: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

2D Filter Function (Convolution)

cv2.filter2D(img, -1, kernel)

Takes input of grayscale img data filters with kernel.

The kernel is an m x m array used to filter the data. Usually a Gaussian, Laplacian, Sorbel, or box filter.

Example:

# img is an existing numpy image array

kernel = np.ones((5, 5), np.float32)/25

box_blur = cv2.filter2D(img, -1, kernel)

Page 39: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

CV2 Canny Edge Function

• Built in Edge detection function in Open CV that uses a more involved algorithm:• Filters image with derivative of Gaussian

• Uses magnitude and orientation of gradients

• Non-Maximum suppression

• Linking and thresholding

• Result is a more refined edge image.

img_edge_canny = cv2.Canny(img_gauss, 50, 100)

Page 40: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Gaussian Blur: Role of Sigma

• We will blur images with the cv2.GaussianBlur() function.

cv2.GaussianBlur(img, (m x m), sigma)

Where:

img = Image Array to Blur

(m x m): size of ‘window’ of Gaussian (odd number)

sigma: The spread of the blur (High for more depth)

Page 41: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

2D Gaussian Filter Image

Page 42: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Example:

Page 43: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Why Blur with Gaussian?

• Remove Noise from the Picture.

Page 44: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Code Sample: Blur with Gaussian

Page 45: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Code Sample: Canny EdgeNote: You will have to experiment with the parameters for GaussianBlur() and Canny() to get a good edge image.

Page 46: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Exit Sequences

• Open CV will “lock” your python when showing images. You will need “Exit Sequences” to cleanly exit running programs to preserve data and not crash your python apps.

• Two Kinds: Non-Looping and Looping• Non-Looping: For Still Images

• Looping: when using the Camera Object

Page 47: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Non-Looping Exit Sequence

Page 48: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Looping Exit Sequence

Page 49: Open CV API commandsnebomusic.net › perception › open_cv_python.pdf · 2015-09-14 · CV2 Canny Edge Function •Built in Edge detection function in Open CV that uses a more involved

Thank You!

• Many thanks to Charlie Fligg (Class of ’20) for researching Python and Computer Vision and helping gather software, materials, and tinkering with open CV2 for the Perception Class!