Title: Introduction to MatLab: Image Processing
1Introduction to MatLab Image Processing
Into to Computer Science
- - MatLab stands for Matrix Laboratory.
- - Most of the programming operations have as
input or output a matrix or a vector. - - Images are often represented a matrices.
- MatLab is a powerful tool to manipulate graphics
and images. - http//cs.uc.edu/annexste/Camp08
- fred.annexstein_at_uc.edu
Prepared by Fred Annexstein University of
Cincinnati Some Rights Reserved
2Overview
Like Scratch, MatLab is an interpreted
programming language. Unlike Scratch, Matlab is a
language in which you type commands at the
prompt. Try these MATRIX AND VECTOR
OPERATIONS This is how we can define a vector gtgt
v1, 2, 3 Matlab prints out the following v
1 2 3 Similarly we can define a
matrix gtgt M 1 2 3 4 5 6 7 8 9 The result
is M 1 2 3 4 5
6 7 8 9 If you want to suppress the
MatLab output then you need to finish the line
with semicolon as follows. gtgtM 1 2 3 4 5 6 7
8 9
3Projection
- Say you want to extract some rows and columns of
a matrix. This is called a projection. We simply
give the subset of rows and columns as
parameters, as follows - gtgt M11M(23 , 23)
- M11
- 5 6
- 8 9
- To specify all elements in a given dimension one
can use ' So to get all rows but just columns 1
and 2, we type - gtgt A M( , 12)
- A
- 1 2
- 4 5
- 7 8
4WORKING WITH IMAGES in MatLab
Lets talk about image files and their
formats.. Color vs GrayScale Basic Image
Processing functions Reading in an image gtgt
img1imread('Water lilies.jpg') Displaying an
image gtgt imshow(img1) Finding out size of an
image gtgt size(img1) gtgt size(img1) ans
600 800 3
5WORKING WITH IMAGES in MatLab
- Cropping an image
- gtgt imgsmallimg1(200300,300400,13)
- gtgt imshow(imgsmall)
- gtgt imgsmallimg1(150250,350450,13)
- gtgt imshow(imgsmall)
- gtgt size(imgsmall)
- ans
- 101 101 3
- Exercise 1. Find 2 images online
- 2. Crop them to the same size
- 3. Add the two images together.
- 4. Display resulting image
- Advanced Exercise
6ReScaling
- We can rescale by changing the number of rows and
columns, yet preserve the information in the
image - gtgt rows, cols, colors size(img1)
- rows 600 cols 800 colors 3
- Increase the number of rows
- gtgt stretchfactor 1.5
- gtgt rowVec linspace(1,rows,stretchfactorrows)
- gtgt newrowsround(rowVec)
- gtgt newimagimg1(newrows,,)
- gtgt imshow(newimg)
- Decrease number of columns
- gtgt stretchfactor 0.75
- gtgt colVec linspace(1,cols,stretchfactorcols)
- gtgt newcolsround(colVec)
- gtgt newimagnewimg(,newcols,)
- gtgtimshow(newimg)
7Example Program Inverting an image
- To invert or to add two images we need to convert
to double and then rescale the result back so
that it looks like an image - InvImg 1 - double(IMG1)/255
- NewImg uint8(round(InvImg255)))
- Imshow(NewImg)
8WORKING WITH IMAGES in MatLab
- Color Masking
- Sometimes we want to replace pixels of an image
of one or more colors with pixels from another
image. It is useful to use a blue or green
screen in some instances. - Find an image with a big plot of one color. First
we will replace that color. And then we will find
another image for pixel replacement. - Let us plot the color values of one chosen
rowThis will tell us the pixel values of the
color we want to replace. - v imread(myimg.jpg)
- image(v)
- row input(which row?)
- red v(row,,1)
- green v(row,,2)
- blue v(row,,3)
- plot(red,r)
- hold on
- plot(green,g)
- plot(blue,b)
9WORKING WITH IMAGES in MatLab
- Suppose we want to replace those values whose
intensities exceed a threshold value of 160 in
each color. - v imread(myimg.jpg)
- thresh 160
- layer (v(,,3) gt thresh)
- mask(,,1) layer
- mask(,,2) layer
- mask(,,3) layer
- To reset the color to red
- gtgtnewv v
- gtgtnewv(mask) 255
- Or to replace pixels from a different image w
usegtgt newv(mask) w(mask) - Let us try to do this with a blue screen..