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

Face Detection - MATLAB CODE

                                  Lets see how to detect face, nose, mouth and eyes using the MATLAB built-in class and function. Based on Viola-Jones face detection algorithm, the computer vision system toolbox contains vision.CascadeObjectDetector System object which detects objects based on above mentioned algorithm.
  
Prerequisite: Computer vision system toolbox

FACE DETECTION:

clear all
clc
%Detect objects using Viola-Jones Algorithm

%To detect Face
FDetect = vision.CascadeObjectDetector;

%Read the input image
I = imread('HarryPotter.jpg');

%Returns Bounding Box values based on number of objects
BB = step(FDetect,I);

figure,
imshow(I); hold on
for i = 1:size(BB,1)
    rectangle('Position',BB(i,:),'LineWidth',5,'LineStyle','-','EdgeColor','r');
end
title('Face Detection');
hold off;




The step(Detector,I) returns Bounding Box value that contains [x,y,Height,Width] of the objects of interest.


BB =

    52    38    73    73
   379    84    71    71
   198    57    72    72

NOSE DETECTION:


%To detect Nose
NoseDetect = vision.CascadeObjectDetector('Nose','MergeThreshold',16);



BB=step(NoseDetect,I);


figure,
imshow(I); hold on
for i = 1:size(BB,1)
    rectangle('Position',BB(i,:),'LineWidth',4,'LineStyle','-','EdgeColor','b');
end
title('Nose Detection');
hold off;





EXPLANATION:


To denote the object of interest as 'nose', the argument  'Nose' is passed.

vision.CascadeObjectDetector('Nose','MergeThreshold',16);

The default syntax for Nose detection :
vision.CascadeObjectDetector('Nose');

Based on the input image, we can modify the default values of the parameters passed to vision.CascaseObjectDetector. Here the default value for 'MergeThreshold' is 4.

When default value for 'MergeThreshold' is used, the result is not correct.
Here there are more than one detection on Hermione.




To avoid multiple detection around an object, the 'MergeThreshold' value can be overridden. 


MOUTH DETECTION:



%To detect Mouth
MouthDetect = vision.CascadeObjectDetector('Mouth','MergeThreshold',16);

BB=step(MouthDetect,I);


figure,
imshow(I); hold on
for i = 1:size(BB,1)
 rectangle('Position',BB(i,:),'LineWidth',4,'LineStyle','-','EdgeColor','r');
end
title('Mouth Detection');
hold off;



EYE DETECTION:


%To detect Eyes
EyeDetect = vision.CascadeObjectDetector('EyePairBig');

%Read the input Image
I = imread('harry_potter.jpg');

BB=step(EyeDetect,I);



figure,imshow(I);
rectangle('Position',BB,'LineWidth',4,'LineStyle','-','EdgeColor','b');
title('Eyes Detection');
Eyes=imcrop(I,BB);
figure,imshow(Eyes);







Cropped Image


I will discuss more about object detection and how to train detectors to identify object of our interest in my upcoming posts. Keep reading for updates.

like button Like "IMAGE PROCESSING" page

Image Sharpening using second order derivative –(Laplacian)


Prerequisite: Read EdgeDetection- fundamentals

The derivative operator Laplacian for an Image is defined as



For X-direction,

For Y-direction,


By substituting, Equations in Fig.B and Fig.C in Fig.A, we obtain the following equation







The equation  represented in terms of Mask:
0
1
0
1
-4
1
0
1
0

When the diagonals also considered then the equation becomes,



The Mask representation of the above equation,
1
1
1
1
-8
1
1
1
1

Now let’s discuss further how image sharpening is done using Laplacian.

Equation:
Where f(x,y)  is the input image
              g(x,y) is the sharpened image and
               c= -1 for the above mentioned filter masks.(fig.D and fig.E)

MATLAB CODE:

%Input Image
A=imread('coins.png');
figure,imshow(A);



%Preallocate the matrices with zeros
I1=A;
I=zeros(size(A));
I2=zeros(size(A));

%Filter Masks
F1=[0 1 0;1 -4 1; 0 1 0];
F2=[1 1 1;1 -8 1; 1 1 1];

%Padarray with zeros
A=padarray(A,[1,1]);
A=double(A);

%Implementation of the equation in Fig.D
for i=1:size(A,1)-2
    for j=1:size(A,2)-2
       
        I(i,j)=sum(sum(F1.*A(i:i+2,j:j+2)));
       
    end
end

I=uint8(I);
figure,imshow(I);title('Filtered Image');









The Laplacian derivative equation has produced grayish edge lines and other areas are made dark(background)

%Sharpenend Image
%Refer Equation in Fig.F
B=I1-I;
figure,imshow(B);title('Sharpened Image');




The Filter Image is combined with the Original input image thus the background is preserved and the sharpened image is obtained .



For a Filter Mask that includes Diagonal,


1
1
1
1
-8
1
1
1
1

Filtered Image:


Sharpened Image:



like button Like "IMAGE PROCESSING" page

Edge Detection-Fundamentals


The derivatives of a digital function are defined in terms of differences.
The above statement made me to analyze about derivatives and how it is used for edge detection.  The first time when I came across the edge detection operation [Example: edge(Image,’sobel’)], I wondered how it worked. 
Consider a single dimensional array,
A =
5
4
3
2
2
2
2
8
8
8
6
6
5
4
0

MATLAB CODE:

x=1:15;
y=[5 4 3 2 2 2 2 8 8 8 6 6 5 4 0];

figure,
plot(x,y,'-o','LineWidth',3,'MarkerEdgeColor','k','Color','y');
title('Input Array');




First-order Derivative for one dimensional function f(x):




MATLAB  CODE:

x1=1:14;
y1=diff(y,1);
figure,
plot(x1,y1,'-o','LineWidth',3,'MarkerEdgeColor','k','Color','r');


-1
-1
-1
0
0
0
6
0
0
-2
0
-1
-1
-4
































NOTE: The contiguous values are zero. Since the values are nonzero for non-contiguous values, the result will be thick edges.
The first-order derivative produces thicker edges.

Second-order Derivative for one dimensional function f(x):

















MATLAB CODE:

x2=1:13;
y2=diff(y,2);
figure,
plot(x2,y2,'-o','LineWidth',3,'MarkerEdgeColor','k','Color','g');


0
0
1
0
0
6
-6
0
-2
2
-1
0
-3































The Second-order derivative gives finer result compared to first-order derivative. It gives fine detailed thin lines and isolated points.  Let’s see how the second-order derivative used for Image sharpening (Laplacian) in my upcoming post.
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
Previous Post Next Post Home
Google ping Hypersmash.com