Lets Learn together... Happy Reading

" Two roads diverged in a wood, and I,
I took the one less traveled by,
And that has made all the difference "-Robert Frost

Color Slicing using HSV color space

Figure 1. 


   




          HSV color space can be used for assigning different colors to the foreground and background of the same image conveniently  in comparison to the equivalent RGB image. HSV color space consists of 3 components namely the Hue, the Saturation and the Value.


In MATLAB, HSV color space of an image is three dimensional matrix and each matrix represents each of the 3 component (Hue,Saturation,Value). Hue and saturation range between zero and one. While saturation defines colorfulness hue is specific to the color.













%MATLAB CODE:
A = imread('swimmer.jpg');

figure,imshow(A);title('Original Image');



Original Image
HSV = rgb2hsv(A);
H = HSV(:,:,1); %Hue
figure,imshow(H);colorbar;

Hue

H( H > mean2(H) ) = 1;
HSV(:,:,1) = H;

C = hsv2rgb(HSV);
figure,imshow(C);title('Hue Modified');
Hue Modified

EXPLANATION:

The original image is in RGB  format and it is converted to HSV color space using the MATLAB command ‘rgb2hsv’. The resultant is a three dimensional matrix with Hue, Saturation and Value components in each one of them. By comparing the original RGB image and the Hue component, we can understand that the blue color as high value comparing to other colors in the original image.

Hue is a color wheel, where the colors start from red, then move on to yellow, green, cyan, blue, magenta and ends up again in red.
In our example,the values above the average in the Hue matrix is made 1.(i.e. red). The background of the image is changed from blue to red.

If  the whole image including the swimmer needs to be changed to red then instead of finding the average or masking the image, assign zero or 1 to Hue matrix.
After the modification, use ‘hsv2rgb’ command to return back to RGB color space.

%MATLAB CODE:

HSV = rgb2hsv(A);
S = HSV(:,:,2); %Saturation
figure,imshow(S);colorbar;


Saturation


S(:,:)=0;
HSV(:,:,2) = S;

C = hsv2rgb(HSV);
figure,imshow(C);title('Saturation Modified');

Saturation Modified

 EXPLANATION:
 In this example, the saturation component is modified. The high values of Saturation illustrates that the regions are bright and colorful while the low values illustrates that they are dull and colorless.When saturation matrix is made zero, the colorfulness is completely lost. The above figure clearly shows the gray shade image that is obtained as a result of modifying the saturation matrix.


%MATLAB CODE:

HSV = rgb2hsv(A);
H = HSV(:,:,1); %Hue
S = HSV(:,:,2); %Saturation

H( H > mean2(H) ) = 0.42;
S( H < mean2(H) )=0;
S( H >= mean2(H) )=1;

HSV(:,:,2) = S;
HSV(:,:,1) = H;


C = hsv2rgb(HSV);
figure,imshow(C);title('Saturation Modified - Background');

EXPLANATION:
In this example, both the Hue and the Saturation matrices are modified simultaneously. The background color is changed from blue to green after changing the Hue matrix. The Saturation matrix is changed partially such that the background is not affected but the color on the swimmer is made gray.

%MATLAB CODE:
HSV = rgb2hsv(A);
H = HSV(:,:,1); %Hue
S = HSV(:,:,2); %Saturation


S( H < mean2(H) )=1;
S( H >= mean2(H) )=0;

HSV(:,:,2) = S;
HSV(:,:,1) = H;


C = hsv2rgb(HSV);
figure,imshow(C);title('Saturation Modified - Foreground');

EXPLANATION:
In this example, the background color is made shades of gray while the swimmer still retains the color. The masking is done based on the foreground and background on the saturation matrix and a value of zero is assigned to the background and one is assigned to foreground. From this example, it is evident that if the saturation matrix contains zero then the image in RGB color space will contain shades of gray whereas if the saturation matrix contains one then it will contain fully saturated more colorful image in the RGB color space.

The image(Figure.1) above shows the swimmer and different background colors by modifying the Hue matrix.
like button Like "IMAGE PROCESSING" page

Gray Scale to Pseudo color transformation


   Transformation of a gray scale image into pseudo color image helps in better visualization of the image. In this tutorial, different ways to apply pseudo color transformation to a gray scale image will be discussed along with the MATLAB Code.
The main idea behind pseudo color transformation is to perform three independent transformation (RED,GREEN and BLUE) on the grayscale or intensity image and map  the corresponding intensity value in the image to the result obtained.


Steps to be performed:





Functional Block Diagram

Reference: Digitial Image Processing by Gonzalez

MATLAB CODE:

%READ AN INPUT IMAGE
A = imread('cameraman.tif');
%PRE-ALLOCATE A MATRIX
Output = zeros([size(A,1) size(A,2) 3]);

%Define a colormap
map = colormap(jet(256));

%Assign the columns to 1-D RED,GREEN and BLUE
Red = map(:,1);
Green = map(:,2);
Blue = map(:,3);

%MAP THE COLORS BASED ON THE INTENSITY OF THE IMAGE

Output(:,:,1) = Red(A);
Output(:,:,2) = Green(A);
Output(:,:,3) = Blue(A);


Output = im2uint8(Output);
%DISPLAY THE IMAGE
imshow(Output);

%Save the image in PNG or JPEG format
imwrite(Output,'pseudo_color.jpg');






 Checkerboard with colormaps:



%READ AN INPUT IMAGE
A = imread('coins.png');
%RESIZE THE IMAGE
A = imresize(A,[256 256]);

%PRE-ALLOCATE THE OUTPUT MATRIX
Output = zeros([size(A,1) size(A,2) 3]);

%DEFINE SOME COLORMAPS
maps={'jet(256)';'hsv(256)';'cool(256)';'spring(256)';'summer(256)';'parula(256)';'hot(256)'};

%COLORMAP 1
map=colormap(char(maps(7)));
Red = map(:,1);
Green = map(:,2);
Blue = map(:,3);

R1 = Red(A);
G1 = Green(A);
B1 = Blue(A);

%COLORMAP 2
map=colormap(char(maps(6)));
Red = map(:,1);
Green = map(:,2);
Blue = map(:,3);
R2 = Red(A);
G2 = Green(A);
B2 = Blue(A);

%SIZE OF THE MATRIX
sz=64;

ind=0;

for i=1:sz:size(A,1)-(sz-1)
for j=1:sz:size(A,2)-(sz-1)

%APPLY COLORMAPS BASED ON THE SIZE SZ
if(mod(ind,2)==0)

            Output(i:i+(sz-1),j:j+(sz-1),1) = R1(i:i+(sz-1),j:j+(sz-1));
            Output(i:i+(sz-1),j:j+(sz-1),2) = G1(i:i+(sz-1),j:j+(sz-1));
            Output(i:i+(sz-1),j:j+(sz-1),3) = B1(i:i+(sz-1),j:j+(sz-1));




else
            Output(i:i+(sz-1),j:j+(sz-1),1) = R2(i:i+(sz-1),j:j+(sz-1));
            Output(i:i+(sz-1),j:j+(sz-1),2) = G2(i:i+(sz-1),j:j+(sz-1));
            Output(i:i+(sz-1),j:j+(sz-1),3) = B2(i:i+(sz-1),j:j+(sz-1));


end

        ind=ind+1;
end

    ind=ind+1;
end
Output = im2uint8(Output);

%FINAL IMAGE
imshow(Output);

EXPLANATION:
The above code is to combine two colormaps to obtain checkboard effect on the image. To obtain checkboards in different size change the 'sz' variable with 4,8,16..etc.




UPPER OR LOWER TRIANGLE :

MATLAB CODE:

%READ INPUT IMAGE
A = imread('coins.png');
%RESIZE IMAGE
A = imresize(A,[256 256]);
%PRE-ALLOCATE THE OUTPUT MATRIX
Output = ones([size(A,1) size(A,2) 3]);

%COLORMAPS
maps={'jet(256)';'hsv(256)';'cool(256)';'spring(256)';'summer(256)';'parula(256)';'hot(256)'};


%COLORMAP 1
map=colormap(char(maps(1)));
Red = map(:,1);
Green = map(:,2);
Blue = map(:,3);

R1 = Red(A);
G1 = Green(A);
B1 = Blue(A);


%COLORMAP 2
map=colormap(char(maps(7)));
Red = map(:,1);
Green = map(:,2);
Blue = map(:,3);




%RETRIEVE POSITION OF UPPER TRIANGLE
[x,y]=find(triu(Output)==1);
Output(:,:,1) = Red(A);
Output(:,:,2) = Green(A);
Output(:,:,3) = Blue(A);

for i=1:numel(x)

        Output(x(i),y(i),1)=R1(x(i),y(i));
        Output(x(i),y(i),2)=G1(x(i),y(i));
        Output(x(i),y(i),3)=B1(x(i),y(i));

end

Output = im2uint8(Output);

%FINAL IMAGE
imshow(Output);


EXPLANATION:
Upper triangular part of the image matrix with a colormap and lower triangular part of the image matrix with different colormap.


like button Like "IMAGE PROCESSING" page
Previous Post Next Post Home
Google ping Hypersmash.com