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
figure() %to show in another window
imGray = imread('coins.png'); % a gray image
%make a copy for later use
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.
% 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
p121Gray = imGray(121,121,:);
fprintf('color Gray = %d \n', p121Gray); %print in the command window
% 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
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
if ( (imRGBOrig(i,j,1)> 100) && (imRGBOrig(i,j,2) < 80) && (imRGBOrig(i,j,3) < 80))
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)
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
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
for i=2:mm-1 %the filter is not applied to firs and last columns and rows.
newGray(i,j)=sum(sum( F.*imGray(i-1:i+1,j-1:j+1) ));
% you can observe a blured image. Try with a 5x5 filter!!
Special filters
For some image effects you can use a precise filter
%F=[-1,2,-1;-1,2,-1;-1,2,-1]; %vertical contours
F=[1,0,-1;1,0,-1;1,0,-1]; %vertical contours
for i=2:mm-1 %the filter is not applied to firs and last columns and rows.
newGrayV(i,j)=abs(sum(sum((F.*imGray(i-1:i+1,j-1:j+1)) )));
title('vertical contours');
F=[1,1,1;0,0,0;-1,-1,-1]; %horizontal contours
for i=2:mm-1 %the filter is not applied to firs and last columns and rows.
newGrayH(i,j)=abs(sum(sum( F.*imGray(i-1:i+1,j-1:j+1) )));
title('horizontal contours');
figure() %both directions
newContour = (newGrayV+newGrayH)/2;
title('All Contours Together');
Exercise:
Do the same contour detection using the image BB8.jpg
EmBoss effect filter
for i=2:mm-1 %the filter is not applied to firs and last columns and rows.
val=sum(sum(F.*imGray(i-1:i+1,j-1:j+1)))/3;
val=val+0.5; % avoid zero values
Exercise:
Do the same with the image sports.jpg
(c) Numerical Factory 2020