Contents
Quick primer on some insightful characteristics of matlab for the course
matlab can be used as a scripting language a variable is a matrix (a scalar is a special case)
v=3 size(v)
v =
3
ans =
1 1
v=[1 2 3 4] size(v)
v =
1 2 3 4
ans =
1 4
v=[1 2; 3 4] size(v)
v =
1 2
3 4
ans =
2 2
you can concatenate matrices
v=[v v']
v =
1 2 1 3
3 4 2 4
you can reference single or multiple values:
v(1,2) % row and column indices are 1-based v(:,2) % a single column v(1,:) % a single row v(:,2:4) % some of the columns
ans =
2
ans =
2
4
ans =
1 2 1 3
ans =
2 1 3
4 2 4
common operations work on matrices (careful about multiplication and division)
v*v'
ans =
15 25
25 45
variables have datatypes (usually you can forget, but not when dealing with images). Most common types we will consider are double, (i.e. double-precision floating point), uint8 (i.e. unsigned integers with 8 bits, [0 255] range), logical (i.e. boolean).
whos v
Name Size Bytes Class Attributes v 2x4 64 double
v=uint8(v)
whos v
v =
1 2 1 3
3 4 2 4
Name Size Bytes Class Attributes
v 2x4 8 uint8
v=v>2
whos v
v =
0 0 0 1
1 1 0 1
Name Size Bytes Class Attributes
v 2x4 8 logical
Images are matrices
im=imread('rice.png'); whos im
Name Size Bytes Class Attributes im 256x256 65536 uint8
imshow(im);
imshow([im im]);
imshow([im; im]);
Matrices are images
imshow(rand(100));
imshow(magic(200),[])
... but datatype matters! Careful about data range: [0 1] for double and logical, [0 255] for uint8
imshow(double(im));
imshow(double(im)/256);
imshow(uint8(double(im)/256));
Range also determines brightness/contrast. It can be controlled by the second argument to imshow
imshow(im,[-100 156]); title('increased brighness same contrast');
imshow(im,[ 0 156]); title('increased brighness and contrast');
imshow(im,[ 100 256]); title('decreased brighness, increased contrast');
imshow(im,[ 100 356]); title('decreased brighness, same contrast');
Also consider that brightness and contrast can be changed by additions and multiplications on image data.
It's also useful to play with the imtool command and its "contrast" function, which also shows the histogram of the image.
the histogram can be computed also as
imhist(im);
Color is represented by multiple channels
You get a 3d matrix
im=imread('bluecube.jpg'); whos im imshow(im);
Name Size Bytes Class Attributes im 1417x1417x3 6023667 uint8 Warning: Image is too big to fit on screen; displaying at 50%
this is a 3D matrix, because it has 3 dimensions (not because the 3rd dimension is == 3!).
test=im(:,:,1:2);
whos test
Name Size Bytes Class Attributes test 1417x1417x2 4015778 uint8
We can instead see each single channel as a grayscale image
imr=im(:,:,1); whos imr % note: now it's 2D, so it will be displayed as grayscale
Name Size Bytes Class Attributes imr 1417x1417 2007889 uint8
imshow(imr), title('red channel')
Warning: Image is too big to fit on screen; displaying at 50%
img=im(:,:,2);
imshow(img), title('green channel')
Warning: Image is too big to fit on screen; displaying at 50%
imb=im(:,:,3);
imshow(imb), title('blue channel')
Warning: Image is too big to fit on screen; displaying at 50%
We can plot the result of logical operations
l=imb>(1.3*img);
whos l
Name Size Bytes Class Attributes l 1417x1417 2007889 logical
imshow(l); title('pixels with at least 30% more blue than green');
Warning: Image is too big to fit on screen; displaying at 50%