Thursday 17 March 2016

Image Processing Using Matlab:BilinearInterpolation without using Inbuit Function


Problem:

Implement BilinearInterpolation(image, x, y) to compute the linearly interpolated pixel value at (x, y). Both x and y are continuous values.



Main Function:

clear
close all
input = imread('Moire_small.jpg');
input1=rgb2gray(input);
figure,imshow(input1),title('original');
output = imresize(input1,4,'nearest');
out2=BilinearInterpolation(input1);
figure,imshow(output),title('Bilinear');

Bilinear Interpolation:

function[out1,out2]=BilinearInterpolation(input)

out1=input;
out2=input;
for i = 1:size(input, 1)-4
  for j = 1:size(input, 2)-4
      for k=1:3
          for l=1:3
              out1(i+k,j+l)=(1-k)*(1-l)*(input(i,j))+(k*(1-l)*input(i+4,j))+((1-k)*l*input(i,j+4))+(k*l*input(i+4,j+4));
              out2(i+k,j+l)=input(i+k+1,j+k+1);
          end
      end
     j=j+4;
  end
  i=i+4;

end


Image Processing Using Matlab: Sobel Filter Without using Inbuilt Function

Problem:

Implement SobelImage(image) to compute edge magnitude and orientation information. SobelImage should display the magnitude and orientation of the edges in an image. Please use ”rgb2gray” command in matlab to convert color image into a gray scale image and then run the filter on that.


Main Function:

clear
close all
input = imread('LadyBug.jpg');
%input=double(input);
input1=rgb2gray(input);
figure,imshow(input1),title('original');
[im1,im2]=SobelImage(input1);
%figure(1)
%imshow(uint8(input)), title('original');
figure(3)
imshow(im1), title('magnitude');
figure(4);
imshow(im2), title('direction');

Sobel Filter Function:

function[magnitude,direction]=SobelImage(im2)
temp1=(im2);
temp=double(temp1);
direction=zeros(size(temp));
for i = 2:size(temp, 1)-2
  for j = 2:size(temp, 2)-2
         % x-direction:
         Gx=((2*temp(i+2,j+1)+temp(i+2,j)+temp(i+2,j+2))-(2*temp(i,j+1)+temp(i,j)+temp(i,j+2)));
         %y-direction:
         Gy=((2*temp(i+1,j+2)+temp(i,j+2)+temp(i+2,j+2))-(2*temp(i+1,j)+temp(i,j)+temp(i+2,j)));
         temp1(i,j)=sqrt(Gx^2+Gy^2); %magnitude of the edge
         direction(i,j)=atan(Gy/Gx); %direction of the edge
  end
end
magnitude=temp1;
direction=double(direction);






Image Processing using Matlab: Sharpening Filter Without Using Inbuilt Matlab Function


Problem:

Implement the function SharpenImage(image, sigma, alpha) to sharpen an image. ”sigma” is the Gaussian standard deviation and alpha is the scale factor . Hint: Use SecondDerivImage.
Original Image


Main Function:

clear
close all
input = imread('Yosemite.png');
input1=rgb2gray(input);
figure,imshow(input1),title('original');
im=SharpenImage(input1,1,5);
figure(3)
imshow(im), title('sharpened');


SharpenImage Function:

function[out]=SharpenImage(im3,sigma,alp)

im7=SecondDerivImage(im3,sigma);
im7=padarray(im7,[1,1],'replicate','pre');
im9=alp*im7;
size(im9)
size(im3)

out=im3-im9;
Sharpened Image

Image Processing: First and Second derivative Without Using Inbuilt Matlab Function

Problem:

Implement the functions FirstDerivImage(image, sigma) and SecondDerivImage(image, sigma) to filter an image with the first and second derivatives of the Gaussian. ”sigma” is the standard deviation of the Gaussian. The first derivative should be computed along the x-axis with a regular Gaussian, while the second derivative should be computed in both directions, i.e., the Mexican hat filter. Hint: To compute the first derivative, first compute the x-derivative of the image, followed by Gaussian blurring the image (see slide in Filters). You can use a similar trick for the second derivative (see slide in Filters.) Remember to add 128 to the final pixel values, so you can see the negative values
(Original Image)


Main Function

clear
close all
input = imread('LadyBug.jpg');
input=rgb2gray(input);
im=FirstDerivImage(input,1);
figure(3)
imshow(im+128), title('First_Derivative');
second_deriv=SecondDerivImage(input,1);
figure(4)
imshow(second_deriv+128), title('2nd_Derivative');


Function First Derivative:

function[out]=FirstDerivImage(im2,sigma)

im3=diff(im2,1); %Calculates(x[i+1]-x[i]) , the second argument to this function gives number of %iteration
out=imgaussfilt(im3,sigma);

First Derivative
Function Second Derivative:

function[Out]=SecondDerivImage(image,sigma)
temp1=diff(image,1,1); % Same as above the third argument gives axis along which diff is calculated
temp2=diff(temp1,1,2);

Out=imgaussfilt(temp2,sigma);

Image Processing With Matlab: One Dimensional Gausian Filter (Separable Filters)

Problem:

Implement the function SeparableGaussianBlurImage (image, sigma) to Gaussian blur an image using separable filters. ”sigma” is the standard deviation of the Gaussian. The separable filter should first Gaussian blur the image horizontally, followed by blurring the image vertically.






Main Function:

clear
close all
input = imread('Seattle.jpg');
%input=double(input);
input=rgb2gray(input);
im3=SeparableGaussianBlurImage(input,2);
figure(1)
imshow(uint8(input)), title('original');
figure(3)
imshow(uint8(im3)), title('Blurred');



Separable Filter:


function[out]=SeparableGaussianBlurImage(image,sigma)

A =image;
A=double(A);
sz1 = sigma+1;
sz=ceil(sz1/2);
[x]=meshgrid(-sz:sz);
[y]=transpose(x);
M = size(x,1)-1;
N = size(y,1)-1;
final_Output=A;
Exp_comp_x = -(x.^2)/(2*sigma*sigma);
Exp_comp_y = -(y.^2)/(2*sigma*sigma);
Kernel_x= exp(Exp_comp_x)/(2*pi*sigma*sigma);
Kernel_y= exp(Exp_comp_y)/(2*pi*sigma*sigma);
Output=A;

% Convolving in X direction

for i = 1:size(A,1)-M
    for j =1:size(A,2)-N
        Output(i,j)=sum(sum( A(i:i+M,j:j+M).*Kernel_x));
 
    end
end
Output = uint8(Output);
figure,imagesc(uint8(Output));

% Convolving in X direction

for i = 1:size(Output,1)-M
    for j =1:size(Output,2)-N
      final_Output(i,j) = sum(sum(A(i:i+M,j:j+M).*Kernel_y));
    end
end
out = final_Output;

Output:

Image Processing Using Matlab: Gaussian Filter Without using Matlab Inbuilt Function

Problem:

 Implement the function GaussianBlurImage(image, sigma) to Gaussian blur an image. ”sigma” is the standard deviation of the Gaussian. Implement the Gaussian blur using a 2D filter without using inbuilt Matlab Functions.


Sol:

Original Image:


main Script:

clear
close all
input = imread('Seattle.jpg');
input=rgb2gray(input);
im3=GaussianBlurImage(input,4);
figure(1)
imshow(uint8(input)), title('original');
figure(3)
imshow(im3), title('Blurred');

Gaussian BlurImage function definition :

function[Out]=GaussianBlurImage(image, sigma)
tic %To calculate the time taken by the function
A=image;
I=double(A);
Output=image;
sz1 = sigma+1;
window_size=ceil(sz1/2);
[x,y]=meshgrid(-window_size:window_size,-window_size:window_size);
M = size(x,1)-1;
N = size(y,1)-1;
Exp_comp = -(x.^2+y.^2)/(2*sigma*sigma);
Kernel= exp(Exp_comp)/(2*pi*sigma*sigma);
I = padarray(I,[window_size,window_size]);
%Convolution
  for i = 1:size(I,1)-M
    for j =1:size(I,2)-N
        Output(i,j) =sum(sum( I(i:i+M,j:j+M).*Kernel));
     
    end
  end
Out= Output;
toc

Modified Image:

Saturday 24 October 2015

Project Athena

The goal of this project is to use Active Learning method to train the robot about different shapes and colors.