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

Partially Colored gray scale Images

                                  

                                          A RGB image contains three components. Namely: Red, Blue and Green.

The below figure represents various colors and their corresponding RGB (Red , Green and Blue) component values.


Gray scale value of the above RGB components:

A gray scale image can be formed from a RGB image by taking the sum of 29.89% of pixel value of Red component, 58.70% of pixel value of green component and 11.40% of pixel value of Blue component.




For instance, consider the second shade with values [150 9 22] from the color vector.
The corresponding gray scale value = (150*0.2989)+(9*.5870)+(22*.1140)
                                                                = 52.62
                                                                = 53 (approximately) 



Consider the gray scale values 61 ,106 and 83 from the gray scale vector.
If the gray scale value is updated in the RGB components of the colored vector then that particular shade will be in gray scale as shown below.
The value 61 is updated in Red, Green and Blue components. Similarly, 106 and 83 are also updated.


Using this principle, we can use both RGB and gray color in a single image to obtain a partially colored gray scale image.



Let us now learn how to obtain partially colored gray scale image.

Step 1: Read the input RGB image and its corresponding RGB components
 1.       Read an RGB Image
 2.       Store the Red component in matrix R, Green in matrix G and Blue in matrix B.
Step 2:  Define the Grayscale Image
1.       Convert RGB Image to Grayscale Image (GS)
2.       Create a matrix R1, G1 and B1 of the same size of matrix R, G and B
3.       Update the matrices R1,G1 and B1 with the value of the matrix GS

Step 3: Create a Mask
1.       Create a matrix of size GS
2.       Update the pixel positions with one if the pixel position should be RGB else zero or vice versa.
Step 4:  Find the index of the masked positions from the Mask
Step 5:  Create partial color and gray scale
1.       Obtain the Red component (R) for the corresponding index of the mask and update it in the R1 matrix. Similarly, update the other matrices G1 and B1 with values of G and B matrices based on the index of the mask.
2.       Create a Three dimensional matrix of the same size of RGB Image
3.       Update the three dimensional matrix with the R1, G1 and B1 components. This is the required partially coloured gray scale image.
Step 6:  Display the partially colored gray scale image




MATLAB CODE:

%Read a RGB image
A = imread('watch1.jpg');
R = A(:,:,1); %RED component
G = A(:,:,2); %GREEN component
B = A(:,:,3); %BLUE component



%RGB to Gray scale Image
GS = rgb2gray(A);
R1 = GS;
G1 = GS;
B1 = GS;

%Mask - Circle

wndow = uint8((gausswin(size(A,1),12)*gausswin(size(A,2),7)')*255);
wndow = ~wndow;








%Find Index
X = find(wndow==0);

R1(X) = R(X);
G1(X) = G(X);
B1(X) = B(X);

%Create a RGB matrix
C = zeros(size(A));
C(:,:,1) = R1;
C(:,:,2) = G1;
C(:,:,3) = B1;

C = uint8(C);

figure,imshow(C);




 The mask can also be created manually as shown below:







like button Like "IMAGE PROCESSING" page

Convert HSI Image to RGB Image


CONVERT HSI IMAGE TO RGB IMAGE:



Refer How to convert the RGB image to HSI Image


MATLAB CODE:





 figure,imshow(HSI);title('HSI Image');  
 %Obtain the Hue, Saturation and Intensity components  
 H1=HSI(:,:,1);  
 S1=HSI(:,:,2);  
 I1=HSI(:,:,3);  
    
   







 %Multiply Hue by 360 to represent in the range [0 360]  
 H1=H1*360;                                               
    


 %Preallocate the R,G and B components  
 R1=zeros(size(H1));  
 G1=zeros(size(H1));  
 B1=zeros(size(H1));  
 RGB1=zeros([size(H1),3]);  
    


 %RG Sector(0<=H<120)  
 %When H is in the above sector, the RGB components equations are  


    
 B1(H1<120)=I1(H1<120).*(1-S1(H1<120));  
 R1(H1<120)=I1(H1<120).*(1+((S1(H1<120).*cosd(H1(H1<120)))./cosd(60-H1(H1<120))));  
 G1(H1<120)=3.*I1(H1<120)-(R1(H1<120)+B1(H1<120));  


    
 %GB Sector(120<=H<240)  
 %When H is in the above sector, the RGB components equations are  


    
 %Subtract 120 from Hue  
 H2=H1-120;  


    
 R1(H1>=120&H1<240)=I1(H1>=120&H1<240).*(1-S1(H1>=120&H1<240));  
 G1(H1>=120&H1<240)=I1(H1>=120&H1<240).*(1+((S1(H1>=120&H1<240).*cosd(H2(H1>=120&H1<240)))./cosd(60-H2(H1>=120&H1<240))));  
 B1(H1>=120&H1<240)=3.*I1(H1>=120&H1<240)-(R1(H1>=120&H1<240)+G1(H1>=120&H1<240));  


    
 %BR Sector(240<=H<=360)  
 %When H is in the above sector, the RGB components equations are  


    
 %Subtract 240 from Hue  
 H2=H1-240;  


    
 G1(H1>=240&H1<=360)=I1(H1>=240&H1<=360).*(1-S1(H1>=240&H1<=360));  
 B1(H1>=240&H1<=360)=I1(H1>=240&H1<=360).*(1+((S1(H1>=240&H1<=360).*cosd(H2(H1>=240&H1<=360)))./cosd(60-H2(H1>=240&H1<=360))));  
 R1(H1>=240&H1<=360)=3.*I1(H1>=240&H1<=360)-(G1(H1>=240&H1<=360)+B1(H1>=240&H1<=360));  


    
 %Form RGB Image  
 RGB1(:,:,1)=R1;  
 RGB1(:,:,2)=G1;  
 RGB1(:,:,3)=B1;  


    
 %Represent the image in the range [0 255]  
 RGB1=im2uint8(RGB1);  
 figure,imshow(RGB1);title('RGB Image');  
    
   





  





















Explanation











like button Like "IMAGE PROCESSING" page

Converting RGB Image to HSI






Converting RGB Image to HSI

H stands for Hue, S for Saturation and I for Intensity.



MATLAB CODE:
Read a RGB Image
A=imread('peppers.png');
figure,imshow(A);title('RGB Image');




%Represent the RGB image in [0 1] range
I=double(A)/255;

R=I(:,:,1);
G=I(:,:,2);
B=I(:,:,3);

%Hue
numi=1/2*((R-G)+(R-B));
denom=((R-G).^2+((R-B).*(G-B))).^0.5;

%To avoid divide by zero exception add a small number in the denominator
H=acosd(numi./(denom+0.000001));

%If B>G then H= 360-Theta
H(B>G)=360-H(B>G);

%Normalize to the range [0 1]
H=H/360;

%Saturation
S=1- (3./(sum(I,3)+0.000001)).*min(I,[],3);


%Intensity
I=sum(I,3)./3;


%HSI
HSI=zeros(size(A));
HSI(:,:,1)=H;
HSI(:,:,2)=S;
HSI(:,:,3)=I;



figure,imshow(HSI);title('HSI Image');



















Explanation:

1.    Read a RGB image using ‘imread’ function.
2.    Each RGB component will be in the range of [0 255].  Represent the image in [0 1] range by dividing the image by 255.
3.    Find the theta value. If B<=G then H= theta. If B>G then H= 360-theta
4.    Use ‘acosd’ function to find inverse cosine and obtain the result in degrees.
5.    Divide the hue component by 360 to represent in the range [0 1]
6.    Similarly, find the saturation and the intensity components.
7.    Display the image.










like button Like "IMAGE PROCESSING" page

Otsu’s thresholding without using MATLAB function graythresh


                To perform the thresholding I followed these steps:
a.       Reshape the 2 dimensional grayscale image to 1 dimensional.
b.      Find the histogram of the image using  ‘hist’ function.
c.       Initialize a matrix with values from 0 to 255
d.      Find the weight , mean and the variance for the foreground and background
e.      calculate weight of foreground* variance of foreground + weight of background* variance of background.
f.       Find the minimum value.
MATLAB CODE:
%To threshold image without using graythresh function
function mygraythresh
global H Index;
B=imread('tire.tif');

Here I converted the 2d matrix to 1d matrix.
V=reshape(B,[],1);

The histogram of the values from 0 to 255 is stored.
For instance, G(1) contains the number of occurrence of the value zero in the image.
G=hist(V,0:255);
H=reshape(G,[],1);
 'index' is a 1 dimensional matrix ranging between 0 and 255
 Ind=0:255;
 Index=reshape(Ind,[],1);
 result=zeros(size([1 256]));

To avoid many for loops I used only 1 for loop and a function to calculate the weight, mean and variance.

Let me explain the foreground and the background for a value of ‘i’.
if ‘i’ value is 5 then the foreground values will be 0,1,2,3,4,5
and the background values will be 6 to 255.

for i=0:255
     [wbk,varbk]=calculate(1,i);
     [wfg,varfg]=calculate(i+1,255);
    
After calculating the weights and the variance, the final computation is stored in the array ‘result’.
result(i+1)=(wbk*varbk)+(wfg*varfg);
    
    
 end
 %Find the minimum value in the array.                   [threshold_value,val]=min(result);
    
     tval=(val-1)/256;
     
Now convert the image to binary with the calculated threshold value.
bin_im=im2bw(B,tval);
     figure,imshow(bin_im);
 function [weight,var]=calculate(m,n)
%Weight Calculation
     weight=sum(H(m:n))/sum(H);
    
%Mean Calculation
     value=H(m:n).*Index(m:n);
     total=sum(value);
     mean=total/sum(H(m:n));
     if(isnan(mean)==1)
         mean=0;
     end
%Variance calculation.
    value2=(Index(m:n)-mean).^2;
     numer=sum(value2.*H(m:n));
     var=numer/sum(H(m:n));
     if(isnan(var)==1)
         var=0;
     end
    
 end
 end
 
                     
                   
                       
      
     
Threshold value:0.3242

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