Professional Documents
Culture Documents
Fundamentals: Prepared by Mohammad Alsmirat. Adapted From Gonzalez 2009
Fundamentals: Prepared by Mohammad Alsmirat. Adapted From Gonzalez 2009
Fundamentals
• imwrite (f,’filename’)
– Filename should contain an image file extension.
– OR imwrite (f,’filename’, ‘extinction’)
• We will deal her with tif (uncompressed) and jpeg
(compressed) images.
• For jpeg:
– imwrite(f,’filename.jpg’, ‘quality’, q)
– q is the quality factor and it is a number between 0 and
100. (example) (default value of q is 75)
• imfinfo filename
• Compression ration = image size before
compression / image size after compression.
• Save output of imfinfo to a structure variable
to be used later.
>> k = imfinfo ('rose50.jpg');
>> sizeBeforeComp = k.Width * k.Height * k.BitDepth /8;
>> compressionRatio = sizeBeforeComp/k.FileSize;
• Gray-Scale Images
• Binary Images
• Indexed Images
• RGB Images
• g = im2double(im2bw(mat2gray(f),0.6))
• g = double(f > 2);
• single VS double
– single saves memory.
• Create a matrix
– A = [1 2 3 ; 4 5 6 ; 7 8 9]
A=
1 2 3
4 5 6
7 8 9
• To index this matrix
– A(row, column)
– A(2, 3) gives 6
– A(end,end)
• Access a submatrix in A
– S = A([1 2], [1 2 3])
– S = A(1:2, 1:3)
– E = A([1 3], [3 2])
E=
3 2
9 8
• Access a raw
– A(1,:)
• Access a column
– A(:,3)
• Matrix Assignment
– B=A
– B(:,3) = 0
• A(:) convert the matrix A to a victor in
column major order.
Prepared by Mohammad Alsmirat. Adapted from Gonzalez 2009
Indexing Matrices
• A = [1 2 3 ; 4 5 6 ; 7 8 9]
• A([2 7])
ans =
4 3
• Assume we need to access A(1,2), A(2,2), A(1,3) in one
operation
• If we have r = [1 2 1] and c = [2 2 3]
– A(r,c) will not give us what we want
– Instead
• linear_indices = sub2ind(size(A), r,c)
• [r, c] = ind2sub(size(A), linear_indices)
• f = imread(‘rose_1024.tif’);
• fp = f(end:-1:1,:)
– This will flip the image vertically.
• fc = f(257:768,257:768)
– This will crop the image to a small square.
• fs = f(1:2:end,1:2:end)
– Subsample the image and take only 1 of each 2 rows and
1 of each 2 columns.
• plot (f(512,:))
• M-Files can be
– scripts that simply execute a series of MATLAB
statements.
– functions that can accept arguments and can
produce one or more outputs.
• M-files are created using a text editor and
are stored with a name of the form
filename.m
w1 = 0.5 * f;
w2 = 0.5 * g;
w = w1 + w2;
wmax = max(w(:));
wmin = min(w(:));
>> f = [1 2; 3 4];
>> g = [1 2; 2 1];
>> [w wmax wmin] = imblend(f,g)
w=
1.0000 2.0000
2.5000 2.5000
wmax =
2.5000
wmin =
1
>> f = [1 2; 3 4];
>> g = [1 2; 2 1];
>> f == g
ans =
1 1
0 0
>> f > g
ans =
0 0
1 1
>> 5 > f
ans =
1 1
1 1
>> f = [1 2; 3 4];
>> g = [1 2; 0 1];
>> f & g
ans =
1 1
0 1
>> f && g
??? Operands to the || and && operators must be convertible to
logical scalar values.
>> 8 && 5
ans =
1
>> [5] && [6]
ans =
1
>> A = [0 1 2; 3 4 5]
A=
0 1 2
3 4 5
>> all(A)
ans = >> all(A(:,1))
ans =
0 1 1 0
>> any(A) >> all(A(1,:))
ans =
ans = 0
1 1 1 >> all(A(2,:))
ans =
Prepared by Mohammad Alsmirat. Adapted from Gonzalez 2009
1
Floating point number
representation
• Floating point numbers:
– 3, -99, 0.0001, 9.64356, 1.68764e-20, 1i, 3e5i
• How many digits are desplaied?
– get(0,'format')
– Change it with
• format long
function av = average(A)
%AVERAGE Computes the average value o f an array.
% AV = AVERAGE(A) computes the average value of input
% array, A, which must be a 1-D or 2-D array.
Suppose that we have an M-function that accepts an image f and converts it to a specified
class, call it newclass. Only three image classes are acceptable for the conversion: uint8,
uint16, and double. The following code fragment performs the desired conversion and outputs
an error if the class of the input image is not one of the acceptable classes:
switch newclass
case 'uint8'
g = im2uint8(f);
case ' uintl6'
g = im2uintl6(f);
case 'double'
g = im2double(f);
otherwise
error('Unknown or improper image class.' )
end
disp(argument)
>> A= [1 2; 3 4];
>> disp (A)
1 2
3 4
>> sc = 'Digital Image Processing.';
>> disp(sc)
Digital Image Processing.
>> disp('This is another way to display text.')
This is another way to display text.