Title: Image Processing in MatLab
1Image Processing in MatLab
CS100 Into to Computer Science
MatLab is a powerful tool to manipulate graphics
and images
Prepared by Fred Annexstein University of
Cincinnati Some Rights Reserved
2WORKING WITH IMAGES in MatLab
Basic Image functions Reading img1imread('imag
e1.jpg') Writing imwrite(img1,
'image2.jpg') Displaying imshow(img1) Recall
that Last time we did an Exercise Find 2 images
online and add them together. You only needed to
crop them to the same size.
3WORKING WITH IMAGES in MatLab
RESIZING gtgt imgimread(my.jpg') gtgt rows,
cols, colors size(img) Increase the number
of rows gtgt stretchfactor 1.5 gtgt rowVec
linspace(1,rows,stretchfactorrows) gtgt
newrowsround(rowVec) Decrease number of
columns gtgt stretchfactor 0.75 gtgt colVec
linspace(1,cols,stretchfactorcols) gtgt
newcolsround(colVec) gtgt
newimagimg(newrows,,) gtgt imshow(newimg) gtgt
newimagnewimg(,newcols,) gtgtimshow(newimg)
4WORKING 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 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)
5WORKING 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(,,1) gt thresh) (v(,,2) gt
thresh) (v(,,2) gt thresh) - mask(,,1) layer
- mask(,,2) layer
- mask(,,3) layer
- If you want to only mask a portion of the image
you can use something like - gtgt mask(700end,,) false
- Which sets the mask so that we do not affect rows
700 and above - To reset the color to red
- gtgtnewv v
- gtgtnewv(mask)(1) 255
- Or to replace pixels from a different image w
usegtgt newv(mask) w(mask)