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

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

Edge detection using Local Variance

Local statistics can determine the gradient of the image. The local variance can be used to generate an edge map.
Steps to be performed:
1.       Read a grayscale image
2.       Define a window size (For eg: 3x3,5x5,7x7)
3.       Find the local variance
4.       Find the global mean of the local variance
5.       Set local variance to zero if it is less than the global mean else set it to one


MATLAB code:
%Boundary Detection - Local Variance
%Read an image
I = imread('rice.png');
figure,imagesc(I);colormap(gray);

I = double(I);

Explanation:
A grayscale image is taken as input for edge detection. If the input image is RGB then convert it to gray scaleusing ‘rgb2gray’.







MATLAB code:

%Define the window size
sz=3;
window = ones(sz)/sz.^2;

%Find the local mean
mu = conv2(I,window,'same');

%Find the local Variance
II = conv2(I.^2,window,'same');
Lvar = II-mu.^2;

figure,imagesc(Lvar);colormap(gray);title('Local Variance of the image');

Explanation:
A window size of 3 by 3 is defined and local variance is computed. Check ‘local variance- matlab code’ to understand how local variance is estimated.

MATLAB CODE:
%Define a Threshold
meanL = mean(Lvar(:));

%Set the pixel values based on threshold
Boundary = zeros(size(Lvar));
Boundary(Lvar < meanL)=1;
Boundary(Lvar >= meanL) = 0;

figure,imagesc(Boundary);colormap(gray);title('Boundary Extracted Image');

Explanation:
 The mean of the local variance is obtained and using the mean value as threshold, the boundary is defined for the image.  The mean value of the given image is 239.3638.



The threshold value can also be set randomly by the user. For instance, set the threshold value to 500.



like button Like "IMAGE PROCESSING" page

Local Variance – MATLAB CODE

Variance Formula:
Let us understand how global variance works. First let us theoretically define variance in simple terms. In lay man terms, variance is defined as how divergent values are from the average value.

Consider a matrix A=[5 5 5;5 5 5]. The variance of the matrix A is zero.. In the given example, the average is 5 and all the elements in the matrix are equal to 5. So finding deviation in the above example is not possible.

Let us consider another matrix B =[3 7 1;5 4 2]. The variance of the matrix B is 3.8889.. Let us arrive at the result using theoretical formula as follows:
Variance of B = Mean of B^2 – (mean of B)^2
                        = 17.333 – 13.444
                        = 3.888
MATLAB CODE:
Var(B(:),1)
Local Variance
Instead of finding variance for the whole matrix, variance is computed based on a small sliding window.
Steps to Perform:

MATLAB CODE:
I = imread('cameraman.tif');
figure,imagesc(I);colormap(gray);title('Original Image');

I = double(I);
%Define the window size
sz = 5;
mn=floor(sz/2);
%Preallocate the matrix
Output = zeros(size(I));
%Pad the matrix with zeros
I = padarray(I,[mnmn]);

for i=1:size(I,1)-mn*2
for j=1:size(I,2)-mn*2
tmp = I(i:i+(sz-1),j:j+(sz-1));
mu = mean(tmp(:));
        tmp2 = mean(tmp(:).^2);
Output(i,j)=tmp2 - mu.^2;
end
end

figure,imagesc(Output);colormap(gray);



Quick Implementation:




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