Top Banner
046746 * Matlab Image Processing
24

046746. * Data Types * Image Representation * Image/Video I/O * Matrix access * Image Manipulation * MEX - MATLAB Executable * Data Visualization * General.

Mar 31, 2015

Download

Documents

Gina Sharrock
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 046746. * Data Types * Image Representation * Image/Video I/O * Matrix access * Image Manipulation * MEX - MATLAB Executable * Data Visualization * General.

046746

*Matlab Image Processing

Page 2: 046746. * Data Types * Image Representation * Image/Video I/O * Matrix access * Image Manipulation * MEX - MATLAB Executable * Data Visualization * General.

*Topics

*Data Types

*Image Representation

*Image/Video I/O

*Matrix access

*Image Manipulation

*MEX - MATLAB Executable

*Data Visualization

*General Tips

*Tricks

Page 3: 046746. * Data Types * Image Representation * Image/Video I/O * Matrix access * Image Manipulation * MEX - MATLAB Executable * Data Visualization * General.

*Data Types

*Relevant data types*double – default in most cases

*single

*uint8 – [0 255]

*uint16 – [0 65,535]

*Logical – [0 1]

*Simple casting: double(), uint8().

*Useful when displaying images with a dynamic range not corresponding to its actual type.

*Conversion: im2double(),im2uint8(),lab2double(),lab2uint8()

Page 4: 046746. * Data Types * Image Representation * Image/Video I/O * Matrix access * Image Manipulation * MEX - MATLAB Executable * Data Visualization * General.

I I2

Max difference between images: 1

I = imread('world.jpg');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

Page 5: 046746. * Data Types * Image Representation * Image/Video I/O * Matrix access * Image Manipulation * MEX - MATLAB Executable * Data Visualization * General.

Max difference between images: 1.4

I = double(imread('world.jpg'));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');

*Common problem

I I2

Page 6: 046746. * Data Types * Image Representation * Image/Video I/O * Matrix access * Image Manipulation * MEX - MATLAB Executable * Data Visualization * General.

*Image Representation

2D Matrix

*Intensity: Each pixel value in the dynamic range [minP, maxP].

*Can represent a grayscale image, results of a 2d function, fuzzy ground truth, etc.

*Useful commands: imagesc(), axis, colormap().

*Binary: a.k.a masks.

*Can represent absolute ground truth, etc.

*Useful commands: bwlabel(),bwmorph(),bwdist(),im2bw(),bwperim().

Page 7: 046746. * Data Types * Image Representation * Image/Video I/O * Matrix access * Image Manipulation * MEX - MATLAB Executable * Data Visualization * General.

*Image Representation

2D Matrix

*Indexed: Each pixel value in the range [minP, maxP].

*Can represent segmentation.

*Useful commands: regionprops(),label2rgb()

Page 8: 046746. * Data Types * Image Representation * Image/Video I/O * Matrix access * Image Manipulation * MEX - MATLAB Executable * Data Visualization * General.

*Image Representation

3D Matrix

*True Color: Three 2D matrices stacked. Each represents a color component. (e.g. RGB)

*Can represent an RGB color image,LAB image, etc.

*Useful commands: imshow(),rgb2gray(),rgb2ind().

Page 9: 046746. * Data Types * Image Representation * Image/Video I/O * Matrix access * Image Manipulation * MEX - MATLAB Executable * Data Visualization * General.

*Image/Video I/O

Useful Commands*imread() – read image

*imwrite() – write image

*im2fram() – convert image to movie frame

*movie2avi() – write avi file

*aviread() – read avi file

*mmreader()/VideoReader() – read video (better)

*movie() – show movie

Page 10: 046746. * Data Types * Image Representation * Image/Video I/O * Matrix access * Image Manipulation * MEX - MATLAB Executable * Data Visualization * General.

*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.

Page 11: 046746. * Data Types * Image Representation * Image/Video I/O * Matrix access * Image Manipulation * MEX - MATLAB Executable * Data Visualization * General.

*Image Manipulation

Useful Commands:*imcrop()– Useful for interactive cropping.

*imrotate()– Rotate image.

*imfilter() – Use kernal to convolve/correlation.

*nlfilter() – Sliding neighborhood operation.

*blkproc() – Perform function on (semi-)destinct blocks.

*fspecial() – Create common image filter kernals.

*imresize() – Resize image using defined interpolation.

*kron() – Kronecker tensor product

*padarray() – Pad image.

*colfilt() – Colum-stack filtering (faster)

*imfreehand()- Select region with mouse

Page 12: 046746. * Data Types * Image Representation * Image/Video I/O * Matrix access * Image Manipulation * MEX - MATLAB Executable * Data Visualization * General.

*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.

Page 13: 046746. * Data Types * Image Representation * Image/Video I/O * Matrix access * Image Manipulation * MEX - MATLAB Executable * Data Visualization * General.

*Data Visualization

Useful Commands:

*scatter() – Useful to plot points on image.

*Imagesc() – Useful for 2D data.

*print() – Save figure as image on disk (careful with lossy compressions)

Page 14: 046746. * Data Types * Image Representation * Image/Video I/O * Matrix access * Image Manipulation * MEX - MATLAB Executable * Data Visualization * General.

*General Tips

*Avoid loops

*Manage memory (Clear unused variables)

*Useful command: clearvars()

*Avoid memory duplication – use nested functionsfunction myfunA = 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

Page 15: 046746. * Data Types * Image Representation * Image/Video I/O * Matrix access * Image Manipulation * MEX - MATLAB Executable * Data Visualization * General.

*General Tips

Long-Term Usage (Windows Systems Only)

On 32-bit Microsoft Windows, the workspace of MATLAB can fragment over time due to the fact that the Windows memory manager does not return blocks of certain types and sizes to the operating system. Clearing the MATLAB workspace does not fix this problem. You can minimize the problem by allocating the largest variables first. This cannot address, however, the eventual fragmentation of the workspace that occurs from continual use of MATLAB over many days and weeks, for example. The only solution to this is to save your work and restart MATLAB.The pack command, which saves all variables to disk and loads them back, does not help with this situation.

Page 16: 046746. * Data Types * Image Representation * Image/Video I/O * Matrix access * Image Manipulation * MEX - MATLAB Executable * Data Visualization * General.

*Tricks

Original Result

Stenography (Wikipedia) The art of hiding a message within another larger message

?

Page 17: 046746. * Data Types * Image Representation * Image/Video I/O * Matrix access * Image Manipulation * MEX - MATLAB Executable * Data Visualization * General.

*Tricks

Stenography (Wikipedia) The art of hiding a message within another larger message

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

Page 18: 046746. * Data Types * Image Representation * Image/Video I/O * Matrix access * Image Manipulation * MEX - MATLAB Executable * Data Visualization * General.

*Tricks

Almost Connected (Steve Eddins’ Blog)

Page 19: 046746. * Data Types * Image Representation * Image/Video I/O * Matrix access * Image Manipulation * MEX - MATLAB Executable * Data Visualization * General.

*Tricks

Almost Connected (Steve Eddins’ Blog)

url = 'http://blogs.mathworks.com/images/steve/2010/blobs_in_clumps.png';bw = imread(url);lbl = bwlabel(bw);figure; imagesc(lbl); axis image;

50 100 150 200 250 300

50

100

150

200

250

300

Page 20: 046746. * Data Types * Image Representation * Image/Video I/O * Matrix access * Image Manipulation * MEX - MATLAB Executable * Data Visualization * General.

*Tricks

Almost Connected (Steve Eddins’ Blog)

bw2 = bwdist(bw) <= 12.5;lbl2 = bwlabel(bw2);figure; imshow(bw2);figure; imagesc(lbl2); axis image;

50 100 150 200 250 300

50

100

150

200

250

300

Page 21: 046746. * Data Types * Image Representation * Image/Video I/O * Matrix access * Image Manipulation * MEX - MATLAB Executable * Data Visualization * General.

*Tricks

Almost Connected (Steve Eddins’ Blog)

lbl3 = lbl2.*bw;figure;imagesc(lbl3); axis image;

50 100 150 200 250 300

50

100

150

200

250

300

Page 22: 046746. * Data Types * Image Representation * Image/Video I/O * Matrix access * Image Manipulation * MEX - MATLAB Executable * Data Visualization * General.

*Tricks

Feature AND (Steve Eddins’ Blog)

bw = imread('text.png');dots = rand(size(bw))>0.99;

Page 23: 046746. * Data Types * Image Representation * Image/Video I/O * Matrix access * Image Manipulation * MEX - MATLAB Executable * Data Visualization * General.

*Tricks

Feature AND (Steve Eddins’ Blog)

touching_pixels = bw & dots;

Overlapping

Page 24: 046746. * Data Types * Image Representation * Image/Video I/O * Matrix access * Image Manipulation * MEX - MATLAB Executable * Data Visualization * General.

*Tricks

Feature AND (Steve Eddins’ Blog)

out = imreconstruct(touching_pixels, bw);

Reconstructed