Sie sind auf Seite 1von 11

"//"

Image and Video


Processing in MATLAB
Partly based on slides by Dmitri Rudoy

Topics
Image Data Types
Image Representation
Image and Video I/O
Matrix Access
Image Manipulation
MEX (in brief)
Data Visualization
Tips
Examples

"//"

Image Data Types


Relevant data types

uint8 [0 255] native for natural images


uint16 [0 65,535] common for medical images
logical [0 1] native for masks, morph. elements
double 64bit floating point (default range [0 1])
single when you want to save memory (32bit floating point)

Simple casting: double(), uint8().


Type Conversion (of images):
im2double(),im2single(),im2uint8(), im2unit16()

Works for colors, gray-scale, logical images

Common Problem
I = imread(pears.png');
I2 = I-1.4;
diffI = I-I2;
fprintf('Max difference between images: %d\n',max(diffI(:)));
figure(1);
subplot(1,2,1);
imshow(I); title('I');
subplot(1,2,2);
imshow(I2); title('I2');

"//"

Common Problem
I = double(imread('pears.png'));
I2 = I-1.4;
diffI = I-I2;
fprintf('Max difference between images:
%2.1f\n',max(diffI(:)));
figure(1);
subplot(1,2,1);
imshow(I); title('I');
subplot(1,2,2);
imshow(I2); title('I2');

Possible Solution
I = double(imread('pears.png'));
I2 = I-1.4;
diffI = I-I2;
fprintf('Max difference between images:
%2.1f\n',max(diffI(:)));
figure(1);
subplot(1,2,1);
max_I = 255;
imshow(I/max_I); title('I');
subplot(1,2,2);
imshow(I2/max_I); title('I2');

"//"

Image Representation
2D Matrix
Intensity: Each pixel value in the dynamic range [minP, maxP].
Can represent a grayscale image, results of a 2d function etc.
Useful commands: imshow (), imagesc(), colormap().

Binary: a.k.a masks.


Can represent areas of interest in image, morphological
structuring elements and more
Useful commands:
bwconncomp(),labelmatrix(), bwmorph(),
bwdist(), im2bw(), bwperim().

Image Representation
2D Matrix
Indexed: Each pixel value in the range [1, M].
Points to a colormap containing true colors.
Can represent segmentation.
Useful commands: regionprops(), label2rgb()

"//"

Image Representation
3D Matrix
True Color: Three 2D matrices stacked. Each represents a color
component. (e.g. RGB)
Can represent an RGB color image, Ycbcr image, LAB image, etc.
Useful commands: imshow(), rgb2gray(), rgb2ind().

Image and Video I/O


Useful Commands

imread() read image


imwrite() write image
im2frame() convert image to movie frame
movie2avi() write avi file
aviread() read avi file
mmreader()/VideoReader() read video (better)
VideoWriter() create video file (2011b+)
movie() show movie
implay() show video interactively

"//"

Matrix Access
Useful Commands:
sub2ind() convert subscript (e.g. (r,c,clr)) to index (n).
ind2sub() convert index (n) to subscipt (e.g. (r,c,clr)).
meshgrid() generate X,Y grids.

Image Manipulation
Useful Functions:

imcrop() Useful for interactive cropping.


imrotate() Rotate image.
imfilter() Use kernal to convolve/correlation.
nlfilter() Sliding neighborhood operation.
blockproc() Perform function on distinct blocks.
fspecial() Create common image filter kernels.
imresize() scale up/down image using defined interpolation.
padarray() Pad image.
colfilt() Column-stack filtering (faster)
imfreehand() Select region with mouse
Many more see IP, CV toolbox help

"//"

MEX - MATLAB Executable


Dynamically linked subroutines produced from C, C++ or
Fortran source code.
Useful when dealing with non efficient-Matlab algorithms (e.g.
iterative algorithm implemented as loops).
mex setup : Setup mex compiling configurations.

Data Visualization
Useful Commands:
scatter()/plot() Useful to plot points on image.
bar() Useful for plotting histograms.
print() Save figure as image on disk (careful with lossy
compressions)

"//"

General Tips
Avoid loops
Manage memory (Clear unused variables)
Useful command: clearvars()

Avoid memory duplication use nested functions


Copy-on-Write
function myfun
A = magic(500);
function setrowval(row, value)
A(row,:) = value;
end
setrowval(400, 0);
disp('The new value of A(399:401,1:10) is')
A(399:401,1:10)
end

Example 1
Steganography (Wikipedia)
The art of hiding a message within another larger message
Original

Result

"//"

Example 1
Steganography (Wikipedia)
The art of hiding a message within another larger message

I= imread('SteganographyOriginal.png');
I4=85*mod(I,4);
figure;
subplot(1,2,1)
imshow(I); title('Original');
subplot(1,2,2)
imshow(I4);title('Result');

Example 2
Almost Connected (Steve Eddins Blog)

"//"

Example 2
Almost Connected (Steve Eddins Blog)
bw = imread(url);
CC = bwconncomp(bw);
lbl = labelmatrix(CC);
figure; imagesc(lbl); axis image;

Example 2
Almost Connected (Steve Eddins Blog)
bw2 = bwdist(bw) <= 12.5;
CC2 = bwconncomp(bw2);
lbl2 = labelmatrix(CC2);
figure; imshow(bw2);
figure; imagesc(lbl2); axis image;

10

"//"

Example 2
Almost Connected (Steve Eddins Blog)
lbl3 = lbl2.*uint8(bw);
figure;
imagesc(lbl3); axis image;

Application: Bright Cars


Detection in Video
Objective: Tag with a red square bright colored cars in a color
video with moving cars.
Stages:
1)
2)
3)
4)
5)
6)

11

Convert each RGB frame to gray scale.


Search for regional maxima above a brightness threshold
Remove small regions (i.e. morphology)
Compute centroid of each region
Place a red square in centroid location
Repeat 1-5 for all video frames