Matlab Image Processing

Download Report

Transcript Matlab Image Processing

Image and Video
Processing in MATLAB
Partly based on slides by Dmitri Roduy
Topics
• Data Types
• Image Representation
• Image/Video I/O
• Matrix access
• Image Manipulation
• Application: Bright Cars Detection in Video
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:
bwlabel(),bwmorph(),bwdist(),im2bw(),bwperim().
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(),rgb2ycbcr(),
ycbcr2rgb(), rgb2lab().
Image/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:
•
•
•
•
•
•
•
•
•
imrotate()– Rotate image.
imfilter() – Use kernal to convolve/correlation.
nlfilter() – Sliding neighborhood operation.
blkproc() – Perform function on (semi-)distinct blocks.
fspecial() – Create common image filter kernels.
imresize() – scale up/down image using defined interpolation.
padarray() – Pad image.
colfilt() – Column-stack filtering (faster)
Many more – see IP toolbox help
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)
Convert each RGB frame to gray scale.
Search for regional maximas 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
Morphological Open
Erosion of the image with the Structuring Element followed by
Dilation of the image with the Structuring Element.