Image processing 4
Colormaps in indexed colour
Example 1
Graphs of three components RGB for HSV colormap
figure('Color','w');
rgbplot (hsv (256));
axis([0 256 0 1]);
grid;
colormap (hsv (256));
colorbar ('horiz');
ylabel('RGB components intensity ', 'FontSize', 15, 'FontName', 'Arial CE');
title('hsv','FontSize',15)
Exercise 1
Create graphs for at least 3 other colormaps (jet, hot, cool, gray, bone, copper, pink,
prism, flag).
Custom colormap creation
Example 2
Colormap is a matrix with three columns and 256 elements with values form the range between [0, 1].
More in -> help colormap.
[L1 m]=imread('portret.jpg');
figure
imshow(L1)
isindx = isind(L1)
disp('Random colormaps:')
for i = 1:5
colormap( rand(25,3) )
disp('Press any key...')
pause
end
disp('Interesting colormaps:')
figure
imshow(L1(:,:,1))
map = zeros(256, 3);
map(:,1) = [0:(1/255):1]';
map(:,2) = 1 - [0:(1/255):1]';
colormap(map)
pause;
map(:,1) = [0:(1/255):1]';
map(:,2) = 1 - [0:(1/255):1]';
map(:,2) = ([0:(1/255):1]').^5;
colormap(map)
pause;
map(:,1) = [0:(1/255):1]';
map(:,2) = 1 - [0:(1/255):1]';
map(:,2) = (1 + sin(5*([0:(1/255):1]')))/2;
colormap(map)
pause;
map(:,1) = [0:(1/255):1]';
map(:,2) = (1 + cos(10*(1 - [0:(1/255):1]')))/2;
map(:,3) = [0:(1/255):1]';
colormap(map)
pause;
map(:,1) = [0:(1/255):1]';
map(:,2) = (1 + cos(10*(1 - [0:(1/255):1]')))/2;
map(:,3) = 1 - [0:(1/255):1]';
colormap(map)
pause;
map(:,1) = ([0:(1/255):1]').^3;
map(:,2) = ([0:(1/255):1]').^0.3;
map(:,3) = sqrt(1 - [0:(1/255):1]');
colormap(map)
pause; close all;
Exercise 2
Create your own colormap and apply it to the image (portret.jpg).
x = (1:128)'/128;
y = (1:64)'/64;
z = (1:256)'/256;
r = [x; flipud(x)];
g = [y; flipud(y); y; flipud(y)];
b = z;
M = [r g b];
Example 3
Interactive colormaps:
L1=imread('portret.jpg');
imshow(L1);
colormapeditor
Exercise 3
Using mapeditor try to create a colormap in which the teeth in the image „face.jpg” would be distinguished. You can use imtool in order to precise the values of pixels.
mapa = get(1,'Colormap'); - zapis mapy do zmiennej
Example 4
Artificial image in indexed colour and in the RGB space:
L1=randi(256,3)
figure
imshow (L1, hsv(256), 'InitialMagnification','fit')
title('Image in the indexed colour);
L2 = ind2rgb (L1, hsv)
figure
imshow (L2, 'InitialMagnification','fit')
title('Image in RGB space');
figure
imshow (L2(:,:,1), 'InitialMagnification','fit')
title('Share of the red component’);
figure
imshow (L2(:,:,2), 'InitialMagnification','fit')
title('Share of the green component’);
figure
imshow (L2(:,:,3), 'InitialMagnification','fit')
title('Share of the blue component’);
Exercise 4
Create any image matrix in indexed color with dimensions 3 x 3. Display it in the
chosen colormap and convert it into grayscale.
Images in RGB colours
Example 5
An artificial image RGB+gray:
L1 = zeros(2,2,3)
Green | Blue |
---|---|
Gray | Red |
L1(1,1,2) = 1
L1(1,2,3) = 1
L1(2,1,:) = 0.5
L1(2,2,1) = 1
figure
imshow(L1, 'InitialMagnification','fit')
Exercise 5
Create an artificial image with pixels colours shown below:
Yellow | Magenta |
---|---|
Black | Cyan |
L1 = zeros(2,2,3)
L1(1,1,2) = 1
L1(1,1,1) = 1
L1(1,2,1) = 1
L1(1,2,3) = 1
L1(2,2,3) = 1
L1(2,2,2) = 1
figure
imshow(L1,'InitialMagnification','fit')
Example 6
Color mode change from indexed to grayscale and image resolution reduction:
2, 4, 8, 16 i 32 times (nearest neighbor method)
[L1,map]=imread('Arch.bmp')
L1=ind2gray(L1,map)
figure
imshow(L1,'notruesize')
[L2a]=imresize(L1,size(L1)/2,'nearest')
figure
imshow(L2a,'notruesize')
[L2a]=imresize(L1,size(L1)/4,'nearest')
figure
imshow(L2a,'notruesize')
[L2a]=imresize(L1,size(L1)/8,'nearest')
figure
imshow(L2a,'notruesize')
[L2a]=imresize(L1,size(L1)/16,'nearest')
figure
imshow(L2a,'notruesize')
[L2a]=imresize(L1,size(L1)/32,'nearest')
figure
imshow(L2a,'notruesize')
pause; close all;
Exercise 6
Functions: rgb2ind, ind2gray, rgb2hsv are used to convert between image
types. Please, try to use them with selected images.
Example 7
Changing of the image color range from 256 into 4 shades of gray:
L1=uint8 ((0:254)'*ones ([1 40]))'
figure; imshow(L1,gray (256));
[L2a,map2a]=imapprox(L1,gray (256),4);
figure; imshow(L2a,map2a);
delta=256/4
L2b=uint8 (round ((double (L1)-(delta/2-1))/delta)*delta+(delta/2-1))
figure; imshow(L2b)
Exercise 7
For the image above, change the color range to 8 and then to 16 shades of gray
Example 8
Changing of the real image color range:
[L1,map]=imread('Arch.bmp')
figure
imshow(L1,map)
ind2gray(L1,map)
[L2a,map2a]=imapprox(L1,map,2^8)
figure
imshow(L2a,map2a)
[L2b,map2b]=imapprox(L1,map,2^4)
figure
imshow(L2b,map2b)
[L2c,map2c]=imapprox(L1,map,2^2)
figure
imshow(L2c,map2c)
[L2d,map2d]=imapprox(L1,map,2^1)
figure
imshow(L2d,map2d)
Example 9
An image section showing the level of its brightness (intensity):
[L1]=imread('portret.jpg');
figure; imshow(L1);
[mm,nn]=size(L1);
c=improfile(L1,[1 nn],[82 82]);
line([1 nn],[82 82],'Color',[1 1 1],'LineWidth',3);
set(gcf,'Color',([1 1 1]));
figure; plot(c(:,:,1),'k');
set(gcf,'Color',([1 1 1]));
xlabel('n','FontSize',15,'FontName','Arial CE');
ylabel('L(82,n)','FontSize',15,'FontName','Arial CE')
We can create the image section interactively. When the image is displayed in the
figure, write „p=improfile” and show the profile starting and ending points. Then
display the graph like the previous one.
Example 10
Sections showing RGB intensity:
L1=imread('e0102.bmp');
figure; imshow(L1);
[mm,nn]=size(L1);
c=improfile(L1,[12 240],[65 160]);
line([12 240],[65 160],'Color',[1 1 1],'LineWidth',3);
set(gcf,'Color',([1 1 1]));
figure; plot(c(:,:,1),'r');
hold on;
plot(c(:,:,2),'g');
plot(c(:,:,3),'b');
set(gcf,'Color',([1 1 1]));
xlabel('składowe RGB','FontSize',15,'FontName','Arial CE');
ylabel('L(m,n,1)-R, L(m,n,2)-G, L(m,n,3)-B', 'FontSize',15, 'FontName', 'Arial
CE')
Example 11
Values of pixels intensity displayed in a three-dimensional graph. The
height of a particular pixel corresponds with its brightness.
[L1 m]=imread('portret.jpg');
figure
imshow(L1)
[r c] = size(L1)
[x,y] = meshgrid(0:c-1,0:r-1);
z = double(L1)
size(x)
size(y)
size(z)
surfc(x,y,z);
axis([0 c 0 r 0 255])
L2 = imresize(L1,size(L1)/8,'nearest')
figure
imshow(L1)
[r c] = size(L2)
[x,y] = meshgrid(0:c-1,0:r-1);
z = double(L2)
size(x)
size(y)
size(z)
surfc(x,y,z);
axis([0 c 0 r 0 255])
pause; close all;
Example 12
RGB sections created interactively – press the left button and show the section
line. The right button ends selection.
[L1]=imread('e0102.bmp');
figure; imshow(L1);
p = improfile
size(p)
figure;
plot(p(:,:,1),'r')
title('RED')
figure;
plot(p(:,:,2),'g')
title('GREEN')
figure;
plot(p(:,:,3),'b')
title('BLUE')
Example 13
RGB colors intensity in the selected image points:
[L1]=imread('e0102.bmp')
figure
imshow(L1)
x=[12 146 300]
y=[104 156 129]
piksel=impixel(L1,x,y)
text(x(1),y(1),'*','FontSize',45,'FontName','Arial CE','Color',([1 1 1]))
text(x(2),y(2),'*','FontSize',45,'FontName','Arial CE','Color',([1 1 1]))
text(x(3),y(3),'*','FontSize',45,'FontName','Arial CE','Color',([1 1 1]))
set(gcf,'Color',([1 1 1]))
piksel
while true
piksel = impixel
end
Exercise 8
Using chosen images, execute different colour mode conversions. Check pixels
values in the selected places.