Sunday, January 29, 2012

Region Growing


This method gets image and threshold as arugments and gets the mouse click coordinates as the seed to proceed. Here, starting from the seed the intensity values of each pixel is compared with its neighbours and if it is within the threshold, it'll be marked as one.

Matlab Code

function regionGrowing(name,T)
    im=rgb2gray(imread(name));
    im=im2double(im);
    [r,c]=size(im);
    A=zeros(r,c); % segmented mask
    F=[]; % frontier list
    subplot(1,2,1);
    imshow(im);
    title('Original');
    s=uint16(ginput(1));    % get the click coordinates
    s=[s(2),s(1)];      % [row,col]
    A(s(1),s(2))=1;
    F=[F;s];
    while(~isempty(F)) % if frontier is empty
        n=neighbours(F(1,1),F(1,2),r,c);   % 4 neighbourhood
        for i=1:size(n,1)
            if(abs(im(F(1,1),F(1,2))-im(n(i,1),n(i,2)))<T && A(n(i,1),n(i,2))~=1)% less than threshold & not already segmented
                A(n(i,1),n(i,2))=1;
                F=[F;n(i,1),n(i,2)];
            end
        end
        F(1,:)=[];
    end
    subplot(1,2,2);
    imshow(A);
    title(sprintf('Threshold: %0.4f',T));
end

function out=neighbours(s1,s2,r,c)
    out=[];
    if(s2>1),  out=[out;s1,s2-1];   end
    if(s1>1),  out=[out;s1-1,s2];   end
    if(s1<r),  out=[out;s1+1,s2];   end
    if(s2<c),  out=[out;s1,s2+1];   end
end

Output

The segmented region is shown in white.


Thursday, January 5, 2012

Gaussian Smoothing


This code takes image name and the kernel size as arguments and does Gaussian smoothing by creating a Gaussian kernel of specified size and apply the kernel to the image.


The output will be a blurred image.


Matlab Code

function gaussianSmoothing(name,w)
    h = gaussianKernel(w); % w x w Gaussian kernel    
    imrgb = imread(name);
    im = im2double(rgb2gray(imrgb));
    result = conv2(im,h,'same'); % Apply the kernel
    subplot(1 ,2 ,1)
    imshow(im);
    title('Original');
    subplot(1 ,2 ,2)
    imshow(result)
    title('Result');
end


function h=gaussianKernel(w)
    x=-floor(w/2):floor(w/2);
    y=-floor(w/2):floor(w/2);
    [X,Y]=meshgrid(x,y);        
    Z=(1/2*pi)*exp(-(X.^2+Y.^2)/2); % sigma=1
    h=Z/sum(sum(Z)); % normalize
end

Output


Gamma Correction


This method takes the image name, c and gamma as arguments and apply the transformation 
s = cr^gamma to each of the pixels and produce the output.


You can note that for gamma > 1, wide range of dark input pixels are mapped into narrow range of output pixels, image will be darkened. Opposite is true for gamma < 1.

Matlab Code

function gammaCorrection(name, c, gamma)
r = imread (name);
r=im2double(r);
r=rgb2gray(r);
s = c * (r .^ gamma);
subplot (1 ,2 ,1)
imshow(r);
title('Original');
subplot (1 ,2 ,2)
imshow(s)
title(sprintf('Gamma: %0.1f',gamma));
end

Output





Zoom Images : Nearest Neighbour & Bilinear Interpolation


The method zoom() will get the image name and the zoom factor as arguments and produce the required zoomed image using both Nearest neighbour and Bilinear interpolation methods to help us compare the results.

In nearest neighbour method, I just repeated the rows and columns to get the required zoom level. 

In bilinear interpolation method, first I copied the original pixel values to a new matrix and did the interpolation in two steps. First, I interpolated the columns (in x-direction) using original pixel values. Then, I interpolated the rows (in y-direction) which finished the bilinear interpolation.

Matlab Code

function zoom (name,n)
    close all
    im=imread(name);
    im=rgb2gray(im);
    imshow(im);
    [r,c]=size(im);
    % NEAREST NEIGHBOUR
    x=zeros(r*n,c*n);
    for i=1:r
        for j=1:c
            x(i*n-n+1:i*n,j*n-n+1:j*n)=repmat(im(i,j),n,n);
        end
    end
    figure
    subplot (1 ,2 ,1)
    imshow(mat2gray(x));
    title('Nearest Neighbour');
    % BILINEAR
    im = im2double(im);
    [r,c]=size(im);
    y=zeros(r*n-n+1,c*n-n+1);
    % COPY THE ORIGINAL VALUES
    for i=1:r
        for j=1:c
            y(i*n-n+1,j*n-n+1)=im(i,j);            
        end
    end         
    % INTERPOLATE IN X-DIRECTION
    [row,col]=size(y);
    for i=1:n:row
        for j=1:col
            m=mod(j,n);
            if(m==0)
                m=n;
            end
            if(m~=1)
                y(i,j)=((m-1)*y(i,j-(m-1)+n) + (n-(m-1))*y(i,j-(m-1)))/n;
            end
        end
    end      
    % INTERPOLATE IN Y-DIRECTION
    for i=1:row
        for j=1:col
            m=mod(i,n);
            if(m==0)
                m=n;
            end            
            if(m~=1)
                y(i,j)=((m-1)*y(i-(m-1)+n,j) + (n-(m-1))*y(i-(m-1),j))/n;
            end
        end
    end
    subplot (1 ,2 ,2)
    imshow(y);
    title('Bilinear');
end

Output


You can notice that, bilinear interpolation method gives more smooth output.