Numbers between 0.0 and 1.0
A = rand(50, 50);
imshow(A);
A = ones(50, 50);
imshow(A);
A = zeros(50, 50);
imshow(A);
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);
% for Octave only
%pkg install -forge image
pkg load image
% 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');
% an image is a matrix
% what is the size of an image
size(I)
% top right corner of the first layer
I(1:10, 1:10, 1)
% convert to double representation (these notebooks have a bug, not required normally)
I = im2double(I);
imshow(I);
Standard representation of images consists of three layers.
RGB = imread('data/cherry.jpg');
RGB = im2double(RGB);
Plotting one layer will trigger black and white plotting.
imshow(RGB(:, :, 1))
imshow(RGB(:, :, 2))
imshow(RGB(:, :, 3))
R = RGB;
R(:, :, 2) = 0; % set green to zero
R(:, :, 3) = 0; % set blue to zero
imshow(R);
G = RGB;
G(:, :, 1) = 0; % set red to zero
G(:, :, 3) = 0; % set blue to zero
imshow(G);
B = RGB;
B(:, :, 1) = 0; % set red to zero
B(:, :, 2) = 0; % set green to zero
imshow(B);
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);
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);
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);
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);
RGBrs = imresize(RGB, 0.033);
imshow(RGBrs);
imshow(imresize(RGBrs, 30));
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);
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);
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);
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);
RGB = imread('data/cherry.jpg');
RGB = imresize(RGB, 0.1);
imwrite(RGB, 'data/cherry_small.jpg');
imshow(im2double(imread('data/cherry_small.jpg')));
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);