# IMAGE PROCESSING

" 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:

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 "IMAGE PROCESSING" page

### COLOR HISTOGRAM EQUALIZATION - MATLAB CODE

Histogram Equalization can be considered as redistribution of the intensity of the image. Color histogram equalization can be achieved by converting a color image into HSV/HSI image and enhancing the Intensity while preserving hue and saturation components.

However, performing histogram equalization on components of R,G and B independently will not  enhance the image. At the end of this post, check the histogram of before and after histogram equalization of an image which is obtained by performing histogram equalization on the components(R,G and B) independently.

Steps to be performed:
2.      Obtain the ‘Intensity Matrix’ from the HSI Image matrix
3.      Perform Histogram Equalization on the intensity Matrix
http://angeljohnsy.blogspot.com/2011/04/matlab-code-histogram-equalization.html
4.      Update the Intensity Matrix from the HSI Image matrix with the histogram equalized Intensity matrix

MATLAB CODE:
%COLOR HISTOGRAM EQUALIZATION

%CONVERT THE RGB IMAGE INTO HSV IMAGE FORMAT
HSV = rgb2hsv(I);

%PERFORM HISTOGRAM EQUALIZATION ON INTENSITY COMPONENT
Heq = histeq(HSV(:,:,3));

HSV_mod = HSV;
HSV_mod(:,:,3) = Heq;

RGB = hsv2rgb(HSV_mod);

figure,subplot(1,2,1),imshow(I);title('Before Histogram Equalization');

subplot(1,2,2),imshow(RGB);title('After Histogram Equalization');

EXPLANATION:

RGB image matrix is converted into HSI(Hue ,Saturation and Intensity) format and histogram equalization is applied only on the Intensity matrix . The Hue and Saturation matrix remains the same. The updated HSI image matrix is converted back to RGB image matrix.

%DISPLAY THE HISTOGRAM OF THE ORIGINAL AND THE EQUALIZED IMAGE

HIST_IN = zeros([256 3]);
HIST_OUT = zeros([256 3]);

%http://angeljohnsy.blogspot.com/2011/06/histogram-of-image.html
%HISTOGRAM OF THE RED,GREEN AND BLUE COMPONENTS

HIST_IN(:,1) = imhist(I(:,:,1),256); %RED
HIST_IN(:,2) = imhist(I(:,:,2),256); %GREEN
HIST_IN(:,3) = imhist(I(:,:,3),256); %BLUE

HIST_OUT(:,1) = imhist(RGB(:,:,1),256); %RED
HIST_OUT(:,2) = imhist(RGB(:,:,2),256); %GREEN
HIST_OUT(:,3) = imhist(RGB(:,:,3),256); %BLUE

mymap=[1 0 0; 0.2 1 0; 0 0.2 1];

figure,subplot(1,2,1),bar(HIST_IN);colormap(mymap);legend('RED CHANNEL','GREEN CHANNEL','BLUE CHANNEL');title('Before Applying Histogram Equalization');
subplot(1,2,2),bar(HIST_OUT);colormap(mymap);legend('RED CHANNEL','GREEN CHANNEL','BLUE CHANNEL');title('After Applying Histogram Equalization');

EXPLANATION:
Obtain the histogram of each component (Red,Green and Blue) independently.
Define the colormap ‘mymap’ with three colors namely Red, Green and Blue.
Display the histograms of the components before and after histogram equalization.

NOTE:
Histogram of the above image by processing the components independently gives bad result.

 Histogram Equalization applied on individual components
Like "IMAGE PROCESSING" page