School of Computing National University of Singapore CS4243 Computer Vision and Pattern Recognition Semester 1, AY 2010/11 Lab 1: Basic Image Processing in Python

taxidermistplateΛογισμικό & κατασκευή λογ/κού

7 Νοε 2013 (πριν από 4 χρόνια και 1 μέρα)

88 εμφανίσεις

School of Computing

National University of Singapore

CS4243 Computer Vision and Pattern Recognition

Semester 1, AY 2010/11


Lab 1: Basic Image Processing in Python


Objectives:



Learn

to write simple Python programs.



Learn to
use

Python Imaging Library, Op
enCV 2.0 Python, PyOpenCV and NumPy for
basic image processing. In developing computer vision applications, you can choose to
use any one or more of these libraries that are most convenient.


Preparation:



Read the following materials to familiarize yoursel
f
BEFORE

you go for Lab 1:



Python Documentation: The Python Tutorial’s introductory paragraph and
Section 1
Whetting Your Appetite.



NumPy User Gui
de:
Section 1.1 What is NumPy?



Create a folder
in the PC with your name, e.g.,
e:/
myname
. This folder will
be
used a
s
your working directory.



Download the
file

Lab1.zip

into your working directory and unzip it. You should see
the following files:
lab1.doc
,
ponzo.jpg
,
bird.jpg
,
lab1.py
.



Part 1.

Starting Python


This part illustrates how to start using Python for
simple image processing with Python Imaging
Library (PIL).


1.

Start Python interpreter.

Windows: Select Start


All Programs


Python


IDLE (Python GUI).

Linux:


2.

Explore and change

working directory.

a.

Import
os

module for interface to operating system.

>>>
import
os

b.

Get current working directory.

This will display the default current working directory.

>>> os.getcwd()

c.

Change current working directory to your working directory, e.g.,
e:/
myname
.

This allows you to access the images in your working directory wi
thout having to specify
the full path names.

>>> os.
chdir
(
“e:/
myname

)

d.

Examine the current working directory.

It should
display

the one that you have set.

>>> os.getcwd()

e.

List the files in the current working directory.

>>> os.
listdir
(
“.”
)


3.

Read and displa
y

image with PIL.

a.

Read image.

>>>
import Image

>>> im
age

= Image.open(“ponzo.jpg”)

b.

Display image information.

>>>
print im
age
.format, im
age
.size, im
age
.mode

c.

Display image. This is a simple function that invokes the default image display tool in
Windows or
Linux to display the image.

It is mainly used for quick display
ing

of images
for debugging.

>>> im
age
.show()


4.

Apply simple image processing function.

>>>
import ImageEnhance

>>> enhancer = ImageEnhance.Contrast(image)

>>> enhanced_image = enhancer.enhance(
2.0
)

>>> enhanced_image.show()


5.

Save image to a file.

>>>
enhanced_image.save(

out
.jpg”
)


6.

Write

codes in a program file.



Although the interpretative mode of Python is very convenient for doing quick checks, it
is often more convenient to write the codes in

a program file. The file
lab1
.py

illustrates how to put all the above codes into a function.

The file is c
alled a module, and
the module

name is
the file name
lab1
.



To run the program, do the following

>>>
import sys

>>> sys.path.append(“.”)

>>> import
la
b1

>>>
lab1
.test1(“ponzo
.jpg

, “
out
.jpg”
)

The second statement tells Python to search for the module
lab1

in the current directory.
The last statement calls the function
test1

in
lab1

by passing the image file name,
which is also stored in the current dire
ctory.



If you
edit and change

your program, you need to reload the module for the change to be
effective.

In Windows, you can use the editor that comes with IDLE, the Python GUI
, to
open, edit and save the file.

>>> reload(
lab1
)



Part 2.

Using
Python

Libr
aries


This part illustrates how to use
Python

libraries for reading, writing and
processing images
.


1.

Reading and
w
riting

image
with OpenCV 2.0 Python. First, append the appropriate path
name of OpenCV 2.0 Python to the system’s search path. The path
name
depends on where
OpenCV is installed, e.g.,
C:
\
OpenCV2.0
\
Python2.6
\
Lib
\
site
-
packages
\
cv.pyd
.

>>>
sys.path.append(“
C:
\
OpenCV2.0
\
Py
thon2.6
\
Lib
\
site
-
packages
”)

>>> import cv

>>>
im

= cv.LoadImage(“ponzo.jpg”)

>>> cv.SaveImage(“cv.
png
”,
im
)

OpenCV reads the im
age and stores it
in an

image data structure.

Note: S
aving as JPEG image seems to have problem of causing Python to restart.


2.

Reading and writing image with PyOpenCV,
a

Python
wrapper for OpenCV.

>>> import pyopencv as pcv

>>>
mat

=
p
cv.imread(“ponzo.jpg”)

>>>
p
cv.imwrite(“
pcv
.png”,
mat
)

The first statement imports PyOpenCV and gives it a shorter name
pcv
.
PyOpenCV reads
the image and stores it in a matrix data structure.

It

can also

save the image as a JPEG image
by specifying
.jpg

as the file suffix.


3.

Con
vert PyOpenCV matrix into NumPy matrix for using NumPy matrix function
s
.

>>>
nmat

=
mat
.ndarray

>>> nmat *= 0.7

>>>
mat2

=
p
cv.asMat(
nmat
)

>>> pcv.imwrite(“pcv2.
jpg
”,
mat2
)

The
second statement darkens the image by multiplying each element of
nmat

(i.e., e
ach
pixel’s colour value) by 0.7. Such
a matrix

operation is possibly with NumPy matrix.
This
feature makes the code simpler and easier to read.

T
he
third statement writes out
mat2

into a
JPEG file.

4.

Convert between PIL image and OpenCV matrix.

>>>
im

= Im
age.open(“ponzo.jpg”)

# read PIL image

>>>
mat

=
p
cv.Mat.from_pil_image(
im
)

>>>
im2

= mat.to_pil_image()

>>>
im2
.save(“pil.jpg”)

# write PIL image

The
sentence that starts with the
#

symbol is a comment. You don’t need to type it.



Part 3.

Simple Prog
ramming Assignment

(to be submitted for grading)


Write a simple Python program
for image blending as

follow
:

1.

Read

the

image
s

ponzo.jpg

and
bird.jpg

using PIL, OpenCV Python or PyOpenCV.

2.

Convert the images into NumPy matrices, and call them
ponzo

and
bird
.

3.

Perform blending of
ponzo

and
bird

by scalar multiplication and addition of matrices:

blend =
0.4 * ponzo + 0.6 * bird

4.

Save the blended image into an output file.


To edit your program, you can use

the editor that comes with IDLE
, the Python GUI.
Alternat
ively, you can use

NotePad++, Vim or NotePad in Windows, and Vim or GNU Emacs in
Linux.


Submit the following
at the end of the lab session
:

1.

Print
-
out of your
Python program.

2.

Print
-
out of the blended image.

Remember to write your name and matriculation num
ber.