Thursday 17 March 2016

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);

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home