Helpful Image Commands Matlab/Octave

% Load and display an image

img = imread(‘filename.jpg’)

imshow(img)

% Image size

disp(size(img));

% Image class (data type):

disp(class(img));

% At a given location (row, col):

disp(img(50, 100));

% For an entire row:

disp(img(50, :));

plot(img(50, :));

 

% A slice of an image

img(101:103, 201:203))

 

% Crop an image

cropped = img(110:310, 10:160)

 

Color images

Note: Matlab indexes begin at 1 not 0

% Get the red layer (RGB)

img_red = img(:, :, 1);

% Get the green layer (RGB)

img_green = img(:, :, 2);

% Get the blue layer (RGB)

img_blue = img(:, :, 3);

 

Image arithmetic

% Add two images

summed = img_1 + img_2

% Average of two images

average = img_1 / 2 + img_2 / 2

% Multiply by a scalar

0.5 * img

% Blend two images

result = 0.75 * img_1 + 0.25 * img_2

% Image difference

diff = img_1 – img_2

diff = img_2 – img_1

% Absolute image difference

imabsdiff(img_1, img_2)