Tugas Ketiga Pengolahan Citra Digital

4 Operasi Geometri

a.      Operasi Translasi : merupakan pergeseran posisi piksel

Source Code :

Tx = 20;

Ty = 50;

A = imred(‘cameraman.tif’);

[brs kol] = size A(A);

for x = 1 : brs

  for y = 1 : kol

       B(x+Tx,y+Ty) = A(x,y);

  end

end

B = B(1:brs,1:kol);

figure,imshow(uint8(A));

figure,imshow(uint8(B));

 

Hasil Tampilan :

 


b.      Operasi Cropping

Source Code :

I = imread('cameraman.tif');

[brs kol] = size(I);

x1 = 50;

x2 = 200;

y1 = 50;

y2 = 200;

I(1:x1,:) = 0;

I(x2:brs,:) = 0;

I(:,1:y1) = 0;

I(:,y2:kol) = 0;

figure,imshow(I);

 

Hasil Tampilan :


c.       Operasi Flipping

Flipping adalah operasi geometri yang sama dengan pencerminan. Ada  2 macam flipping : horizontal & vertical.

Flipping Vertical adalah pencerminan pada sumbu-X dari citra A menjadi citra B.

Source Code :

I = imread('cameraman.tif');

[brs kol] = size(I);

J = repmat(0,brs,kol);

mirror = floor(kol/2);

for x = 1 : brs-1

for y = 1 : kol-1

 J(x,y) = I((2*mirror)-x, y);

 end

end

figure, imshow(uint8(J));

Hasil Tampilan :

 


 

Flipping Horizontal adalah pencerminan pada sumbu-Y dari citra A menjadi citra B.

Source Code :

I = imread('cameraman.tif');

[brs kol] = size(I);

J = repmat(0,brs,kol);

mirror = floor(brs/2);

for x = 1 : brs-1

 for y = 1 : kol-1

J(x,y) = I(x, (2*mirror)-y);

 end

end

figure, imshow(uint8(J));


Hasil Tampilan :

 



d.      Operasi Rotasi

Source Code :

function J = rotasi(I,T)

m = size(I,1);

n = size(I,2);

if rem(m,2) == 0, Xp = floor((m+1)/2)+1;

else

 Xp = floor((m+1)/2);

end

if rem(n,2) == 0,

 Yp = floor((n+1)/2)+1;

else

 Yp = floor((n+1)/2);

end

X = zeros(m,n);

Y = zeros(m,n);

for x = 1 : m, X(x,1:n) = x;

end

for y = 1 : n, Y(1:m,y) = y;

end

Xa = round(Xp + (X - Xp)*cosd(T) - (Y - Yp)*sind(T));

Ya = round(Yp + (X - Xp)*sind(T) + (Y - Yp)*cosd(T));

r = size(min(Xa(:)) : max(Xa(:)),2);

c = size(min(Ya(:)) : max(Ya(:)),2);

xs = round(abs(r-m)/2);

ys = round(abs(c-n)/2);

J = zeros(r,c);

for x = 1 : m

 for y = 1 : n

 J(Xa(x,y)+xs,Ya(x,y)+ys) = I(x,y);

 End

End

Source code di atas disimpan dengan nama m-file ‘rotasi.m’. Selanjutnya untuk menguji keberhasilan source code di atas, buatlah suatu m-file lagi dan tuliskan source code di bawah ini

clear all; clc;

T = 45;

I = imread('cameraman.tif');

J = rotasi(I,T); imshow(uint8(J),'initialmagnification','fit');

Hasil Tampilan :


e.      Operasi Scalling

Souce Code :

function J = perbesar(I,ShX,ShY)

m = size(I,1);

n = size(I,2);

r = m*ShX;

c = n*ShY;

J = zeros(r,c);

for x = 1 : m

for y = 1 : n

J((x-1)*ShX+1 : x*ShX, (y-1)*ShY+1 : y*ShY) = I(x,y);

end

end   

 

Source code di atas disimpan dengan nama m-file ‘perbesar.m’. Selanjutnya

untuk menguji keberhasilan source code di atas, buatlah suatu m-file lagi dan tuliskan

source code di bawah ini


clear all; clc;

I = imread('cameraman.tif');

ShX = 2;

ShY = 1;

J = perbesar(I,ShX,ShY);

imshow(uint8(J));

 

Hasil Tampilan :

 


     5.     Konvolusi

Konvolusi merupakan operasi yang mendasar dalam pengolahan citra.

*Contoh perintah untuk melakukan konvolusi terhadap matriks/yang berukuran 5 x 5 dan kernel yang berukuran 3 x 3 adalah sebagai berikut. Simpan source code dibawah ini dengan nama convolve.m

Source Code :

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

 

Selanjutnya,

untuk menguji keberhasilan source code di atas, buatlah suatu m-file lagi dan tuliskan

source code di bawah ini simpan dengan nama convolve2.m


Source Code :

clear; clc;

I = [4 4 3 5 4;

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

Hsl = convolve(I, k)

 

Operasi konvolusi yang dilakukan adalah dengan melibatkan zero padding di

dalamnya. Output yang dihasilkan oleh source code di atas adalah sebagai berikut :


Untuk menentukan gambar yang mana yang akan dikenai proses konvolusi maka ketikkan source code dibawah ini dan simpan dengan nama convolve3.m

Source Code :

clear; clc;

I = imread('cameraman.tif');

k = ones(3)/9;

Hsl = convolve(I, k);

imshow(I);

figure, imshow(uint8(Hsl));

 

Hasil Tampilan :

 


     6.     Segmentasi Citra

A.    Operasi Pengambangan

Operasi pengambangan (thresholding) adalah operasi memetakan nilai intensitas piksel ke salah satu dari dua nilai, a1 atau a2, berdasarkan nilai ambang (threshold) T.

Source Code :

F = imread('cameraman.tif');

[r c] = size(F);

T = 128;

for x = 1 : r

 for y = 1 : c

if F(x,y) >= T

 G(x,y) = 255;

 else

 G(x,y) = 0;

 end

 end

end

figure, imshow(F);

figure, imshow(G);

 

Hasil Tampilan :


Tidak ada komentar

Diberdayakan oleh Blogger.