0% found this document useful (0 votes)
45 views3 pages

Image Processing Assignment-3

This document contains code for performing 1D and 2D convolution. For 1D convolution, it defines input vectors x and h, calculates the convolution y using a for loop, and plots the results. It then verifies the output using MATLAB's conv command. For 2D convolution, it defines a convolution function that calculates the output matrix B by sliding the kernel k over the input matrix A and performing element-wise multiplication and summation. It then applies the function to sample input and kernel matrices I and k.

Uploaded by

Malavika Mohan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views3 pages

Image Processing Assignment-3

This document contains code for performing 1D and 2D convolution. For 1D convolution, it defines input vectors x and h, calculates the convolution y using a for loop, and plots the results. It then verifies the output using MATLAB's conv command. For 2D convolution, it defines a convolution function that calculates the output matrix B by sliding the kernel k over the input matrix A and performing element-wise multiplication and summation. It then applies the function to sample input and kernel matrices I and k.

Uploaded by

Malavika Mohan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

IMAGE PROCESSING ASSIGNMENT

1.) 1-d convolution


x=[1 9 3 8 6]
h=[3 6 4]
a=length(x)
b=length(h)
n=a+b-1
y=zeros(1,n)
l=1:n
for i=0:n
for j=0:n
if ((i-j+1)>0 && (i-j+1)<=b && (j+1)<=a)
y(i+1)=y(i+1)+x(j+1).*h(i-j+1);
end
end
end
stem(l,y)
xlabel('l')
ylabel('amplitude')
title('1-d convolution')
figure
i=conv(h,x)
stem(l,i)
xlabel('l')
ylabel('amplitude')
title('1-d convolution using conv command')
1-d convolution
80
70
60

amplitude

50
40
30
20
10
0

4
l

1-d convolution using conv command


80
70
60

amplitude

50
40
30
20
10
0

2.)2-d convolution
FUNCTION
function B = convolve(A, k);
[r c] = size(A);
[m n] = size(k);
h = rot90(k, 2);
center = floor((size(h)+1)/2);
left = center(2) - 1;
right = n - center(2);
top = center(1) - 1;
bottom = m - center(1);
Rep = zeros(r + top + bottom, c + left + right);
for x = 1 + top : r + top
for y = 1 + left : c + left
Rep(x,y) = A(x - top, y - left);
end
end
B = zeros(r , c);
for x = 1 : r
for y = 1 : c
for i = 1 : m
for j = 1 : n
q = x - 1;
w = y -1;
B(x, y) = B(x, y) + (Rep(i + q, j + w) * h(i, j));
end
end
end
end

MAIN PROGRAM
clear; clc;
I = [4 4 3 5 4;

4
l

6 6 5 5 2;
5 6 6 6 2;
6 7 5 5 3;
3 5 2 4 4];
k = [0 -1 0;
-1 4 -1;
0 -1 0];
s = convolve(I, k)
b = conv2(I,k,'same')

answer:
s=

6
9
2
9
1

3 -2
3 0
0 2
6 0
8 -6

8
2
6
2
5

9
-3
-3
1
9

6
9
2
9
1

3 -2
3 0
0 2
6 0
8 -6

8
2
6
2
5

9
-3
-3
1
9

b=

You might also like