advanced matlab
DESCRIPTION
Advanced MATLAB. 046746. Topics. Data Types Image Representation Image/Video I/O Matrix access Image Manipulation MEX - MATLAB Executable Data Visualization General Tips Tricks. Data Types. Relevant data types double – default in most cases (usually 64 bit) - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Advanced MATLAB](https://reader038.vdocuments.net/reader038/viewer/2022103023/56812bdf550346895d904e66/html5/thumbnails/1.jpg)
Advanced MATLAB046746
![Page 2: Advanced MATLAB](https://reader038.vdocuments.net/reader038/viewer/2022103023/56812bdf550346895d904e66/html5/thumbnails/2.jpg)
Topics
• Data Types• Image Representation• Image/Video I/O• Matrix access• Image Manipulation• MEX - MATLAB Executable• Data Visualization• General Tips• Tricks
![Page 3: Advanced MATLAB](https://reader038.vdocuments.net/reader038/viewer/2022103023/56812bdf550346895d904e66/html5/thumbnails/3.jpg)
Data Types
• Relevant data types• double – default in most cases (usually 64 bit)• single – when you want to save memory (usually 32 bit)• uint8 – [0 255] – native for images• uint16 – [0 65,535]• Logical – [0 1] – native for masks
• Simple casting: double(), uint8().• Useful when displaying images with a dynamic range
not corresponding to its actual type.• Conversion (of images): im2double(),im2uint8(),lab2double(),lab2uint8()
• rgb2ind(),ind2rgb(),makecform()
![Page 4: Advanced MATLAB](https://reader038.vdocuments.net/reader038/viewer/2022103023/56812bdf550346895d904e66/html5/thumbnails/4.jpg)
I = imread('world.jpg');I2 = I-1.4;diffI = I-I2;fprintf('Max difference between images: %d\n',max(diffI(:)));
Common problem
fprintf('Max difference between images: %2.1f\n',max(diffI(:)));
Max difference between images: 1.4
Max difference between images: 1
display([ 'Max difference between images: ' num2str( max(diffI(:)) ) ]);
A much better practice is:
Max difference between images: 1.4
![Page 5: Advanced MATLAB](https://reader038.vdocuments.net/reader038/viewer/2022103023/56812bdf550346895d904e66/html5/thumbnails/5.jpg)
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: imagesc(), axis, colormap().
• Binary: a.k.a masks.• Can represent absolute ground truth, etc.• Useful commands: bwlabel(),bwmorph(),bwdist(),im2bw(),bwperim().
![Page 6: Advanced MATLAB](https://reader038.vdocuments.net/reader038/viewer/2022103023/56812bdf550346895d904e66/html5/thumbnails/6.jpg)
Image Representation
2D Matrix
• Indexed: Each pixel value in the range [minP, maxP].• Can represent segmentation.• Useful commands: regionprops(),label2rgb()
![Page 7: Advanced MATLAB](https://reader038.vdocuments.net/reader038/viewer/2022103023/56812bdf550346895d904e66/html5/thumbnails/7.jpg)
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 8: Advanced MATLAB](https://reader038.vdocuments.net/reader038/viewer/2022103023/56812bdf550346895d904e66/html5/thumbnails/8.jpg)
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
![Page 9: Advanced MATLAB](https://reader038.vdocuments.net/reader038/viewer/2022103023/56812bdf550346895d904e66/html5/thumbnails/9.jpg)
Matrix access
Useful Commands:• Ind = sub2ind(matrixSize, rowSub, colSub) convert subscript to
index.• [I,J] = ind2sub(siz,IND) – convert index to subscipt.• meshgrid() – generate X,Y grids.• F = scatteredInterpolant(x,y,v) - creates an interpolant that fits
a surface of the form v = F(x,y). (replaced TriScatteredInterp)
![Page 10: Advanced MATLAB](https://reader038.vdocuments.net/reader038/viewer/2022103023/56812bdf550346895d904e66/html5/thumbnails/10.jpg)
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-)distinct blocks.• fspecial() – Create common image filter kernels.• imresize() – Resize image using defined interpolation.• kron() – Kronecker tensor product• padarray() – Pad image.• colfilt() – Colum-stack filtering (faster)• imfreehand()- Select region with mouse
![Page 11: Advanced MATLAB](https://reader038.vdocuments.net/reader038/viewer/2022103023/56812bdf550346895d904e66/html5/thumbnails/11.jpg)
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 12: Advanced MATLAB](https://reader038.vdocuments.net/reader038/viewer/2022103023/56812bdf550346895d904e66/html5/thumbnails/12.jpg)
Data VisualizationUseful Commands:
• scatter()/plot() – Useful to plot points on image.• imagesc() – Useful for 2D data.• print() – Save figure as image on disk (careful with lossy
compressions)
![Page 13: Advanced MATLAB](https://reader038.vdocuments.net/reader038/viewer/2022103023/56812bdf550346895d904e66/html5/thumbnails/13.jpg)
General Tips• Avoid loops• Manage memory (Clear unused variables)• Useful command: clearvars()
• Avoid memory duplication – use nested functions
function 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 14: Advanced MATLAB](https://reader038.vdocuments.net/reader038/viewer/2022103023/56812bdf550346895d904e66/html5/thumbnails/14.jpg)
General Tips• Avoid memory duplication – don’t want to use nested
functions? Simply use the same variable name:
function x = demox=rand(10000);x=func(x); function a=func(a)a=a*2;
![Page 15: Advanced MATLAB](https://reader038.vdocuments.net/reader038/viewer/2022103023/56812bdf550346895d904e66/html5/thumbnails/15.jpg)
General Tips• Preallocating Arrays
• Preallocate correctly
ticx = 0;for k = 2:1000000 x(k) = x(k-1) + 5;endtocElapsed time is 0.301528 seconds.
ticx = zeros(1, 1000000);for k = 2:1000000 x(k) = x(k-1) + 5;endtocElapsed time is 0.011938 seconds.
A = int8(zeros(100));A = zeros(100, 'int8');
![Page 16: Advanced MATLAB](https://reader038.vdocuments.net/reader038/viewer/2022103023/56812bdf550346895d904e66/html5/thumbnails/16.jpg)
General Tips• You will be amazed of the variety of built in functions that
Matlab offers.• In fact, assume that the function you need is built in Matlab.
It’s probably true!• Use Matlab “help” to understand how to use functions• Use Google to search for new functions!
![Page 17: Advanced MATLAB](https://reader038.vdocuments.net/reader038/viewer/2022103023/56812bdf550346895d904e66/html5/thumbnails/17.jpg)
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 18: Advanced MATLAB](https://reader038.vdocuments.net/reader038/viewer/2022103023/56812bdf550346895d904e66/html5/thumbnails/18.jpg)
Tricks
Original Result
Stenography (Wikipedia) The art of hiding a message within another larger message
?
![Page 19: Advanced MATLAB](https://reader038.vdocuments.net/reader038/viewer/2022103023/56812bdf550346895d904e66/html5/thumbnails/19.jpg)
Tricks
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');
Stenography (Wikipedia) The art of hiding a message within another larger message
![Page 20: Advanced MATLAB](https://reader038.vdocuments.net/reader038/viewer/2022103023/56812bdf550346895d904e66/html5/thumbnails/20.jpg)
TricksAlmost Connected (Steve Eddins’ Blog)
![Page 21: Advanced MATLAB](https://reader038.vdocuments.net/reader038/viewer/2022103023/56812bdf550346895d904e66/html5/thumbnails/21.jpg)
Tricks
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
Almost Connected (Steve Eddins’ Blog)
![Page 22: Advanced MATLAB](https://reader038.vdocuments.net/reader038/viewer/2022103023/56812bdf550346895d904e66/html5/thumbnails/22.jpg)
Tricks
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
Almost Connected (Steve Eddins’ Blog)
![Page 23: Advanced MATLAB](https://reader038.vdocuments.net/reader038/viewer/2022103023/56812bdf550346895d904e66/html5/thumbnails/23.jpg)
Tricks
lbl3 = lbl2.*bw;figure;imagesc(lbl3); axis image;
50 100 150 200 250 300
50
100
150
200
250
300
Almost Connected (Steve Eddins’ Blog)
![Page 24: Advanced MATLAB](https://reader038.vdocuments.net/reader038/viewer/2022103023/56812bdf550346895d904e66/html5/thumbnails/24.jpg)
Tricks
bw = imread('text.png');dots = rand(size(bw))>0.99;
Feature AND (Steve Eddins’ Blog)
![Page 25: Advanced MATLAB](https://reader038.vdocuments.net/reader038/viewer/2022103023/56812bdf550346895d904e66/html5/thumbnails/25.jpg)
Tricks
touching_pixels = bw & dots;Overlapping
Feature AND (Steve Eddins’ Blog)
![Page 26: Advanced MATLAB](https://reader038.vdocuments.net/reader038/viewer/2022103023/56812bdf550346895d904e66/html5/thumbnails/26.jpg)
Tricksout = imreconstruct(touching_pixels, bw);
Reconstructed
Feature AND (Steve Eddins’ Blog)
out = imreconstruct(dots, bw);
Of course this will work just as well: