0% found this document useful (0 votes)
33 views

DIP Assignment 3

This document contains code for performing various digital image processing tasks using MATLAB. It includes code to perform operations like Fourier transforms, image addition/multiplication, filtering, morphological operations like erosion and dilation using structuring elements, and connected component analysis. The code takes input images, applies the specified operations, and displays the output images for comparison.

Uploaded by

Saad Ali Shah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

DIP Assignment 3

This document contains code for performing various digital image processing tasks using MATLAB. It includes code to perform operations like Fourier transforms, image addition/multiplication, filtering, morphological operations like erosion and dilation using structuring elements, and connected component analysis. The code takes input images, applies the specified operations, and displays the output images for comparison.

Uploaded by

Saad Ali Shah
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Digital Image Processing

Assignment 3

Submitted by:
Student Name: Syed Saad Ali Shah

Enrolment No.: 01-134-171-075

Student Name: Abdul Hussain

Enrolment No.: 01-134-171-001

Student Name: Mubeen Ahmad

Enrolment No.: 01-134-171-090

Class and Section: BSCS-7A

Submitted to: Dr. Sumaira Kausar

Department of Computer Science


BAHRIA UNIVERSITY, ISLAMABAD

Code:
img=getimage(handles.axes1);
ft=fft(img);
img2=getimage(handles.axes2);
ft2=fft(img2);
newft=ft+ft2; %sum of fourier transforms
inverseft=fft2(newft); %inverse of sum of fourier transform
axes(handles.axes1);
imshow(ft);
axes(handles.axes2);
imshow(ft2);
axes(handles.axes3);
imshow(newft);
title('sum of fourier transform');
axes(handles.axes4);
imshow(inverseft);
title('inverse of sum of fourier transform');

Output:

Code:
img=getimage(handles.axes1);
ft=fft(img);
img2=getimage(handles.axes2);
ft2=fft(img2);
newimg=img.*img2;%multiple of two images
newft=ft.*ft2; %multiple of fourier transform of two images
axes(handles.axes1);
imshow(ft);
axes(handles.axes2);
imshow(ft2);
axes(handles.axes3);
imshow(newimg);
title('multiple of two images');
axes(handles.axes4);
imshow(newft);
title('multiple of fourier transform of two images');

Output:

Code:
img=getimage(handles.axes1);
ft=fft(img);
i_ft=fft2(ft);
axes(handles.axes3);
imshow(ft);
title('fourier transform');
axes(handles.axes4);
imshow(i_ft);
title('inverse fourier transform');

Output:
Code:
img=getimage(handles.axes2);
[M N]=size(img);
h=fspecial('prewitt');
v=h';
h_trans=fft2(h);
v_trans=fft2(v);

img_trans=fft2(img, 2.*M, 2.*N);


h_inverse=ifft2(imfilter(img_trans, h_trans),2.*M, 2.*N);
v_inverse=ifft2(imfilter(img_trans, v_trans),2.*M, 2.*N);

h_result=real(h_inverse);
v_result=real(v_inverse);

h_crop = h_result(1:M, 1:N);


v_crop = v_result(1:M, 1:N);

added_sum=h_crop+v_crop;

axes(handles.axes2);
imshow(mat2gray(added_sum));
title('added sum');
axes(handles.axes3);
imshow(mat2gray(h_crop));
title('horizontal crop');
axes(handles.axes4);
imshow(mat2gray(v_crop));
title('vertical crop');
Output:

Code:
img=getimage(handles.axes1);

sel1 = strel('rectangle', [5 5]);


sel2 = strel('disk', 4, 4);

result1 = imdilate(img, sel1);


result2 = imdilate(img, sel2);

axes(handles.axes3);
imshow(result1);
title('result1');
axes(handles.axes4);
imshow(result2);
title('result2');
axes(handles.axes2);
imshow(result2);
title('resultant');

Output:
Code:
img=getimage(handles.axes1);
cc1 = bwconncomp(img);
before = cc1.NumObjects;
sel1 = strel('rectangle', [20 20]);

dil = imerode(img, sel1);


cc2 = bwconncomp(dil);
after = cc2.NumObjects;

axes(handles.axes3);
imshow(img);
title(num2str(before));
axes(handles.axes4);
imshow(dil);
title(num2str(after));

Output:

Code:
img = getimage(handles.axes2);

selv = strel('rectangle', [11 3]);


selh = strel('rectangle', [5 11]);
vertical = imerode(img, selv);
horizontal = imerode(img, selh);

axes(handles.axes3); imshow(vertical);
title('Vertical');
axes(handles.axes4); imshow(horizontal);
title('Horizontal');

Output:
Code:
img = imbinarize(mat2gray(getimage(handles.axes1)));
sel = strel('rectangle', [2 12]);
dil = imdilate(img, sel);
cc2 = bwconncomp(dil);
after = cc2.NumObjects;

axes(handles.axes3); imshow(img);
title('Image');
axes(handles.axes4); imshow(dil);
title("No. Words: "+num2str(after));

img = imbinarize(mat2gray(imread("Lines.png")));
Output:
Code:
function myMorphology(img)

bin = imbinarize(img);
% Apply closing
dilated = morph_op(bin, 6, 3, 'dilate');
closed = morph_op(dilated, 4, 4, 'erode');
% Eroded
eroded = morph_op(closed, 4, 4, 'erode');
% Difference
diff = closed - eroded;
imshow(diff);

function out = morph_op(img, r, c, op)


out = img;
pr = floor(r/2);
pc = floor(c/2);
padded = padarray(img, [pr pc], 0, 'both');
[M, N] = size(img);
for i=1:M
for j=1:N
hit = 0;
fit = 1;
for k=-pr:pr
for l=-pc:pc
if (padded(i+pr+k, j+pc+l) == 1)
hit = 1;
else
fit = 0;
end
end
end
if (fit && strcmp(op, 'erode'))
out(i, j) = 1;
elseif (hit && strcmp(op, 'dilate'))
out(i, j) = 1;
else
out(i, j) = 0;
end
end
end

Output:

You might also like