Image as matrix data in Matlab

Matlab allow us to load digital images and to manipulate the numbers associated with each pixel (color images RGB or gray images).
Usually color data are uint8 variables, therefore it is convenient to convert these numbers to double if products or divisions are involved in the data manipulation.

Load and show an image

The Matlab functions imread and imshow allow us load images to Matlab
imRGB = imread('greens.jpg'); % a color image
imshow(imRGB)
figure() %to show in another window
imGray = imread('coins.png'); % a gray image
imshow(imGray)
%make a copy for later use
imRGBOrig= imRGB;
imGrayOrig= imGray;

Manipulating an image

If you have already load an image you can acces the pixel values. To know its size you have to take into account 3 dimensions: witdh x height x depth. Being depth 1 or 3 according to color or gray images.
[m,n,p]=size(imRGB);
[mm,nn,pp]=size(imGray);
 
% access to color channel values for pixell 121x121
p121RGB = imRGB(121,121,:);
fprintf('colors RGB = %d %d %d \n', p121RGB); %print in the command window
colors RGB = 23 46 28
p121Gray = imGray(121,121,:);
fprintf('color Gray = %d \n', p121Gray); %print in the command window
color Gray = 68
 
% modify a pixel value (to red)
imRGB(121,121,1) = 255; %assign max red color channel
imRGB(121,121,2) = 0; %assign min green color channel
imRGB(121,121,3) = 0; %assign min blue color channel
% if you have only a channel (to white)
imGray(121,121,1) = 255; %assign max red color channel
 
% We can also modify a set of pixels like a matrix
%
imRGB(140:220,140:220,:)=255; %white square
imshow(imRGB)

Manipulate according color level

We will change the red pixels (until a certain level) to black. All three channels are explored before changing the color.
imRGB = imRGBOrig; % recover original values
for i=1:m
for j=1:n
if ( (imRGBOrig(i,j,1)> 100) && (imRGBOrig(i,j,2) < 80) && (imRGBOrig(i,j,3) < 80))
imRGB(i,j,:)=[0,0,0];
end
end
end
 
figure()
imshow(imRGB);

Apply filters to an image (kernel convolution)

A filter or kernel F is a small matrix (of size kxk) that we will use to change the present color value of a pixel according to the respective kxk neighbours. Image must be converted to double before apply the filter.
The value of pixel (i,j) is computed from the kernel convolution formula (for a 3x3 example)
imgNew(i,j)=
F(1,1)*img(i-1,j-1)+F(1,2)*img(i-1,j)+F(1,3)*img(i-1,j+1)+
F(2,1)*img(i,j-1) +F(2,2)*img(i,j) +F(2,3)*img(i,j+1)+
F(3,1)*img(i+1,j-1)+F(3,2)*img(i+1,j)+F(3,3)*img(i+1,j+1);
or more compact (you must use a 3x3 submatrix centered at (i,j))
imgNew(i,j)= sum(sum( F.*img(i-1:i+1,j-1:j+1) ));
F=[1,1,1;1,1,1;1,1,1]/9 %is a mean filter
F = 3×3
0.1111 0.1111 0.1111 0.1111 0.1111 0.1111 0.1111 0.1111 0.1111
imGray=im2double(imGray); %convert to double pixel data
newGray=imGray;
for i=2:mm-1 %the filter is not applied to firs and last columns and rows.
for j=2:nn-1
newGray(i,j)=sum(sum( F.*imGray(i-1:i+1,j-1:j+1) ));
end
end
subplot(1,2,1)
imshow(imGray)
subplot(1,2,2)
imshow(newGray)
% you can observe a blured image. Try with a 5x5 filter!!

Special filters

For some image effects you can use a precise filter
% Contour filters
%F=[-1,2,-1;-1,2,-1;-1,2,-1]; %vertical contours
F=[1,0,-1;1,0,-1;1,0,-1]; %vertical contours
newGrayV=imGray;
for i=2:mm-1 %the filter is not applied to firs and last columns and rows.
for j=2:nn-1
newGrayV(i,j)=abs(sum(sum((F.*imGray(i-1:i+1,j-1:j+1)) )));
end
end
subplot(1,2,1)
imshow(imGray)
subplot(1,2,2)
imshow(newGrayV)
title('vertical contours');
figure()
F=[1,1,1;0,0,0;-1,-1,-1]; %horizontal contours
newGrayH=imGray;
for i=2:mm-1 %the filter is not applied to firs and last columns and rows.
for j=2:nn-1
newGrayH(i,j)=abs(sum(sum( F.*imGray(i-1:i+1,j-1:j+1) )));
end
end
subplot(1,2,1)
imshow(imGray)
subplot(1,2,2)
imshow(newGrayH)
title('horizontal contours');
figure() %both directions
newContour = (newGrayV+newGrayH)/2;
imshow(newContour)
title('All Contours Together');

Exercise:

Do the same contour detection using the image BB8.jpg

EmBoss effect filter

F=[2,0,0;
0,-1,0;
0,0,-1];
 
newGray=imGray;
for i=2:mm-1 %the filter is not applied to firs and last columns and rows.
for j=2:nn-1
val=sum(sum(F.*imGray(i-1:i+1,j-1:j+1)))/3;
val=val+0.5; % avoid zero values
newGray(i,j)=val;
end
end
figure()
subplot(1,2,1)
imshow(imGray)
subplot(1,2,2)
imshow(newGray)

Exercise:

Do the same with the image sports.jpg
(c) Numerical Factory 2020