Displaying images

Numbers between 0.0 and 1.0

In [1]:
A = rand(50, 50);
imshow(A);
In [2]:
A = ones(50, 50);
imshow(A);
In [3]:
A = zeros(50, 50);
imshow(A);
In [4]:
N = 400;
x = linspace(-5, 5, N);
y = linspace(-5, 5, N);
[X, Y] = meshgrid(x, y);
A = cos(X .* Y);

% shift everything to above zero
A = A - min(min(A));
% normalize everything within the displayable range of 0.0 to 1.0
A = A / max(max(A));

imshow(A);

Reading images

In [5]:
% for Octave only
%pkg install -forge image
pkg load image
In [6]:
% Benjamint444, edited by Fir0002 [CC BY-SA 3.0 (http://creativecommons.org/licenses/by-sa/3.0/)], from Wikimedia Commons
I = imread('data/cherry.jpg');
In [7]:
% an image is a matrix
% what is the size of an image
size(I)
ans =

   1200   1200      3

In [8]:
% top right corner of the first layer
I(1:10, 1:10, 1)
ans =

  254  253  252  253  253  254  255  254  255  254
  252  253  253  254  254  254  254  253  255  254
  252  253  253  254  254  254  253  254  253  253
  253  254  253  254  254  253  254  255  255  254
  253  254  253  255  255  254  255  254  255  254
  253  254  254  254  254  254  255  254  254  254
  254  254  254  253  254  254  255  255  255  254
  255  253  255  254  255  255  254  255  253  254
  253  254  253  253  254  254  255  254  253  254
  253  254  254  255  255  254  255  254  255  255

In [9]:
% convert to double representation (these notebooks have a bug, not required normally)
I = im2double(I); 

imshow(I);

Red, Green, Blue (RGB)

Standard representation of images consists of three layers.

In [10]:
RGB = imread('data/cherry.jpg');
RGB = im2double(RGB);

Plotting one layer will trigger black and white plotting.

In [11]:
imshow(RGB(:, :, 1))
In [12]:
imshow(RGB(:, :, 2))
In [13]:
imshow(RGB(:, :, 3))
In [14]:
R = RGB;
R(:, :, 2) = 0; % set green to zero
R(:, :, 3) = 0; % set blue to zero
imshow(R);
In [15]:
G = RGB;
G(:, :, 1) = 0; % set red to zero
G(:, :, 3) = 0; % set blue to zero
imshow(G);
In [16]:
B = RGB;
B(:, :, 1) = 0; % set red to zero
B(:, :, 2) = 0; % set green to zero
imshow(B);

Resizing images

In [17]:
RGB = imread('data/cherry.jpg');
RGB = im2double(RGB); % my Jupyter notebook has a bug

[X, Y] = meshgrid(1:size(RGB, 1), 1:size(RGB, 2));

Nx = 40;
Ny = 40;
[Xs, Ys] = meshgrid(linspace(1, size(RGB, 1), Nx), linspace(1, size(RGB, 2), Ny));

% interpolate each color layer
Rs = interp2(X, Y, RGB(:, :, 1), Xs, Ys);
Gs = interp2(X, Y, RGB(:, :, 2), Xs, Ys);
Bs = interp2(X, Y, RGB(:, :, 3), Xs, Ys);

% concatenate matrices along the third dimension, three layers of matrices
RGBs = cat(3, Rs, Gs, Bs); 
imshow(RGBs);
In [18]:
R_lin = interp2(Xs, Ys, Rs, X, Y, 'linear');
G_lin = interp2(Xs, Ys, Gs, X, Y, 'linear');
B_lin = interp2(Xs, Ys, Bs, X, Y, 'linear');

% concatenate matrices along the third dimension, three layers of matrices
RGB_lin = cat(3, R_lin, G_lin, B_lin); 
imshow(RGB_lin);
In [19]:
R_near = interp2(Xs, Ys, Rs, X, Y, 'nearest');
G_near = interp2(Xs, Ys, Gs, X, Y, 'nearest');
B_near = interp2(Xs, Ys, Bs, X, Y, 'nearest');

% concatenate matrices along the third dimension, three layers of matrices
RGB_near = cat(3, R_near, G_near, B_near); 
imshow(RGB_near);
In [20]:
R_spl = interp2(Xs, Ys, Rs, X, Y, 'spline');
G_spl = interp2(Xs, Ys, Gs, X, Y, 'spline');
B_spl = interp2(Xs, Ys, Bs, X, Y, 'spline');

% concatenate matrices along the third dimension, three layers of matrices
RGB_spl = cat(3, R_spl, G_spl, B_spl); 
imshow(RGB_spl);
In [21]:
RGBrs = imresize(RGB, 0.033);
imshow(RGBrs);
In [22]:
imshow(imresize(RGBrs, 30));

Filtering images

In [23]:
RGB = imread('data/cherry.jpg');
RGB = im2double(RGB);

% selection using a level for all pixels (1.0 is bright)
thresh = 0.98;
RGB(RGB >= thresh) = 0;
imshow(RGB);
In [24]:
RGBgreen = RGB;

mask2D = (RGBgreen(:, :, 2) <= 0.5);

% concatenate the 2D mask along the third dimension thrice
mask3D = cat(3, mask, mask, mask);

% repeat the 2D matrix once along rows, once along cols and three times along the depth/thickness
mask3D = repmat(mask2D, [1, 1, 3]);

% use the mask and set everything to 0.0 (dark)
RGBgreen(mask3D) = 0.0;
imshow(RGBgreen);
error: 'mask' undefined near line 2 column 17
In [25]:
RGBred = RGB;

% different condition on each color
maskR = (RGBred(:, :, 1) <= 0.6);
maskG = (RGBred(:, :, 2) >= 0.4);
maskB = (RGBred(:, :, 3) >= 0.9);
mask3D = cat(3, maskR, maskG, maskB);
RGBred(mask3D) = 0.0;
imshow(RGBred);
In [26]:
RGB = imread('data/cherry.jpg');
RGB = im2double(RGB);

% convert image to black and white
BW = rgb2gray(RGB);

% extract just the color green
G = RGB(:, :, 2);
mask = G >= 0.65; % save the mask

% use new black and white layer
G = BW;
% heighten the green in it
G(mask) = G(mask) + 0.3;

% form a composite image out of the layers
SC = cat(3, BW, G, BW);
imshow(SC);

Writing images

In [27]:
RGB = imread('data/cherry.jpg');
RGB = imresize(RGB, 0.1);
imwrite(RGB, 'data/cherry_small.jpg');

imshow(im2double(imread('data/cherry_small.jpg')));

Fractal

In [28]:
load('data/fractal.mat');

% make convergence just a really long divergence
IT(IT == 0) = 1000;

% convert iterations to a log scale
IT = log10(IT);

% shift to above zero
IT = IT - min(min(IT));

% normalize to be between 0.0 - 1.0
IT = IT ./ max(max(IT));

% rescale to preserve dimensions
xrng = 1 - (-2);
yrng = 1 - (-1);

width = 1500;
height = round(yrng / xrng * width);

[Xorg, Yorg] = meshgrid(1:size(IT, 1), 1:size(IT, 2));
[Xeq, Yeq] = meshgrid(linspace(1, size(IT, 1), width), linspace(1, size(IT, 2), height));
ITeq = interp2(Xorg, Yorg, IT, Xeq, Yeq);

% visualize
imshow(ITeq);