Introduction: Python Basics

Variables

Variables in python are very flexible, unlike C++:

  • Interpreted language.
  • No need to declare the variable type.
  • The same variable can be assigned to different types.
x = 5
y = 'Hello SBME'
z = 5
z = "Hello SBME"

Lists

# List construction
subjects = ['Technical Writing','Computer Vision']

# Append an element
subjects.append('Biochemistry')

# Append another list
subjects.extend(['Bioelectronics','Stochastic'])

print( subjects ) # ??
print( subjects[0] ) # ??
print( subjects[1] ) # ??

Arithmetic Operations


x = 19
y = 18 

z = x / y
z = x * y
z = x + y
z = x - y

Logical Opertations

x = 17 % 2 == 1

y = 9 / 3 < 1 

b = x or y ## True

b = x and y ## False
  • and => && in C++

  • or => || in C++

If, elif, else


x = 23 
y = 22 

if x < y:
  z = 13 # Local scope

elif x % 2 == 1 and x > y :
  h = 17 # Local scope

else:
  v = 80 # Local scope

print( z ) # Error: z is out of scope
print( h ) # Error: h is out of scope

Loops


for i in range(10):
  print( i )

i = 0
while i < 10 :
  print( i )
  i += 1

Functions

def mean( list ):
  sum = 0
  for element in list:
    sum += element
  return sum / len( sum )

m = mean([1,12,42,1,23,12])
print( m )

Importing Libraries

import numpy as np

Numpy

Reference: {NumPy Reference}

import numpy as np

a = np.array([12,23,44,21,23])

print( np.mean( a ))
print( np.std( a ))

Matplotlib

  • Matplotlib is Matlab-like visualization library.
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

image1 = mpimg.imread("images.jpg")
image2 = mpimg.imread("cameraman1.png")

print( image1.shape )
print( image2.shape )

plt.imshow(image2)
plt.show()

Getting Started with Python: Installing Anaconda

Anaconda

  • Shipped +1000 Data Science Packages (DSP, Image Processing, Machine Learning, AI, Statistics).
  • Shipped with Python IDE (Spyder).
  • Shipped with Jupyter Notebook.

Download Anaconda 3 (Python 3)

References: Image Processing with Python

Demo

You can find the demo files on this repository at github cv-week1-demo

Prerequisites

  1. Anaconda3 installed.
  2. git installed

Downloading the Demo

Issue the following command:

$ git clone https://github.com/sbme-tutorials/cv-week1-demo.git
$ cd cv-week1-demo

Expect soon migration to Python notebooks for more convenience.