Daftar Coding Octave
Sebelum menjalankan coding simpan dulu gambar di "C" / sesuai instalasi, kemudian buat folder image dan simpan gambar di folder tersebut, jalankan di octave sesuai dengan format gambar, misalnya jpg, png, tif.
Coding octave di bawah bisa lansung di copas dan di jalankan di software octave.
1. Pengenalan Dasar Citra
kode program mengubah citra berwarna ke citra abu-abu
-------------------------------------------------------------------------
img=imread('c:\image\hotma.jpg');
x=uint8(0.2989 * double(img(:,:,1))...
+ 0.5870 *double(img(:,:,2)) + 0.1141 *double(img(:,:,3)));
kode program mengetahui susunan warna pada citra
===========================================
a=imread('c:\image\hotma.jpg');
red =a(:,:,1);
green=a(:,:,2);
blue =a(:,:,3);
subplot(2,2,1);imshow(a),title('empat bola')
subplot(2,2,2);imshow(red),title('empat bola merah')
subplot(2,2,3);imshow(green),title('empat bola hijau')
subplot(2,2,4);imshow(blue),title('empat bola biru')
kode program mengubah citra abu-abu ke citra biner
========================================
img=imread('c:\image\hotmaabu.jpg');
[tinggi,lebar] = size(img);
ambang=150;
biner=zeros(tinggi, lebar);
for baris=1 : tinggi
for kolom=1 : lebar
if img(baris,kolom) >=ambang
biner(baris, kolom) = 0;
else
biner(baris,kolom) =1;
end
end
end
imshow(biner)
mengkonversi citra berwarna ke abu abu dan biner
=======================================
RGB=imread('C:\Image\hotma.jpg');
YCBCR = rgb2ntsc(RGB);
Y=YCBCR(:,:,1); %Ekstraksi matriks Y
Cb=YCBCR(:,:,2); %Ekstraksi matriks Bc
Cr=YCBCR(:,:,3); %Ekstraksi matriks Br
subplot(2,2,1); imshow(RGB);
subplot(2,2,2); imshow(Y);
subplot(2,2,3); imshow(Cb);
subplot(2,2,4); imshow(Cr);
-------------------------------------------------------------------------
img=imread('c:\image\hotma.jpg');
x=uint8(0.2989 * double(img(:,:,1))...
+ 0.5870 *double(img(:,:,2)) + 0.1141 *double(img(:,:,3)));
kode program mengetahui susunan warna pada citra
===========================================
a=imread('c:\image\hotma.jpg');
red =a(:,:,1);
green=a(:,:,2);
blue =a(:,:,3);
subplot(2,2,1);imshow(a),title('empat bola')
subplot(2,2,2);imshow(red),title('empat bola merah')
subplot(2,2,3);imshow(green),title('empat bola hijau')
subplot(2,2,4);imshow(blue),title('empat bola biru')
kode program mengubah citra abu-abu ke citra biner
========================================
img=imread('c:\image\hotmaabu.jpg');
[tinggi,lebar] = size(img);
ambang=150;
biner=zeros(tinggi, lebar);
for baris=1 : tinggi
for kolom=1 : lebar
if img(baris,kolom) >=ambang
biner(baris, kolom) = 0;
else
biner(baris,kolom) =1;
end
end
end
imshow(biner)
mengkonversi citra berwarna ke abu abu dan biner
=======================================
RGB=imread('C:\Image\hotma.jpg');
YCBCR = rgb2ntsc(RGB);
Y=YCBCR(:,:,1); %Ekstraksi matriks Y
Cb=YCBCR(:,:,2); %Ekstraksi matriks Bc
Cr=YCBCR(:,:,3); %Ekstraksi matriks Br
subplot(2,2,1); imshow(RGB);
subplot(2,2,2); imshow(Y);
subplot(2,2,3); imshow(Cb);
subplot(2,2,4); imshow(Cr);
2. Operasi Piksel dan Histogram
program histogram citra keabuan
=============================
img=imread('c:\image\boneka.tif');
[jum_baris,jum_kolom]=size(img);
histogram=zeros(256,1);
for baris=1 : jum_baris
for kolom=1 : jum_kolom
histogram(img(baris,kolom) + 1) = ...
histogram(img(baris,kolom) + 1) + 1;
end
end
horijontal=(0:255) ;
bar(horijontal,histogram);
subplot(1,2,1);imshow(img)
subplot(1,2,2);bar(horijontal,histogram)
program pemotongan aras keabuan (menghilangkan derau)
================================================
function [img] = potong(citra,f1,f2)
img=imread(citra);
[jum_baris,jum_kolom]=size(img);
for baris=1 : jum_baris
for kolom=1 : jum_kolom
if img(baris, kolom ) <=f1
img(baris,kolom)=0;
end
if img(baris,kolom) >=f2
img(baris,kolom)=255;
end
end
end
end %akhir fungsi
h=potong('c:\image\daun.tif',30,140);
imshow(h)
p=imread('c:\image\daun.tif');
subplot(1,2,1);imshow(p)
subplot(1,2,2);imshow(h)
cara kedua program pemotongan aras keabuan (masih menyimpan debug)
=======================================================
img=imread('c:\image\daun.tif');
[jum_baris,jum_kolom]=size(img);
for baris=1 : jum_baris
for kolom= 1 : jum_kolom
citra=img;
if citra(baris,kolom) <=30
citra(baris,kolom)=0;
end
if citra(baris,kolom) >=150
citra(baris,kolom)=255;
end
end
end
=============================
img=imread('c:\image\boneka.tif');
[jum_baris,jum_kolom]=size(img);
histogram=zeros(256,1);
for baris=1 : jum_baris
for kolom=1 : jum_kolom
histogram(img(baris,kolom) + 1) = ...
histogram(img(baris,kolom) + 1) + 1;
end
end
horijontal=(0:255) ;
bar(horijontal,histogram);
subplot(1,2,1);imshow(img)
subplot(1,2,2);bar(horijontal,histogram)
program pemotongan aras keabuan (menghilangkan derau)
================================================
function [img] = potong(citra,f1,f2)
img=imread(citra);
[jum_baris,jum_kolom]=size(img);
for baris=1 : jum_baris
for kolom=1 : jum_kolom
if img(baris, kolom ) <=f1
img(baris,kolom)=0;
end
if img(baris,kolom) >=f2
img(baris,kolom)=255;
end
end
end
end %akhir fungsi
h=potong('c:\image\daun.tif',30,140);
imshow(h)
p=imread('c:\image\daun.tif');
subplot(1,2,1);imshow(p)
subplot(1,2,2);imshow(h)
cara kedua program pemotongan aras keabuan (masih menyimpan debug)
=======================================================
img=imread('c:\image\daun.tif');
[jum_baris,jum_kolom]=size(img);
for baris=1 : jum_baris
for kolom= 1 : jum_kolom
citra=img;
if citra(baris,kolom) <=30
citra(baris,kolom)=0;
end
if citra(baris,kolom) >=150
citra(baris,kolom)=255;
end
end
end
3. Operasi Ketetanggan Piksel
Program filter Batas
==========================
f = imread('c:\image\mobil.tif');
[jum_baris, jum_kolom] = size(f);
g = f;
for baris=2 : jum_baris-1
for kolom=2 : jum_kolom-1
minpiksel = min([f(baris-1, kolom-1) ...
f(baris-1, kolom) f(baris, kolom+1) ...
f(baris, kolom-1) ...
f(baris, kolom+1) f(baris+1, kolom-1) ...
f(baris+1, kolom) f(baris+1, kolom+1)]);
makspiksel = max([f(baris-1, kolom-1) ...
f(baris-1, kolom) f(baris, kolom+1) ...
f(baris, kolom-1) ...
f(baris, kolom+1) f(baris+1, kolom-1) ...
f(baris+1, kolom) f(baris+1, kolom+1)]);
if f(baris, kolom) < minpiksel
g(baris, kolom) = minpiksel;
else
if f(baris, kolom) > makspiksel
g(baris, kolom) = makspiksel;
else
g(baris, kolom) = f(baris, kolom);
end
end
end
end
Program Filter Pererataan
===============================
f = imread('c:\image\boneka2.tif');
[jum_baris, jum_kolom] = size(f);
f2=double(f);
for baris=2 : jum_baris - 1
for kolom=2 : jum_kolom - 1
jumlah= f2 (baris - 1, kolom - 1) + ...
f2(baris - 1 , kolom) + ...
f2(baris - 1, kolom - 1) + ...
f2(baris, kolom - 1) + ...
f2(baris, kolom) + ...
f2(baris, kolom + 1) + ...
f2(baris +1, kolom - 1) + ...
f2(baris+1, kolom) + ...
f2(baris+1, kolom + 1) ;
g(baris, kolom) = uint8 ( 1/9 * jumlah);
end
end
program filter median
=======================
f = imread('c:\image\mobil.tif');
[jum_baris, jum_kolom] = size(f);
for baris=2 : jum_baris - 1
for kolom=2 : jum_kolom - 1
data= [f(baris-1, kolom-1) ...
f(baris-1, kolom) ...
f(baris-1, kolom+1) ...
f(baris, kolom-1) ...
f(baris, kolom) ...
f(baris, kolom+1) ...
f(baris+1, kolom-1) ...
f(baris+1, kolom) ...
f(baris+1, kolom-1)];
%urutkan
for i=1 : 8
for j=i+1 : 9
if data(i) > data(j)
temp =data(i);
data(i)=data(j);
data(j)=temp;
end
end
end
%ambil nilai median
g(baris,kolom)=data(5);
end
end
subplot(1,2,1);imshow(f)
subplot(1,2,2);imshow(g)
==========================
f = imread('c:\image\mobil.tif');
[jum_baris, jum_kolom] = size(f);
g = f;
for baris=2 : jum_baris-1
for kolom=2 : jum_kolom-1
minpiksel = min([f(baris-1, kolom-1) ...
f(baris-1, kolom) f(baris, kolom+1) ...
f(baris, kolom-1) ...
f(baris, kolom+1) f(baris+1, kolom-1) ...
f(baris+1, kolom) f(baris+1, kolom+1)]);
makspiksel = max([f(baris-1, kolom-1) ...
f(baris-1, kolom) f(baris, kolom+1) ...
f(baris, kolom-1) ...
f(baris, kolom+1) f(baris+1, kolom-1) ...
f(baris+1, kolom) f(baris+1, kolom+1)]);
if f(baris, kolom) < minpiksel
g(baris, kolom) = minpiksel;
else
if f(baris, kolom) > makspiksel
g(baris, kolom) = makspiksel;
else
g(baris, kolom) = f(baris, kolom);
end
end
end
end
Program Filter Pererataan
===============================
f = imread('c:\image\boneka2.tif');
[jum_baris, jum_kolom] = size(f);
f2=double(f);
for baris=2 : jum_baris - 1
for kolom=2 : jum_kolom - 1
jumlah= f2 (baris - 1, kolom - 1) + ...
f2(baris - 1 , kolom) + ...
f2(baris - 1, kolom - 1) + ...
f2(baris, kolom - 1) + ...
f2(baris, kolom) + ...
f2(baris, kolom + 1) + ...
f2(baris +1, kolom - 1) + ...
f2(baris+1, kolom) + ...
f2(baris+1, kolom + 1) ;
g(baris, kolom) = uint8 ( 1/9 * jumlah);
end
end
program filter median
=======================
f = imread('c:\image\mobil.tif');
[jum_baris, jum_kolom] = size(f);
for baris=2 : jum_baris - 1
for kolom=2 : jum_kolom - 1
data= [f(baris-1, kolom-1) ...
f(baris-1, kolom) ...
f(baris-1, kolom+1) ...
f(baris, kolom-1) ...
f(baris, kolom) ...
f(baris, kolom+1) ...
f(baris+1, kolom-1) ...
f(baris+1, kolom) ...
f(baris+1, kolom-1)];
%urutkan
for i=1 : 8
for j=i+1 : 9
if data(i) > data(j)
temp =data(i);
data(i)=data(j);
data(j)=temp;
end
end
end
%ambil nilai median
g(baris,kolom)=data(5);
end
end
subplot(1,2,1);imshow(f)
subplot(1,2,2);imshow(g)
4. Operasi Geometrik
program penggeseran citra
---------------------------------------------
f=imread('c:\image\hotmaabu.jpg');
[jum_baris,jum_kolom]=size(f);
sx=20; %penggeseran arah horizontal
sy=60; %penggeseran arah vertikal
f2=double(f);
g=zeros(size(f2));
for baris=1 : jum_baris
for kolom=1: jum_kolom
xlama=baris - sx;
ylama=kolom - sy;
if (xlama >=1) && (xlama<=jum_baris) && ...
(ylama >=1) && (ylama<=jum_kolom)
g(baris,kolom)=f2(xlama, ylama);
else
g(baris,kolom)=0;
end
end
end
h=uint8(g);
subplot(1,2,1);imshow(f)
subplot(1,2,2);imshow(h)
program memutar citra
============================
f=imread('c:\image\hotmaabu.jpg');
[jum_baris, jum_kolom]=size(f);
sudut=10; %sudut pemutaran
rad=pi * sudut/180;
cosa=cos(rad);
sina=sin(rad);
f2=f;
for y=1 : jum_baris
for x=1 :jum_kolom
x2=round(x * cosa + y * sina);
y2=round(y * cosa - x * sina);
if (x2>= 1) && (x2<=jum_kolom) && ...
(y2 >=1) && (y2<=jum_baris)
g(y,x)=f2(y2,x2);
else
g(y,x)=0;
end
end
end
subplot(1,2,1);imshow(f)
subplot(1,2,2);imshow(g)
Program memperbesar Citra dengan fungsi
----------------------------------------------------
function g = perbesar(citra, sy, sx)
f = imread(citra);
[baris, kolom] = size(f);
baris_baru = baris * sy;
kolom_baru = kolom * sx;
f2 = double(f);
for y=1 : baris_baru
y2 = ((y-1) / sy) + 1;
for x=1 : kolom_baru
x2 = ((x-1) / sx) + 1;
g(y, x) = f(floor(y2), floor(x2));
end
end
h = uint8(g);
end %akhir fungsi
u=imread('c:\image\lena128.tif');
z=perbesar('c:\image\lena128.tif', 0.25, 0.25);
subplot(1,2,1);imshow(u),title('citra masukan')
subplot(1,2,2);imshow(z),title('citra perbesar')
program memperbesar citra cara tanpa fungsi
=========================================
f=imread('c:\image\hotmaabu.jpg');
[baris, kolom] = size(f);
sy=0.5;
sx=2;
baris_baru = baris * sy;
kolom_baru = kolom * sx;
f2=f;
for y=1 : baris_baru
y2 = ((y-1) / sy) + 1;
for x=1 : kolom_baru
x2 = ((x-1) / sx) + 1;
g(y, x) = f(floor(y2), floor(x2));
end
end
subplot(1,2,1);imshow(f),title('citra masukan')
subplot(1,2,2);imshow(g),title('citra perbesar')
memperkecil citra
------------------------------
z=perbesar('c:\image\lena128.tif', 0.5, 0.5);
program pencerminan citra secara horizontal dengan fungsi
==============================================
function g = cerminh(f)
[tinggi, lebar] = size(f);
for y=1 : tinggi
for x=1 : lebar
x2 = lebar - x + 1;
y2 = y;
g(y, x) = f(y2, x2);
end
end
h = uint8(g);
end %akhir fungsi
f=imread('c:\image\hotmaabu.jpg');
h=cerminh(f);
subplot(1,2,1);imshow(f),title('citra masukan')
subplot(1,2,2);imshow(h),title('citra cermin horizontal')
program pencerminan citra secara horizontal dengan fungsi cara kedua
====================================================
function g = cerminh(f)
[jum_baris, jum_kolom] = size(f);
for baris=1 : jum_baris
for kolom=1 : jum_kolom
x2 = jum_kolom - kolom + 1;
y2 = baris;
g(baris,kolom) = f(y2, x2);
end
end
%g = uint8(g);
end %akhir fungsi
f=imread('c:\image\hotmaabu.jpg');
g=cerminh(f);
subplot(1,2,1);imshow(f),title('citra masukan')
subplot(1,2,2);imshow(g),title('citra cermin horizontal')
program pencerminan citra secara horizontal tanpa fungsi
==============================================
f=imread('c:\image\hotmaabu.jpg');
[jum_baris, jum_kolom] = size(f);
for baris=1 : jum_baris
for kolom=1 : jum_kolom
x2 = jum_kolom - kolom + 1;
y2 = baris;
g(baris,kolom) = f(y2, x2);
end
end
subplot(1,2,1);imshow(f),title('citra masukan')
subplot(1,2,2);imshow(g),title('citra cermin horizontal')
Program pencerminan citra secara Vertikal dengan fungsi
===========================================
function g = cerminv(f)
% Masukan: f = Citra berskala keabuan
[tinggi, lebar] = size(f);
for y=1 : tinggi
for x=1 : lebar
x2 = x;
y2 = tinggi - y + 1;
g(y, x) = f(y2, x2);
end
end
h = uint8(g);
end %akhir fungsi
f=imread('c:\image\hotmaabu.jpg');
h=cerminv(f);
subplot(1,2,1);imshow(f),title('citra masukan')
subplot(1,2,2);imshow(h),title('citra cermin vertikal')
Program pencerminan citra secara Vertikal dengan fungsi cara kedua
===================================================
function g = cerminv(f)
% Masukan: f = Citra berskala keabuan
[jum_baris, jum_kolom] = size(f);
for baris=1 : jum_baris
for kolom=1 : jum_kolom
y2 = jum_baris - baris + 1;
x2 = kolom;
g(baris,kolom) = f(y2, x2);
end
end
g = uint8(g);
end %akhir fungsi
f=imread('c:\image\hotmaabu.jpg');
g=cerminv(f);
subplot(1,2,1);imshow(f),title('citra masukan')
subplot(1,2,2);imshow(g),title('citra cermin vertikal')
Program pencerminan citra secara Vertikal tanpa fungsi
==========================================
f=imread('c:\image\hotmaabu.jpg');
[jum_baris, jum_kolom] = size(f);
for baris=1 : jum_baris
for kolom=1 : jum_kolom
y2 = jum_baris - baris + 1;
x2 = kolom;
g(baris,kolom) = f(y2, x2);
end
end
subplot(1,2,1);imshow(f),title('citra masukan')
subplot(1,2,2);imshow(g),title('citra cermin vertikal')
---------------------------------------------
f=imread('c:\image\hotmaabu.jpg');
[jum_baris,jum_kolom]=size(f);
sx=20; %penggeseran arah horizontal
sy=60; %penggeseran arah vertikal
f2=double(f);
g=zeros(size(f2));
for baris=1 : jum_baris
for kolom=1: jum_kolom
xlama=baris - sx;
ylama=kolom - sy;
if (xlama >=1) && (xlama<=jum_baris) && ...
(ylama >=1) && (ylama<=jum_kolom)
g(baris,kolom)=f2(xlama, ylama);
else
g(baris,kolom)=0;
end
end
end
h=uint8(g);
subplot(1,2,1);imshow(f)
subplot(1,2,2);imshow(h)
program memutar citra
============================
f=imread('c:\image\hotmaabu.jpg');
[jum_baris, jum_kolom]=size(f);
sudut=10; %sudut pemutaran
rad=pi * sudut/180;
cosa=cos(rad);
sina=sin(rad);
f2=f;
for y=1 : jum_baris
for x=1 :jum_kolom
x2=round(x * cosa + y * sina);
y2=round(y * cosa - x * sina);
if (x2>= 1) && (x2<=jum_kolom) && ...
(y2 >=1) && (y2<=jum_baris)
g(y,x)=f2(y2,x2);
else
g(y,x)=0;
end
end
end
subplot(1,2,1);imshow(f)
subplot(1,2,2);imshow(g)
Program memperbesar Citra dengan fungsi
----------------------------------------------------
function g = perbesar(citra, sy, sx)
f = imread(citra);
[baris, kolom] = size(f);
baris_baru = baris * sy;
kolom_baru = kolom * sx;
f2 = double(f);
for y=1 : baris_baru
y2 = ((y-1) / sy) + 1;
for x=1 : kolom_baru
x2 = ((x-1) / sx) + 1;
g(y, x) = f(floor(y2), floor(x2));
end
end
h = uint8(g);
end %akhir fungsi
u=imread('c:\image\lena128.tif');
z=perbesar('c:\image\lena128.tif', 0.25, 0.25);
subplot(1,2,1);imshow(u),title('citra masukan')
subplot(1,2,2);imshow(z),title('citra perbesar')
program memperbesar citra cara tanpa fungsi
=========================================
f=imread('c:\image\hotmaabu.jpg');
[baris, kolom] = size(f);
sy=0.5;
sx=2;
baris_baru = baris * sy;
kolom_baru = kolom * sx;
f2=f;
for y=1 : baris_baru
y2 = ((y-1) / sy) + 1;
for x=1 : kolom_baru
x2 = ((x-1) / sx) + 1;
g(y, x) = f(floor(y2), floor(x2));
end
end
subplot(1,2,1);imshow(f),title('citra masukan')
subplot(1,2,2);imshow(g),title('citra perbesar')
memperkecil citra
------------------------------
z=perbesar('c:\image\lena128.tif', 0.5, 0.5);
program pencerminan citra secara horizontal dengan fungsi
==============================================
function g = cerminh(f)
[tinggi, lebar] = size(f);
for y=1 : tinggi
for x=1 : lebar
x2 = lebar - x + 1;
y2 = y;
g(y, x) = f(y2, x2);
end
end
h = uint8(g);
end %akhir fungsi
f=imread('c:\image\hotmaabu.jpg');
h=cerminh(f);
subplot(1,2,1);imshow(f),title('citra masukan')
subplot(1,2,2);imshow(h),title('citra cermin horizontal')
program pencerminan citra secara horizontal dengan fungsi cara kedua
====================================================
function g = cerminh(f)
[jum_baris, jum_kolom] = size(f);
for baris=1 : jum_baris
for kolom=1 : jum_kolom
x2 = jum_kolom - kolom + 1;
y2 = baris;
g(baris,kolom) = f(y2, x2);
end
end
%g = uint8(g);
end %akhir fungsi
f=imread('c:\image\hotmaabu.jpg');
g=cerminh(f);
subplot(1,2,1);imshow(f),title('citra masukan')
subplot(1,2,2);imshow(g),title('citra cermin horizontal')
program pencerminan citra secara horizontal tanpa fungsi
==============================================
f=imread('c:\image\hotmaabu.jpg');
[jum_baris, jum_kolom] = size(f);
for baris=1 : jum_baris
for kolom=1 : jum_kolom
x2 = jum_kolom - kolom + 1;
y2 = baris;
g(baris,kolom) = f(y2, x2);
end
end
subplot(1,2,1);imshow(f),title('citra masukan')
subplot(1,2,2);imshow(g),title('citra cermin horizontal')
Program pencerminan citra secara Vertikal dengan fungsi
===========================================
function g = cerminv(f)
% Masukan: f = Citra berskala keabuan
[tinggi, lebar] = size(f);
for y=1 : tinggi
for x=1 : lebar
x2 = x;
y2 = tinggi - y + 1;
g(y, x) = f(y2, x2);
end
end
h = uint8(g);
end %akhir fungsi
f=imread('c:\image\hotmaabu.jpg');
h=cerminv(f);
subplot(1,2,1);imshow(f),title('citra masukan')
subplot(1,2,2);imshow(h),title('citra cermin vertikal')
Program pencerminan citra secara Vertikal dengan fungsi cara kedua
===================================================
function g = cerminv(f)
% Masukan: f = Citra berskala keabuan
[jum_baris, jum_kolom] = size(f);
for baris=1 : jum_baris
for kolom=1 : jum_kolom
y2 = jum_baris - baris + 1;
x2 = kolom;
g(baris,kolom) = f(y2, x2);
end
end
g = uint8(g);
end %akhir fungsi
f=imread('c:\image\hotmaabu.jpg');
g=cerminv(f);
subplot(1,2,1);imshow(f),title('citra masukan')
subplot(1,2,2);imshow(g),title('citra cermin vertikal')
Program pencerminan citra secara Vertikal tanpa fungsi
==========================================
f=imread('c:\image\hotmaabu.jpg');
[jum_baris, jum_kolom] = size(f);
for baris=1 : jum_baris
for kolom=1 : jum_kolom
y2 = jum_baris - baris + 1;
x2 = kolom;
g(baris,kolom) = f(y2, x2);
end
end
subplot(1,2,1);imshow(f),title('citra masukan')
subplot(1,2,2);imshow(g),title('citra cermin vertikal')
5. Pengolahan Citra Di Kawasan Frekuensi
program ripple tanpa fungsi
====================
f=imread('c:\image\hotmaabu.jpg'); aman
[tinggi,lebar]=size(f);
ax= 10; % amplitudo gelombang sinus pada kolom
ay= 10; % amplitudo gelombang sinus pada baris
tx=250; % periode gelombang sinus pada kolom
ty=250; %periode gelombang sinus pada baris
for y=1 : tinggi 100
for x=1 : lebar 100
x2=x + ax * sin(2 * pi * y / tx);
y2=y + ay * sin(2 * pi * x / ty );
if (x2>=1) && (x2<=lebar) && ...
(y2 >=1) && (y2<=tinggi)
%Lakukan interpolasi Bilinier
p=floor(y2); 56.8
q=floor(x2); 45.7
a=y2- p;
b=x2-q;
if (floor(x2) ==lebar) || ...
(floor (y2) == tinggi)
g(y,x)=f(floor(y2), floor(x2));
else
intensitas= (1 -a ) * ((1-b) * f(p,q) + ...
b * f(p, q + 1)) + ...
a * ((1 - b) * f(p +1, q) + ...
b * f(p+1, q + 1));
g(y,x)= intensitas;
end
else
g(y,x)=0;
end
end
end
g=uint8(g);
subplot(1,2,1);imshow(f)
subplot(1,2,2);imshow(g)
program efek Twirl tanpa fungsi
====================================
f=imread('c:\image\hotmaabu.jpg');
[baris,kolom] =size(f);
xc=round(kolom / 2);
yc=round(baris / 2);
alpha= 60 * pi /180;
rmaks= 0.5 *sqrt (xc^2 + yc^2); % 1/2 diagonal citra
for y=1 : baris
for x= 1 : kolom
r=sqrt((x - xc)^2 + (y -yc)^2);
beta=atan2(y- yc, x-xc) + ...
alpha * (rmaks - r ) / rmaks;
x2=xc + r * cos(beta);
y2=yc + r * sin(beta);
if (x2 >= 1) && (x2 <=kolom) && ...
(y2 >=1) && (y2 <=baris)
%lakukan interpolasi bilinear
p=floor(y2);
q=floor(x2);
a=y2 - p;
b=x2 - q;
if (floor (x2) == kolom) || ...
(floor (y2) == baris)
g(y,x)=f(floor(y2), floor(x2));
else
intensitas = (1-a) * ((1-b) * f (p,q) + ...
b * f(p, q+1)) + ...
a * ((1 - b) * f(p +1, q) + ...
b * f(p + 1, q+1));
g(y,x)=intensitas;
end
else
g(y,x)=0;
end
end
end
g=uint8(g);
subplot(1,2,1);imshow(f)
subplot(1,2,2);imshow(g)
Program efek ripple dengan fungsi
=====================
function g=ripple(f,ax,ay,tx,ty)
[tinggi,lebar]=size(f);
for y=1 : tinggi
for x=1 : lebar
x2=x + ax * sin(2 * pi * y / tx);
y2=y + ay * sin(2 * pi * x / ty );
if (x2>=1) && (x2<=lebar) && ...
(y2 >=1) && (y2<=tinggi)
%Lakukan interpolasi Bilinier
p=floor(y2);
q=floor(x2);
a=y2- p;
b=x2-q;
if (floor(x2) ==lebar) || ...
(floor (y2) == tinggi)
g(y,x)=f(floor(y2), floor(x2));
else
intensitas= (1 -a ) * ((1-b) * f(p,q) + ...
b * f(p, q + 1)) + ...
a * ((1 - b) * f(p +1, q) + ...
b * f(p+1, q + 1));
g(y,x)= intensitas;
end
else
g(y,x)=0;
end
end
end
g=uint8(g);
end %akhir fungsi
f=imread('c:\image\hotmaabu.jpg');
g=ripple(f,10,15,120,250);
subplot(1,2,1);imshow(f)
subplot(1,2,2);imshow(g)
program efek Twirl
======================
function g=twirl(f)
[tinggi,lebar]=size(f);
xc=round(lebar / 2);
yc=round(tinggi / 2);
alpha= 60 * pi /180;
rmaks= 0.5 *sqrt (xc^2 + yc^2); % 1/2 diagonal citra
for y=1 : tinggi
for x= 1 : lebar
r=sqrt((x - xc)^2 + (y -yc)^2);
beta=atan2(y- yc, x-xc) + ...
alpha * (rmaks - r ) / rmaks;
x2=xc + r * cos(beta);
y2=yc + r * sin(beta);
if (x2 >= 1) && (x2 <=lebar) && ...
(y2 >=1) && (y2 <=tinggi)
%lakukan interpolasi bilinear
p=floor(y2);
q=floor(x2);
a=y2 - p;
b=x2 - q;
if (floor (x2) == lebar) || ...
(floor (y2) == tinggi)
g(y,x)=f(floor(y2), floor(x2));
else
intensitas = (1-a) * ((1-b) * f (p,q) + ...
b * f(p, q+1)) + ...
a * ((1 - b) * f(p +1, q) + ...
b * f(p + 1, q+1));
g(y,x)=intensitas;
end
else
g(y,x)=0;
end
end
end
g=uint8(g);
end % akhir fungsi
f=imread('c:\image\hotmaabu.jpg');
g=twirl(f);
subplot(1,2,1);imshow(f)
subplot(1,2,2);imshow(g)
Program efek ripple fungsi
=====================
f=imread('c:\image\hotma.jpg');
[tinggi,lebar]=size(f);
xc=round(lebar / 2);
yc=round(tinggi / 2);
alpha= 30 * pi /180;
rmaks= 0.25 *sqrt (xc^2 + yc^2); % 1/2 diagonal citra
for y=1 : tinggi
for x= 1 : lebar
r=sqrt((x - xc)^2 + (y -yc)^2);
beta=atan2(y- yc, x-xc) + ...
alpha * (rmaks - r ) / rmaks;
x2=xc + r * cos(beta);
y2=yc + r * sin(beta);
if (x2 >= 1) && (x2 <=lebar) && ...
(y2 >=1) && (y2 <=tinggi)
%lakukan interpolasi bilinear
p=floor(y2);
q=floor(x2);
a=y2 - p;
b=x2 - q;
if (floor (x2) == lebar) || ...
(floor (y2) == tinggi)
g(y,x)=f(floor(y2), floor(x2));
else
intensitas = (1-a) * ((1-b) * f (p,q) + ...
b * f(p, q+1)) + ...
a * ((1 - b) * f(p +1, q) + ...
b * f(p + 1, q+1));
g(y,x)=intensitas;
end
else
g(y,x)=0;
end
end
end
g=uint8(g);
subplot(1,2,1);imshow(f)
subplot(1,2,2);imshow(g)
program fft atau mengetahui frekuensi gelombang pada citra
------------------------------------------------------------------------------------
x=imread('c:\image\bunga.tif');
y=fft(x);
subplot(1,2,1);imshow(x)
subplot(1,2,2);imshow(y)
program fft2 atau mengetahui frekuensi gelombang pada citra
------------------------------------------------------------------------------------
x=imread('c:\image\bunga.tif');
y=fft2(x);
subplot(1,2,1);imshow(x)
subplot(1,2,2);imshow(y)
program absolut untuk mengetahui spektrum cahaya pada citra
------------------------------------------------------------------------------------
x=imread('c:\image\pantai.png');
s2=log(1 + abs (x));
subplot(1,2,1);imshow(x)
subplot(1,2,2);imshow(abs(s2),[])
====================
f=imread('c:\image\hotmaabu.jpg'); aman
[tinggi,lebar]=size(f);
ax= 10; % amplitudo gelombang sinus pada kolom
ay= 10; % amplitudo gelombang sinus pada baris
tx=250; % periode gelombang sinus pada kolom
ty=250; %periode gelombang sinus pada baris
for y=1 : tinggi 100
for x=1 : lebar 100
x2=x + ax * sin(2 * pi * y / tx);
y2=y + ay * sin(2 * pi * x / ty );
if (x2>=1) && (x2<=lebar) && ...
(y2 >=1) && (y2<=tinggi)
%Lakukan interpolasi Bilinier
p=floor(y2); 56.8
q=floor(x2); 45.7
a=y2- p;
b=x2-q;
if (floor(x2) ==lebar) || ...
(floor (y2) == tinggi)
g(y,x)=f(floor(y2), floor(x2));
else
intensitas= (1 -a ) * ((1-b) * f(p,q) + ...
b * f(p, q + 1)) + ...
a * ((1 - b) * f(p +1, q) + ...
b * f(p+1, q + 1));
g(y,x)= intensitas;
end
else
g(y,x)=0;
end
end
end
g=uint8(g);
subplot(1,2,1);imshow(f)
subplot(1,2,2);imshow(g)
program efek Twirl tanpa fungsi
====================================
f=imread('c:\image\hotmaabu.jpg');
[baris,kolom] =size(f);
xc=round(kolom / 2);
yc=round(baris / 2);
alpha= 60 * pi /180;
rmaks= 0.5 *sqrt (xc^2 + yc^2); % 1/2 diagonal citra
for y=1 : baris
for x= 1 : kolom
r=sqrt((x - xc)^2 + (y -yc)^2);
beta=atan2(y- yc, x-xc) + ...
alpha * (rmaks - r ) / rmaks;
x2=xc + r * cos(beta);
y2=yc + r * sin(beta);
if (x2 >= 1) && (x2 <=kolom) && ...
(y2 >=1) && (y2 <=baris)
%lakukan interpolasi bilinear
p=floor(y2);
q=floor(x2);
a=y2 - p;
b=x2 - q;
if (floor (x2) == kolom) || ...
(floor (y2) == baris)
g(y,x)=f(floor(y2), floor(x2));
else
intensitas = (1-a) * ((1-b) * f (p,q) + ...
b * f(p, q+1)) + ...
a * ((1 - b) * f(p +1, q) + ...
b * f(p + 1, q+1));
g(y,x)=intensitas;
end
else
g(y,x)=0;
end
end
end
g=uint8(g);
subplot(1,2,1);imshow(f)
subplot(1,2,2);imshow(g)
Program efek ripple dengan fungsi
=====================
function g=ripple(f,ax,ay,tx,ty)
[tinggi,lebar]=size(f);
for y=1 : tinggi
for x=1 : lebar
x2=x + ax * sin(2 * pi * y / tx);
y2=y + ay * sin(2 * pi * x / ty );
if (x2>=1) && (x2<=lebar) && ...
(y2 >=1) && (y2<=tinggi)
%Lakukan interpolasi Bilinier
p=floor(y2);
q=floor(x2);
a=y2- p;
b=x2-q;
if (floor(x2) ==lebar) || ...
(floor (y2) == tinggi)
g(y,x)=f(floor(y2), floor(x2));
else
intensitas= (1 -a ) * ((1-b) * f(p,q) + ...
b * f(p, q + 1)) + ...
a * ((1 - b) * f(p +1, q) + ...
b * f(p+1, q + 1));
g(y,x)= intensitas;
end
else
g(y,x)=0;
end
end
end
g=uint8(g);
end %akhir fungsi
f=imread('c:\image\hotmaabu.jpg');
g=ripple(f,10,15,120,250);
subplot(1,2,1);imshow(f)
subplot(1,2,2);imshow(g)
program efek Twirl
======================
function g=twirl(f)
[tinggi,lebar]=size(f);
xc=round(lebar / 2);
yc=round(tinggi / 2);
alpha= 60 * pi /180;
rmaks= 0.5 *sqrt (xc^2 + yc^2); % 1/2 diagonal citra
for y=1 : tinggi
for x= 1 : lebar
r=sqrt((x - xc)^2 + (y -yc)^2);
beta=atan2(y- yc, x-xc) + ...
alpha * (rmaks - r ) / rmaks;
x2=xc + r * cos(beta);
y2=yc + r * sin(beta);
if (x2 >= 1) && (x2 <=lebar) && ...
(y2 >=1) && (y2 <=tinggi)
%lakukan interpolasi bilinear
p=floor(y2);
q=floor(x2);
a=y2 - p;
b=x2 - q;
if (floor (x2) == lebar) || ...
(floor (y2) == tinggi)
g(y,x)=f(floor(y2), floor(x2));
else
intensitas = (1-a) * ((1-b) * f (p,q) + ...
b * f(p, q+1)) + ...
a * ((1 - b) * f(p +1, q) + ...
b * f(p + 1, q+1));
g(y,x)=intensitas;
end
else
g(y,x)=0;
end
end
end
g=uint8(g);
end % akhir fungsi
f=imread('c:\image\hotmaabu.jpg');
g=twirl(f);
subplot(1,2,1);imshow(f)
subplot(1,2,2);imshow(g)
Program efek ripple fungsi
=====================
f=imread('c:\image\hotma.jpg');
[tinggi,lebar]=size(f);
xc=round(lebar / 2);
yc=round(tinggi / 2);
alpha= 30 * pi /180;
rmaks= 0.25 *sqrt (xc^2 + yc^2); % 1/2 diagonal citra
for y=1 : tinggi
for x= 1 : lebar
r=sqrt((x - xc)^2 + (y -yc)^2);
beta=atan2(y- yc, x-xc) + ...
alpha * (rmaks - r ) / rmaks;
x2=xc + r * cos(beta);
y2=yc + r * sin(beta);
if (x2 >= 1) && (x2 <=lebar) && ...
(y2 >=1) && (y2 <=tinggi)
%lakukan interpolasi bilinear
p=floor(y2);
q=floor(x2);
a=y2 - p;
b=x2 - q;
if (floor (x2) == lebar) || ...
(floor (y2) == tinggi)
g(y,x)=f(floor(y2), floor(x2));
else
intensitas = (1-a) * ((1-b) * f (p,q) + ...
b * f(p, q+1)) + ...
a * ((1 - b) * f(p +1, q) + ...
b * f(p + 1, q+1));
g(y,x)=intensitas;
end
else
g(y,x)=0;
end
end
end
g=uint8(g);
subplot(1,2,1);imshow(f)
subplot(1,2,2);imshow(g)
program fft atau mengetahui frekuensi gelombang pada citra
------------------------------------------------------------------------------------
x=imread('c:\image\bunga.tif');
y=fft(x);
subplot(1,2,1);imshow(x)
subplot(1,2,2);imshow(y)
program fft2 atau mengetahui frekuensi gelombang pada citra
------------------------------------------------------------------------------------
x=imread('c:\image\bunga.tif');
y=fft2(x);
subplot(1,2,1);imshow(x)
subplot(1,2,2);imshow(y)
program absolut untuk mengetahui spektrum cahaya pada citra
------------------------------------------------------------------------------------
x=imread('c:\image\pantai.png');
s2=log(1 + abs (x));
subplot(1,2,1);imshow(x)
subplot(1,2,2);imshow(abs(s2),[])
6. Operasi Pada Citra Biner
Program mencari tepi biner objek dengan fungsi
===========================================
function [g] = tepibiner(f)
% tepi biner Berguna untuk mendapatkan tepi objek
% pada citra biner
[jum_baris, jum_kolom] = size(f);
g = zeros(jum_baris, jum_kolom);
for q = 2 : jum_baris - 1
for p = 2 : jum_kolom - 1
p0 = f(q, p+1);
p1 = f(q-1, p+1);
p2 = f(q-1, p);
p3 = f(q-1, p-1);
p4 = f(q, p-1);
p5 = f(q+1, p-1);
p6 = f(q+1, p);
p7 = f(q+1, p+1);
sigma = p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7;
if sigma == 8
g(q, p) = 0;
else
g(q, p) = f(q, p);
end
end
end
end % akhir fungsi
u=imread('c:\image\hotmabin.jpg');
g=tepibiner(u);imshow(g)
program mencari tepi biner objek tanpa fungsi
===========================================
f=imread('c:\image\daun_bin.tif');
[jum_baris, jum_kolom] = size(f);
g=f;
g = zeros(jum_baris, jum_kolom);
for q = 2 : jum_baris - 1
for p = 2 : jum_kolom - 1
p0 = f(q, p+1);
p1 = f(q-1, p+1);
p2 = f(q-1, p);
p3 = f(q-1, p-1);
p4 = f(q, p-1);
p5 = f(q+1, p-1);
p6 = f(q+1, p);
p7 = f(q+1, p+1);
sigma = p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7;
if sigma == 8
g(q, p) = 0;
else
g(q, p) = f(q, p);
end
end
end
subplot(1,2,1);imshow(f)
subplot(1,2,2);imshow(g)
function hasil=perim1(bw)
u=inbound_tracing(bw);
hasil=length(u) -1;
end %akhir fungsi
img=imread('c:\image\daun_bin.tif');
perim1(img)
program perimeter/keliling tanpa inbound_tracing
=========================================
img=imread('c:\image\daun_bin.tif');
perimeter=length(img) -1
program mencari luas objek citra biner denga fungsi
===========================================
function hasil = luas(bw)
% Untuk menghitung luas citra bw (citra biner)
[tinggi, lebar] = size(bw);
hasil = 0;
for p = 1 : tinggi
for q = 1 : lebar
if bw(p, q) == 1
hasil = hasil + 1;
end
end
end
end % akhir fungsi
Daun = imread('c:\image\daun_bin.tif');
luas(Daun)
program mencari luas objek citra biner tanpa fungsi
==========================================
x = imread('c:\image\daun_bin.tif');
[tinggi, lebar] = size(x);
hasil = 0;
for p = 1 : tinggi
for q = 1 : lebar
if x(p, q) == 1
hasil = hasil + 1;
end
end
end
luas_citra_daun_bin=hasil
output
luas_citra_daun_bin = 31862
program diameter objek biner dengan fungsi (masih debug)
================================================
function [diameter, x1, y1, x2, y2] = peroleh_diameter(BW)
U = get_contour(BW);
n = length(U);
jarak_maks = 0;
piksel1 = 0;
piksel2 = 0;
for p=1 : n-1
for q=p+1 : n
jarak = sqrt((U(p,1)-U(q,1)) ^ 2 + (U(p,2)-U(q,2)) ^ 2);
if jarak > jarak_maks
jarak_maks = jarak;
piksel1 = p;
piksel2 = q;
end
end
end
y1 = U(piksel1, 1);
x1 = U(piksel1, 2);
y2 = U(piksel2, 1);
x2 = U(piksel2, 2);
diameter = jarak_maks;
end %akhir fungsi
Daun = imread('c:\image\daun_bin.tif');
[d,x1,y1,x2,y2] = peroleh_diameter(Daun);
d
program diameter objek biner tanpa fungsi
==============================
x= imread('c:\image\daun_bin.tif');
n = length(x);
jarak_maks = 0;
piksel1 = 0;
piksel2 = 0;
for p=1 : n-1
for q=p+1 : n
jarak = sqrt((x(p,1)-x(q,1)) ^ 2 + (x(p,2)-x(q,2)) ^ 2);
if jarak > jarak_maks
jarak_maks = jarak;
piksel1 = p;
piksel2 = q;
end
end
end
diameter = jarak_maks
program pusat massa dan fitur menggunakan pusat massa dengan fungsi
==========================================================
function [pusat_x, pusat_y] = centroid(BW)
% CENTROID Untuk memperoleh pusat massa sebuah objek
% yang terletak pada citra biner BW
[tinggi, lebar] = size(BW);
pusat_x = 0;
pusat_y = 0;
luas = 0;
for q = 1 : tinggi
for p = 1 : lebar
if BW(q, p) == 1
luas = luas + 1;
pusat_x = pusat_x + p;
pusat_y = pusat_y + q;
end
end
end
pusat_x = pusat_x / luas;
pusat_y = pusat_y / luas;
end %akhir fungsi
Daun = imread('C:\Image\daun_bin.tif');
[x, y] = centroid(Daun); imshow(Daun);
[panjang, lebar] = size(Daun);
line([0 lebar], [round(y) round(y)],’Color’,’b’)
line([round(x) round(x)], [0 panjang],’Color’,’b’)
program pusat massa dan fitur menggunakan pusat massa tanpa fungsi
==========================================================
objek = imread('C:\Image\daun_bin.tif');
[tinggi, lebar] = size(objek);
pusat_x = 0;
pusat_y = 0;
luas = 0;
for q = 1 : tinggi
for p = 1 : lebar
if objek(q, p) ==1
luas = luas + 1;
pusat_x = pusat_x + p;
pusat_y = pusat_y + q;
end
end
end
pusat_x = pusat_x / luas;
pusat_y = pusat_y / luas;
imshow(objek);
line([0 lebar], [round(pusat_y) round(pusat_y)],’Color’,’b’)
line([round(pusat_x) round(pusat_x)], [0 tinggi],’Color’,’b’)
===========================================
function [g] = tepibiner(f)
% tepi biner Berguna untuk mendapatkan tepi objek
% pada citra biner
[jum_baris, jum_kolom] = size(f);
g = zeros(jum_baris, jum_kolom);
for q = 2 : jum_baris - 1
for p = 2 : jum_kolom - 1
p0 = f(q, p+1);
p1 = f(q-1, p+1);
p2 = f(q-1, p);
p3 = f(q-1, p-1);
p4 = f(q, p-1);
p5 = f(q+1, p-1);
p6 = f(q+1, p);
p7 = f(q+1, p+1);
sigma = p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7;
if sigma == 8
g(q, p) = 0;
else
g(q, p) = f(q, p);
end
end
end
end % akhir fungsi
u=imread('c:\image\hotmabin.jpg');
g=tepibiner(u);imshow(g)
program mencari tepi biner objek tanpa fungsi
===========================================
f=imread('c:\image\daun_bin.tif');
[jum_baris, jum_kolom] = size(f);
g=f;
g = zeros(jum_baris, jum_kolom);
for q = 2 : jum_baris - 1
for p = 2 : jum_kolom - 1
p0 = f(q, p+1);
p1 = f(q-1, p+1);
p2 = f(q-1, p);
p3 = f(q-1, p-1);
p4 = f(q, p-1);
p5 = f(q+1, p-1);
p6 = f(q+1, p);
p7 = f(q+1, p+1);
sigma = p0 + p1 + p2 + p3 + p4 + p5 + p6 + p7;
if sigma == 8
g(q, p) = 0;
else
g(q, p) = f(q, p);
end
end
end
subplot(1,2,1);imshow(f)
subplot(1,2,2);imshow(g)
function hasil=perim1(bw)
u=inbound_tracing(bw);
hasil=length(u) -1;
end %akhir fungsi
img=imread('c:\image\daun_bin.tif');
perim1(img)
program perimeter/keliling tanpa inbound_tracing
=========================================
img=imread('c:\image\daun_bin.tif');
perimeter=length(img) -1
program mencari luas objek citra biner denga fungsi
===========================================
function hasil = luas(bw)
% Untuk menghitung luas citra bw (citra biner)
[tinggi, lebar] = size(bw);
hasil = 0;
for p = 1 : tinggi
for q = 1 : lebar
if bw(p, q) == 1
hasil = hasil + 1;
end
end
end
end % akhir fungsi
Daun = imread('c:\image\daun_bin.tif');
luas(Daun)
program mencari luas objek citra biner tanpa fungsi
==========================================
x = imread('c:\image\daun_bin.tif');
[tinggi, lebar] = size(x);
hasil = 0;
for p = 1 : tinggi
for q = 1 : lebar
if x(p, q) == 1
hasil = hasil + 1;
end
end
end
luas_citra_daun_bin=hasil
output
luas_citra_daun_bin = 31862
program diameter objek biner dengan fungsi (masih debug)
================================================
function [diameter, x1, y1, x2, y2] = peroleh_diameter(BW)
U = get_contour(BW);
n = length(U);
jarak_maks = 0;
piksel1 = 0;
piksel2 = 0;
for p=1 : n-1
for q=p+1 : n
jarak = sqrt((U(p,1)-U(q,1)) ^ 2 + (U(p,2)-U(q,2)) ^ 2);
if jarak > jarak_maks
jarak_maks = jarak;
piksel1 = p;
piksel2 = q;
end
end
end
y1 = U(piksel1, 1);
x1 = U(piksel1, 2);
y2 = U(piksel2, 1);
x2 = U(piksel2, 2);
diameter = jarak_maks;
end %akhir fungsi
Daun = imread('c:\image\daun_bin.tif');
[d,x1,y1,x2,y2] = peroleh_diameter(Daun);
d
program diameter objek biner tanpa fungsi
==============================
x= imread('c:\image\daun_bin.tif');
n = length(x);
jarak_maks = 0;
piksel1 = 0;
piksel2 = 0;
for p=1 : n-1
for q=p+1 : n
jarak = sqrt((x(p,1)-x(q,1)) ^ 2 + (x(p,2)-x(q,2)) ^ 2);
if jarak > jarak_maks
jarak_maks = jarak;
piksel1 = p;
piksel2 = q;
end
end
end
diameter = jarak_maks
program pusat massa dan fitur menggunakan pusat massa dengan fungsi
==========================================================
function [pusat_x, pusat_y] = centroid(BW)
% CENTROID Untuk memperoleh pusat massa sebuah objek
% yang terletak pada citra biner BW
[tinggi, lebar] = size(BW);
pusat_x = 0;
pusat_y = 0;
luas = 0;
for q = 1 : tinggi
for p = 1 : lebar
if BW(q, p) == 1
luas = luas + 1;
pusat_x = pusat_x + p;
pusat_y = pusat_y + q;
end
end
end
pusat_x = pusat_x / luas;
pusat_y = pusat_y / luas;
end %akhir fungsi
Daun = imread('C:\Image\daun_bin.tif');
[x, y] = centroid(Daun); imshow(Daun);
[panjang, lebar] = size(Daun);
line([0 lebar], [round(y) round(y)],’Color’,’b’)
line([round(x) round(x)], [0 panjang],’Color’,’b’)
program pusat massa dan fitur menggunakan pusat massa tanpa fungsi
==========================================================
objek = imread('C:\Image\daun_bin.tif');
[tinggi, lebar] = size(objek);
pusat_x = 0;
pusat_y = 0;
luas = 0;
for q = 1 : tinggi
for p = 1 : lebar
if objek(q, p) ==1
luas = luas + 1;
pusat_x = pusat_x + p;
pusat_y = pusat_y + q;
end
end
end
pusat_x = pusat_x / luas;
pusat_y = pusat_y / luas;
imshow(objek);
line([0 lebar], [round(pusat_y) round(pusat_y)],’Color’,’b’)
line([round(pusat_x) round(pusat_x)], [0 tinggi],’Color’,’b’)
7. Pengolahan Citra Berwarna
Program Citra berwarna RGB Ke CMYK dengan fungsi sudah dimodifikasi hotma
=====================================================================
function [c,m,y,k] = rgbkecmyk(r,g,b)
if max(max(r)) > 1.0 || max(max(g)) > 1.0 || max(max(b)) > 1.0
r = double(r) / 255;
g = double(g) / 255;
b = double(b) / 255;
end
u = 0.5;
be = 1;
[tinggi, lebar] = size(r);
for baris=1: tinggi
for kolom=1: lebar
kb = min([(1-r(baris,kolom)) (1-g(baris,kolom)) (1-b(baris,kolom))]);
if kb == 1
c(baris,kolom) = 0;
m(baris,kolom) = 0;
y(baris,kolom) = 0;
else
c(baris,kolom) = (1.0 - r(baris,kolom) - u * kb);
m(baris,kolom) = (1.0 - g(baris,kolom) - u * kb);
y(baris,kolom) = (1.0 - b(baris,kolom) - u * kb);
k(baris,kolom) = be * kb;
end
end
end
% Konversikan ke jangkauan [0,255]
c = uint8(c * 255);
m = uint8(m * 255);
y = uint8(y * 255);
k = uint8(k * 255);
end %akhir fungsi
[c,m,y,k] = rgbkecmyk(171, 215, 170)
konversi rgb ke cmyk dengan nilai =
c = 64
m = 20
y = 65
k = 40
img = imread('c:\Image\an.jpg');
[c,m,y,k] = rgbkecmyk(img(:,:,1), img(:,:,2), img(:,:,3));
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow([c,m,y,k])
Program Citra berwarna RGB Ke CMYK dengan fungsi dari buku
==============================================
function [C,M,Y,K] = RGBkeCMY(R,G,B)
if max(max(R)) > 1.0 || max(max(G)) > 1.0 || max(max(B)) > 1.0
R = double(R) / 255;
G = double(G) / 255;
B = double(B) / 255;
end
u = 0.5;
b = 1;
[tinggi, lebar] = size(R);
for m=1: tinggi
for n=1: lebar
Kb = min([(1-R(m,n)) (1-G(m,n)) (1-B(m,n))]);
if Kb == 1
C(m,n) = 0;
M(m,n) = 0;
Y(m,n) = 0;
else
C(m,n) = (1.0 - R(m,n) - u * Kb);
M(m,n) = (1.0 - G(m,n) - u * Kb);
Y(m,n) = (1.0 - B(m,n) - u * Kb);
K(m,n) = b * Kb;
end
end
end
% Konversikan ke jangkauan [0,255]
C = uint8(C * 255);
M = uint8(M * 255);
Y = uint8(Y * 255);
K = uint8(K * 255);
end %akhir fungsi
[C,M,Y,K] = RGBkeCMY(171, 215, 170)
hasil konversi RGB ke CMYK=
C = 64
M = 20
Y = 65
K = 40
img = imread('C:\Image\an.jpg');
[C,M,Y,K] = RGBkeCMY(img(:,:,1), img(:,:,2), img(:,:,3));
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow([C,M,Y,K])
program cmyk ke rgb dengan fungsi dari buku
=======================================
function [R,G,B] = CMYkeRGB(C,M,Y,K)
% CMYkeRGB digunakan untuk mengonversi CMYK ke RGB
% Berdasarkan Pratt (2001)
% Dasar: b=1 dan u = 0,5
% Normalisasi CMY ke [0, 1]
C = double(C);
M = double(M);
Y = double(Y);
K = double(K);
if max(max(C)) > 1.0 || max(max(M)) > 1.0 || ...
max(max(Y)) > 1.0 || max(max(K)) > 1.0
C = double(C) / 255;
M = double(M) / 255;
Y = double(Y) / 255;
K = double(K) / 255;
end
u = 0.5;
b = 1;
[tinggi, lebar] = size(C);
for m=1: tinggi
for n=1: lebar
Kb = K(m,n) / b;
if Kb == 1
R(m,n)=0;
G(m,n)=0;
B(m,n)=0;
else
R(m,n) = 1 - (C(m, n) + u * Kb);
G(m,n) = 1 - (M(m, n) + u * Kb);
B(m,n) = 1 - (Y(m, n) + u * Kb);
end
end
end
% Konversikan ke jangkauan [0,255]
R = uint8(R * 255);
G = uint8(G * 255);
B = uint8(B * 255);
end %akhir fungsi
[R,G,B] = CMYkeRGB(64,20,65,40)
hasil konversi CMYK ke RGB =
R = 171
G = 215
B = 170
img=imread('c:\image\andri 12.jpg');
[R,G,B] = CMYkeRGB(img(:,:,1), img(:,:,2), img(:,:,3));
Program rgb ke yiq dengan fungsi sudah dimodifikasi Hotma
==================================================
function [y, i, q] = rgbkeyiq(r,g,b)
if max(max(r)) > 1 || max(max(g)) > 1 || max(max(b)) > 1
r = double(r) / 255;
g = double(g) / 255;
b = double(b) / 255;
end
[tinggi, lebar] = size(r);
for m=1: tinggi
for n=1: lebar
y(m,n) = 0.299 * r(m,n) + 0.587 * g(m,n) + 0.114*b(m,n);
i(m,n) = 0.596 * r(m,n) - 0.274 * g(m,n) - 0.322*b(m,n);
q(m,n) = 0.211 * r(m,n) - 0.523 * g(m,n) + 0.312*b(m,n);
end
end
% Konversikan ke jangkauan [0,255]
y = uint8(y * 255);
i = uint8(i * 255);
q = uint8(q * 255);
end %akhir fungsi
[y,i,q] = rgbkeyiq(171, 20, 250)
hasil konversi RGB ke YIQ =
Y = 91
I = 16
Q = 104
img = imread('C:\Image\an.jpg');
[y,i,q] = rgbkeyiq(img(:,:,1), img(:,:,2), img(:,:,3));
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow([y,i,q])
Program RGB ke YIQ dengan fungsi
======================================
function [Y, I, Q] = RGBkeYIQ(R,G,B)
% RGBkeYIQ digunakan untuk mengonversi RGB ke YIQ
% Normalisasi RGB ke [0, 1]
R = double(R);
G = double(G);
B = double(B);
if max(max(R)) > 1.0 || max(max(G)) > 1.0 || max(max(B)) > 1.0
R = double(R) / 255;
G = double(G) / 255;
B = double(B) / 255;
end
[tinggi, lebar] = size(R);
for m=1: tinggi
for n=1: lebar
Y(m,n) = 0.299 * R(m,n) + 0.587 * G(m,n) + 0.114*B(m,n);
I(m,n) = 0.596 * R(m,n) - 0.274 * G(m,n) - 0.322*B(m,n);
Q(m,n) = 0.211 * R(m,n) - 0.523 * G(m,n) + 0.312*B(m,n);
end
end
% Konversikan ke jangkauan [0,255]
Y = uint8(Y * 255);
I = uint8(I * 255);
Q = uint8(Q * 255);
end %akhir fungsi
[Y,I,Q] = RGBkeYIQ(171, 20, 250)
hasil konversi RGB ke YIQ =
Y = 91
I = 16
Q = 104
img = imread('C:\Image\an.jpg');
[Y,I,Q] = RGBkeYIQ(img(:,:,1), img(:,:,2), img(:,:,3));
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow([Y,I,Q])
Program RGB ke YCB dengan fungsi dari buku
=========================================
function [Y, Cb, Cr] = RGBkeYCB(R,G,B)
% RGBkeYCB digunakan untuk mengonversi RGB ke YCbCr
% Normalisasi RGB ke [0, 1]
R = double(R);
G = double(G);
B = double(B);
if max(max(R)) > 1.0 || max(max(G)) > 1.0 || ...
max(max(B)) > 1.0
R = double(R) / 255;
G = double(G) / 255;
B = double(B) / 255;
end
[tinggi, lebar] = size(R);
for m=1: tinggi
for n=1: lebar
Y(m,n)=0.299*R(m,n)+0.587*G(m,n)+ 0.114*B(m,n);
Cb(m,n)=-0.1687*R(m,n)-0.33126*G(m,n)+0.5*B(m,n);
Cr(m,n)=0.5*R(m,n)-0.41869*G(m,n)-0.08131*B(m,n);
end
end
Y = Y * 255;
Cb = Cb * 255;
Cr = Cr * 255;
end %akhir fungsi
[Y,Cb,Cr] = RGBkeYCB(9, 16, 250)
hasil konversi RGB ke YCB dengan angka
Y = 40.583
Cb = 118.18
Cr = -22.527
perintah untuk melihat konversi dengan citra
=======================================
img = imread('C:\Image\an.jpg');
[Y,Cb,Cr] = RGBkeYCB(img(:,:,1), img(:,:,2), img(:,:,3));
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow([Y,Cb,Cr])
=====================================================================
function [c,m,y,k] = rgbkecmyk(r,g,b)
if max(max(r)) > 1.0 || max(max(g)) > 1.0 || max(max(b)) > 1.0
r = double(r) / 255;
g = double(g) / 255;
b = double(b) / 255;
end
u = 0.5;
be = 1;
[tinggi, lebar] = size(r);
for baris=1: tinggi
for kolom=1: lebar
kb = min([(1-r(baris,kolom)) (1-g(baris,kolom)) (1-b(baris,kolom))]);
if kb == 1
c(baris,kolom) = 0;
m(baris,kolom) = 0;
y(baris,kolom) = 0;
else
c(baris,kolom) = (1.0 - r(baris,kolom) - u * kb);
m(baris,kolom) = (1.0 - g(baris,kolom) - u * kb);
y(baris,kolom) = (1.0 - b(baris,kolom) - u * kb);
k(baris,kolom) = be * kb;
end
end
end
% Konversikan ke jangkauan [0,255]
c = uint8(c * 255);
m = uint8(m * 255);
y = uint8(y * 255);
k = uint8(k * 255);
end %akhir fungsi
[c,m,y,k] = rgbkecmyk(171, 215, 170)
konversi rgb ke cmyk dengan nilai =
c = 64
m = 20
y = 65
k = 40
img = imread('c:\Image\an.jpg');
[c,m,y,k] = rgbkecmyk(img(:,:,1), img(:,:,2), img(:,:,3));
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow([c,m,y,k])
Program Citra berwarna RGB Ke CMYK dengan fungsi dari buku
==============================================
function [C,M,Y,K] = RGBkeCMY(R,G,B)
if max(max(R)) > 1.0 || max(max(G)) > 1.0 || max(max(B)) > 1.0
R = double(R) / 255;
G = double(G) / 255;
B = double(B) / 255;
end
u = 0.5;
b = 1;
[tinggi, lebar] = size(R);
for m=1: tinggi
for n=1: lebar
Kb = min([(1-R(m,n)) (1-G(m,n)) (1-B(m,n))]);
if Kb == 1
C(m,n) = 0;
M(m,n) = 0;
Y(m,n) = 0;
else
C(m,n) = (1.0 - R(m,n) - u * Kb);
M(m,n) = (1.0 - G(m,n) - u * Kb);
Y(m,n) = (1.0 - B(m,n) - u * Kb);
K(m,n) = b * Kb;
end
end
end
% Konversikan ke jangkauan [0,255]
C = uint8(C * 255);
M = uint8(M * 255);
Y = uint8(Y * 255);
K = uint8(K * 255);
end %akhir fungsi
[C,M,Y,K] = RGBkeCMY(171, 215, 170)
hasil konversi RGB ke CMYK=
C = 64
M = 20
Y = 65
K = 40
img = imread('C:\Image\an.jpg');
[C,M,Y,K] = RGBkeCMY(img(:,:,1), img(:,:,2), img(:,:,3));
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow([C,M,Y,K])
program cmyk ke rgb dengan fungsi dari buku
=======================================
function [R,G,B] = CMYkeRGB(C,M,Y,K)
% CMYkeRGB digunakan untuk mengonversi CMYK ke RGB
% Berdasarkan Pratt (2001)
% Dasar: b=1 dan u = 0,5
% Normalisasi CMY ke [0, 1]
C = double(C);
M = double(M);
Y = double(Y);
K = double(K);
if max(max(C)) > 1.0 || max(max(M)) > 1.0 || ...
max(max(Y)) > 1.0 || max(max(K)) > 1.0
C = double(C) / 255;
M = double(M) / 255;
Y = double(Y) / 255;
K = double(K) / 255;
end
u = 0.5;
b = 1;
[tinggi, lebar] = size(C);
for m=1: tinggi
for n=1: lebar
Kb = K(m,n) / b;
if Kb == 1
R(m,n)=0;
G(m,n)=0;
B(m,n)=0;
else
R(m,n) = 1 - (C(m, n) + u * Kb);
G(m,n) = 1 - (M(m, n) + u * Kb);
B(m,n) = 1 - (Y(m, n) + u * Kb);
end
end
end
% Konversikan ke jangkauan [0,255]
R = uint8(R * 255);
G = uint8(G * 255);
B = uint8(B * 255);
end %akhir fungsi
[R,G,B] = CMYkeRGB(64,20,65,40)
hasil konversi CMYK ke RGB =
R = 171
G = 215
B = 170
img=imread('c:\image\andri 12.jpg');
[R,G,B] = CMYkeRGB(img(:,:,1), img(:,:,2), img(:,:,3));
Program rgb ke yiq dengan fungsi sudah dimodifikasi Hotma
==================================================
function [y, i, q] = rgbkeyiq(r,g,b)
if max(max(r)) > 1 || max(max(g)) > 1 || max(max(b)) > 1
r = double(r) / 255;
g = double(g) / 255;
b = double(b) / 255;
end
[tinggi, lebar] = size(r);
for m=1: tinggi
for n=1: lebar
y(m,n) = 0.299 * r(m,n) + 0.587 * g(m,n) + 0.114*b(m,n);
i(m,n) = 0.596 * r(m,n) - 0.274 * g(m,n) - 0.322*b(m,n);
q(m,n) = 0.211 * r(m,n) - 0.523 * g(m,n) + 0.312*b(m,n);
end
end
% Konversikan ke jangkauan [0,255]
y = uint8(y * 255);
i = uint8(i * 255);
q = uint8(q * 255);
end %akhir fungsi
[y,i,q] = rgbkeyiq(171, 20, 250)
hasil konversi RGB ke YIQ =
Y = 91
I = 16
Q = 104
img = imread('C:\Image\an.jpg');
[y,i,q] = rgbkeyiq(img(:,:,1), img(:,:,2), img(:,:,3));
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow([y,i,q])
Program RGB ke YIQ dengan fungsi
======================================
function [Y, I, Q] = RGBkeYIQ(R,G,B)
% RGBkeYIQ digunakan untuk mengonversi RGB ke YIQ
% Normalisasi RGB ke [0, 1]
R = double(R);
G = double(G);
B = double(B);
if max(max(R)) > 1.0 || max(max(G)) > 1.0 || max(max(B)) > 1.0
R = double(R) / 255;
G = double(G) / 255;
B = double(B) / 255;
end
[tinggi, lebar] = size(R);
for m=1: tinggi
for n=1: lebar
Y(m,n) = 0.299 * R(m,n) + 0.587 * G(m,n) + 0.114*B(m,n);
I(m,n) = 0.596 * R(m,n) - 0.274 * G(m,n) - 0.322*B(m,n);
Q(m,n) = 0.211 * R(m,n) - 0.523 * G(m,n) + 0.312*B(m,n);
end
end
% Konversikan ke jangkauan [0,255]
Y = uint8(Y * 255);
I = uint8(I * 255);
Q = uint8(Q * 255);
end %akhir fungsi
[Y,I,Q] = RGBkeYIQ(171, 20, 250)
hasil konversi RGB ke YIQ =
Y = 91
I = 16
Q = 104
img = imread('C:\Image\an.jpg');
[Y,I,Q] = RGBkeYIQ(img(:,:,1), img(:,:,2), img(:,:,3));
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow([Y,I,Q])
Program RGB ke YCB dengan fungsi dari buku
=========================================
function [Y, Cb, Cr] = RGBkeYCB(R,G,B)
% RGBkeYCB digunakan untuk mengonversi RGB ke YCbCr
% Normalisasi RGB ke [0, 1]
R = double(R);
G = double(G);
B = double(B);
if max(max(R)) > 1.0 || max(max(G)) > 1.0 || ...
max(max(B)) > 1.0
R = double(R) / 255;
G = double(G) / 255;
B = double(B) / 255;
end
[tinggi, lebar] = size(R);
for m=1: tinggi
for n=1: lebar
Y(m,n)=0.299*R(m,n)+0.587*G(m,n)+ 0.114*B(m,n);
Cb(m,n)=-0.1687*R(m,n)-0.33126*G(m,n)+0.5*B(m,n);
Cr(m,n)=0.5*R(m,n)-0.41869*G(m,n)-0.08131*B(m,n);
end
end
Y = Y * 255;
Cb = Cb * 255;
Cr = Cr * 255;
end %akhir fungsi
[Y,Cb,Cr] = RGBkeYCB(9, 16, 250)
hasil konversi RGB ke YCB dengan angka
Y = 40.583
Cb = 118.18
Cr = -22.527
perintah untuk melihat konversi dengan citra
=======================================
img = imread('C:\Image\an.jpg');
[Y,Cb,Cr] = RGBkeYCB(img(:,:,1), img(:,:,2), img(:,:,3));
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow([Y,Cb,Cr])
8. Segmentasi Citra
Program deteksi garis citra biner dengan fungsi
==============================================
function [g] = deteksi(f, h, potong, pembulatan)
% deteksi Melakukan operasi korelasi kernel h dengan citra f
% h harus mempunyai tinggi dan lebar ganjil
% Argumen potong bersifat opsional. Apabila
% bernilai true, bagian citra yang tidak diproses
% akan dipotong. Bawaan = false
% Argumen pembulatan bersifat opsional.
% Apabila bernilai true, pembulatan dengan uint8
% tidak dilakukan. Bawaan = true
% Hasil: citra g
if nargin < 3
potong = false;
end
if nargin < 4
pembulatan = true;
end
[tinggi_f, lebar_f] = size(f);
[tinggi_h, lebar_h] = size(h);
if rem(lebar_h, 2) == 0 || rem(tinggi_h, 2) == 0
disp('lebar dan tinggi h harus ganjil');
return;
end
m2 = floor(tinggi_h / 2);
n2 = floor(lebar_h / 2);
% Menentukan ukuran hasil beserta penentu koordinat horizontal/vertikal/45/-45
if potong == true
sisi_m2 = m2;
sisi_n2 = n2;
g = zeros(tinggi_f - 2 * m2, lebar_f - 2 * n2);
else
sisi_m2 = 0;
sisi_n2 = 0;
g = zeros(size(f));
end
f2=double(f);
for y=m2+1 : tinggi_f-m2
for x=n2+1 : lebar_f-n2
% Pelaksanaan korelasi f(baris, kolom)
jum = 0;
for p= -m2 : m2
for q= -n2 : n2
jum = jum + h(p+m2+1,q+n2+1) * f2(y+p, x+q);
end
end
g(y - sisi_m2, x - sisi_n2) = jum;
end
end
if pembulatan == true
g = uint8(g);
end
end %akhir fungsi
h1 = [-1 2 -1; -1 2 -1 ; -1 2 -1];
img = imread('C:\image\kotak.png');
g = deteksi(img, h1);
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow(g,[0 1])
Program Deteksi garis pada citra Biner
====================================
function [G] = deteksi(F, H, potong, pembulatan)
if nargin < 3
potong = false;
end
if nargin < 4
pembulatan = true;
end
[tinggi_f, lebar_f] = size(F);
[tinggi_h, lebar_h] = size(H);
if rem(lebar_h,2) == 0 || rem(tinggi_h,2) == 0
disp('Lebar dan tinggi H harus ganjil');
return;
end
m2 = floor(tinggi_h/2);
n2 = floor(lebar_h/2);
% Menentukan ukuran hasil beserta
% penentu ofset koordinat
if potong == true
sisi_m2 = m2;
sisi_n2 = n2;
G = zeros(tinggi_f - 2 * m2, lebar_f - 2 * n2);
else
sisi_m2 = 0;
sisi_n2 = 0;
G = zeros(size(F));
end
F2=double(F);
for y=m2+1 : tinggi_f-m2
for x=n2+1 : lebar_f-n2
% Pelaksanaan korelasi F(baris, kolom)
jum = 0;
for p=-m2 : m2
for q=-n2 : n2
jum = jum + H(p+m2+1,q+n2+1) * ...
F2(y+p, x+q);
end
end
G(y - sisi_m2, x - sisi_n2) = jum;
end
end
if pembulatan == true
G = uint8(G);
end
end %akhir fungsi
H1 = [-1 -1 2; -1 2 -1 ; 2 -1 -1];
img = imread('C:\image\kotak.png');
G = deteksi(img, H1);
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow(G,[0 1])
H1 = [-1 -1 -1; 2 2 2; -1 -1 -1];
img = imread('C:\image\kotak.png');
G = deteksi(img, H1);
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow(G,[0 1])
H1 = [-1 2 -1; -1 2 -1;-1 2 -1];
img = imread('C:\image\kotak.png');
G = deteksi(img, H1);
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow(G,[0 1])
H1 = [2 -1 -1; -1 2 -1;-1 -1 2];
img = imread('C:\image\kotak.png');
G = deteksi(img, H1);
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow(G,[0 1])
program Operator Robert dengan fungsi dr Buku
======================================
function [G] = roberts(F)
[m, n] = size(F);
F=double(F);
for y=1 : m-1
for x=1 : n-1
G(y, x) = sqrt((F(y,x)-F(y+1,x+1))^2 + ...
(F(y+1,x)-F(y,x+1))^2) ;
end
end
G = uint8(G);
end %akhir fungsi
img = (imread('C:\Image\lena128.tif'));
G = roberts(img);
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow(G)
program Operator Robert dengan fungsi sudah dimodifikasi
==========================================
function [g] = roberts(f)
[jum_baris, jum_kolom] = size(f);
f=double(f);
for y=1 : jum_baris-1
for x=1 : jum_kolom-1
g(y, x) = sqrt((f(y,x)-f(y+1,x+1))^2 + ...
(f(y+1,x)-f(y,x+1))^2) ;
end
end
g = uint8(g);
end %akhir fungsi
f = imread('c:\image\boneka.tif');
g = roberts(f);
subplot(1,2,1);imshow(f)
subplot(1,2,2);imshow(g)
program Operator Robert tanpa fungsi sudah dimodifikasi
=======================================
img = imread('c:\image\lena128.tif');
f=img;
[jum_baris, jum_kolom] = size(f);
f=double(f);
g=zeros(jum_baris,jum_kolom);
for y=1 : jum_baris -1
for x=1 : jum_kolom -1
g(y, x) = sqrt((f(y,x)-f(y+1,x+1))^2 + ...
(f(y+1,x)-f(y,x+1))^2) ;
end
end
g = uint8(g);
subplot(1,2,1);imshow(f)
subplot(1,2,2);imshow(g)
program Operator Prewitt dengan fungsi dari buku
=======================================
function [G] = prewitt(F)
% PREWITT Pemerolehan tepi objek pada citra F
% melalui operator Prewitt
% Hasil: citra G
[m, n] = size(F);
F=double(F);
G=zeros(m,n);
for y=2 : m-1
for x=2 : n-1
G(y, x) = sqrt((F(y-1,x-1) + F(y,x-1) + F(y+1,x-1) - ...
F(y,x) - F(y,x+1) - F(y+1,x+1))^2 + ...
(F(y+1,x-1)+ F(y+1,x) + F(y+1,x+1) - ...
F(y-1,x-1) - F(y-1,x) - F(y-1,x+1))^2) ;
end
end
G = uint8(G);
end %akhir fungsi
Img = (imread('C:\Image\lena128.tif'));
G = prewitt(Img);
subplot(1,2,1);imshow(Img)
subplot(1,2,2);imshow(G)
program Operator Prewitt dengan fungsi sudah dimodifikasi
===================================================
function [g] = prewitt(f)
[jum_baris, jum_kolom] = size(f);
f=double(f);
g=zeros(jum_baris,jum_kolom);
for y=2 : jum_baris-1
for x=2 : jum_kolom-1
g(y, x) = sqrt((f(y-1,x-1) + f(y,x-1) + f(y+1,x-1) - ...
f(y,x) - f(y,x+1) - f(y+1,x+1))^2 + ...
(f(y+1,x-1)+ f(y+1,x) + f(y+1,x+1) - ...
f(y-1,x-1) - f(y-1,x) - f(y-1,x+1))^2) ;
end
end
g = uint8(g);
end %akhir fungsi
f = imread('c:\image\boneka.tif');
g = prewitt(f);
subplot(1,2,1);imshow(f)
subplot(1,2,2);imshow(g)
program Operator sobel dengan fungsi
===================================================
function [G] = sobel(F)
% SOBEL Pemerolehan tepi objek pada citra F
% melalui operator Sobel
% Hasil: citra G
[m, n] = size(F);
F=double(F);
G=zeros(m,n);
for y=2 : m-1
for x=2 : n-1
G(y, x) = sqrt(...
(F(y-1,x+1)+2*F(y,x+1)+F(y+1,x+1) - ...
F(y-1,x-1)-F(y,x-1)-F(y+1,x-1))^2 + ...
(F(y-1,x-1)+2*F(y-1,x)+F(y-1,x+1) - ...
F(y+1,x-1)-2*F(y+1,x)-F(y+1,x+1))^2) ;
end
end
G = uint8(G);
end %akhir fungsi
img = imread('C:\image\lena128.tif');
g = prewitt(img);
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow(g)
program Operator sobel dengan fungsi sudah di modifikasi
===================================================
function [g] = sobel(f)
[m, n] = size(f);
f=double(f);
g=zeros(m,n);
for y=2 : m-1
for x=2 : n-1
g(y, x) = sqrt(...
(f(y-1,x+1)+2*f(y,x+1)+f(y+1,x+1) - ...
f(y-1,x-1)-f(y,x-1)-f(y+1,x-1))^2 + ...
(f(y-1,x-1)+2*f(y-1,x)+f(y-1,x+1) - ...
f(y+1,x-1)-2*f(y+1,x)-f(y+1,x+1))^2) ;
end
end
g = uint8(g);
end %akhir fungsi
img = imread('c:\image\lena128.tif');
g = prewitt(img);
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow(g)
Program freichen dengan fungsi dari buku
========================================
function [G] = freichen(F)
% FREICHEN Pemerolehan tepi objek pada citra F
% melalui operator Frei-Chen
% Hasil: citra G
[m, n] = size(F);
akar2 = sqrt(2);
F=double(F);
G=zeros(m,n);
for y=2 : m-1
for x=2 : n-1
G(y, x) = sqrt(...
(F(y-1,x+1)+akar2*F(y,x+1)+F(y+1,x+1) - ...
F(y-1,x-1)-F(y,x-1)-F(y+1,x-1))^2 + ...
(F(y-1,x-1)+akar2*F(y-1,x)+F(y-1,x+1) - ...
F(y+1,x-1)-akar2*F(y+1,x)-F(y+1,x+1))^2) ;
end
end
G = uint8(G);
end %akhir fungsi
img = imread('c:\image\lena128.tif');
g = prewitt(img);
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow(g)
Program freichen dengan fungsi sudah di modifikasi
========================================
function [g] = freichen(f)
[m, n] = size(f);
akar2 = sqrt(2);
f=double(f);
g=zeros(m,n);
for y=2 : m-1
for x=2 : n-1
g(y, x) = sqrt((f(y-1,x+1)+akar2*f(y,x+1)+f(y+1,x+1) - ...
f(y-1,x-1)-f(y,x-1)-f(y+1,x-1))^2 + ...
(f(y-1,x-1)+akar2*f(y-1,x)+f(y-1,x+1) - ...
f(y+1,x-1)-akar2*f(y+1,x)-f(y+1,x+1))^2) ;
end
end
g = uint8(g);
end %akhir fungsi
img = imread('c:\image\lena128.tif');
g = freichen(img);
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow(g)
Program Laplacian dengan fungsi dari buku
=======================================
function [G] = laplacian(F)
% LAPALACIAN2 Pemperoleh tepi objek pada citra F
% melalui operator Laplacian #1
% Hasil: citra G
[m, n] = size(F);
G=zeros(m,n); % Semua berisi nol
F=double(F);
for y=2 : m-1
for x=2 : n-1
G(y, x) = 8 * F(y,x) - ...
(F(y-1,x)+ F(y,x-1)+F(y,x+1)+F(y+1,x) + ...
F(y-1,x-1)+ F(y-1,x+1)+F(y+1,x-1)+F(y+1,x+1));
end
end
G = uint8(G);
end %akhir fungsi
img = imread('c:\image\lena128.tif');
g = laplacian(img);
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow(g)
Program Laplacian dengan fungsi sudah di modif
=======================================
function [g] = laplacian(f)
[m, n] = size(f);
g=zeros(m,n);
f=double(f);
for y=2 : m-1
for x=2 : n-1
g(y, x) = 8 * f(y,x) - ...
(f(y-1,x)+ f(y,x-1)+f(y,x+1)+f(y+1,x) + ...
f(y-1,x-1)+ f(y-1,x+1)+f(y+1,x-1)+f(y+1,x+1));
end
end
g = uint8(g);
end %akhir fungsi
f = imread('c:\image\boneka.tif');
g = laplacian(f);
subplot(1,2,1);imshow(f)
subplot(1,2,2);imshow(g)
Program Laplacian tanpa fungsi sudah di modif
=======================================
img = imread('c:\image\lena128.tif');
f=img;
[m, n] = size(f);
g=zeros(m,n);
f=double(f);
for y=2 : m-1
for x=2 : n-1
g(y, x) = 8 * f(y,x) - ...
(f(y-1,x)+ f(y,x-1)+f(y,x+1)+f(y+1,x) + ...
f(y-1,x-1)+ f(y-1,x+1)+f(y+1,x-1)+f(y+1,x+1));
end
end
g = uint8(g);
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow(g)
==============================================
function [g] = deteksi(f, h, potong, pembulatan)
% deteksi Melakukan operasi korelasi kernel h dengan citra f
% h harus mempunyai tinggi dan lebar ganjil
% Argumen potong bersifat opsional. Apabila
% bernilai true, bagian citra yang tidak diproses
% akan dipotong. Bawaan = false
% Argumen pembulatan bersifat opsional.
% Apabila bernilai true, pembulatan dengan uint8
% tidak dilakukan. Bawaan = true
% Hasil: citra g
if nargin < 3
potong = false;
end
if nargin < 4
pembulatan = true;
end
[tinggi_f, lebar_f] = size(f);
[tinggi_h, lebar_h] = size(h);
if rem(lebar_h, 2) == 0 || rem(tinggi_h, 2) == 0
disp('lebar dan tinggi h harus ganjil');
return;
end
m2 = floor(tinggi_h / 2);
n2 = floor(lebar_h / 2);
% Menentukan ukuran hasil beserta penentu koordinat horizontal/vertikal/45/-45
if potong == true
sisi_m2 = m2;
sisi_n2 = n2;
g = zeros(tinggi_f - 2 * m2, lebar_f - 2 * n2);
else
sisi_m2 = 0;
sisi_n2 = 0;
g = zeros(size(f));
end
f2=double(f);
for y=m2+1 : tinggi_f-m2
for x=n2+1 : lebar_f-n2
% Pelaksanaan korelasi f(baris, kolom)
jum = 0;
for p= -m2 : m2
for q= -n2 : n2
jum = jum + h(p+m2+1,q+n2+1) * f2(y+p, x+q);
end
end
g(y - sisi_m2, x - sisi_n2) = jum;
end
end
if pembulatan == true
g = uint8(g);
end
end %akhir fungsi
h1 = [-1 2 -1; -1 2 -1 ; -1 2 -1];
img = imread('C:\image\kotak.png');
g = deteksi(img, h1);
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow(g,[0 1])
Program Deteksi garis pada citra Biner
====================================
function [G] = deteksi(F, H, potong, pembulatan)
if nargin < 3
potong = false;
end
if nargin < 4
pembulatan = true;
end
[tinggi_f, lebar_f] = size(F);
[tinggi_h, lebar_h] = size(H);
if rem(lebar_h,2) == 0 || rem(tinggi_h,2) == 0
disp('Lebar dan tinggi H harus ganjil');
return;
end
m2 = floor(tinggi_h/2);
n2 = floor(lebar_h/2);
% Menentukan ukuran hasil beserta
% penentu ofset koordinat
if potong == true
sisi_m2 = m2;
sisi_n2 = n2;
G = zeros(tinggi_f - 2 * m2, lebar_f - 2 * n2);
else
sisi_m2 = 0;
sisi_n2 = 0;
G = zeros(size(F));
end
F2=double(F);
for y=m2+1 : tinggi_f-m2
for x=n2+1 : lebar_f-n2
% Pelaksanaan korelasi F(baris, kolom)
jum = 0;
for p=-m2 : m2
for q=-n2 : n2
jum = jum + H(p+m2+1,q+n2+1) * ...
F2(y+p, x+q);
end
end
G(y - sisi_m2, x - sisi_n2) = jum;
end
end
if pembulatan == true
G = uint8(G);
end
end %akhir fungsi
H1 = [-1 -1 2; -1 2 -1 ; 2 -1 -1];
img = imread('C:\image\kotak.png');
G = deteksi(img, H1);
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow(G,[0 1])
H1 = [-1 -1 -1; 2 2 2; -1 -1 -1];
img = imread('C:\image\kotak.png');
G = deteksi(img, H1);
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow(G,[0 1])
H1 = [-1 2 -1; -1 2 -1;-1 2 -1];
img = imread('C:\image\kotak.png');
G = deteksi(img, H1);
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow(G,[0 1])
H1 = [2 -1 -1; -1 2 -1;-1 -1 2];
img = imread('C:\image\kotak.png');
G = deteksi(img, H1);
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow(G,[0 1])
program Operator Robert dengan fungsi dr Buku
======================================
function [G] = roberts(F)
[m, n] = size(F);
F=double(F);
for y=1 : m-1
for x=1 : n-1
G(y, x) = sqrt((F(y,x)-F(y+1,x+1))^2 + ...
(F(y+1,x)-F(y,x+1))^2) ;
end
end
G = uint8(G);
end %akhir fungsi
img = (imread('C:\Image\lena128.tif'));
G = roberts(img);
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow(G)
program Operator Robert dengan fungsi sudah dimodifikasi
==========================================
function [g] = roberts(f)
[jum_baris, jum_kolom] = size(f);
f=double(f);
for y=1 : jum_baris-1
for x=1 : jum_kolom-1
g(y, x) = sqrt((f(y,x)-f(y+1,x+1))^2 + ...
(f(y+1,x)-f(y,x+1))^2) ;
end
end
g = uint8(g);
end %akhir fungsi
f = imread('c:\image\boneka.tif');
g = roberts(f);
subplot(1,2,1);imshow(f)
subplot(1,2,2);imshow(g)
program Operator Robert tanpa fungsi sudah dimodifikasi
=======================================
img = imread('c:\image\lena128.tif');
f=img;
[jum_baris, jum_kolom] = size(f);
f=double(f);
g=zeros(jum_baris,jum_kolom);
for y=1 : jum_baris -1
for x=1 : jum_kolom -1
g(y, x) = sqrt((f(y,x)-f(y+1,x+1))^2 + ...
(f(y+1,x)-f(y,x+1))^2) ;
end
end
g = uint8(g);
subplot(1,2,1);imshow(f)
subplot(1,2,2);imshow(g)
program Operator Prewitt dengan fungsi dari buku
=======================================
function [G] = prewitt(F)
% PREWITT Pemerolehan tepi objek pada citra F
% melalui operator Prewitt
% Hasil: citra G
[m, n] = size(F);
F=double(F);
G=zeros(m,n);
for y=2 : m-1
for x=2 : n-1
G(y, x) = sqrt((F(y-1,x-1) + F(y,x-1) + F(y+1,x-1) - ...
F(y,x) - F(y,x+1) - F(y+1,x+1))^2 + ...
(F(y+1,x-1)+ F(y+1,x) + F(y+1,x+1) - ...
F(y-1,x-1) - F(y-1,x) - F(y-1,x+1))^2) ;
end
end
G = uint8(G);
end %akhir fungsi
Img = (imread('C:\Image\lena128.tif'));
G = prewitt(Img);
subplot(1,2,1);imshow(Img)
subplot(1,2,2);imshow(G)
program Operator Prewitt dengan fungsi sudah dimodifikasi
===================================================
function [g] = prewitt(f)
[jum_baris, jum_kolom] = size(f);
f=double(f);
g=zeros(jum_baris,jum_kolom);
for y=2 : jum_baris-1
for x=2 : jum_kolom-1
g(y, x) = sqrt((f(y-1,x-1) + f(y,x-1) + f(y+1,x-1) - ...
f(y,x) - f(y,x+1) - f(y+1,x+1))^2 + ...
(f(y+1,x-1)+ f(y+1,x) + f(y+1,x+1) - ...
f(y-1,x-1) - f(y-1,x) - f(y-1,x+1))^2) ;
end
end
g = uint8(g);
end %akhir fungsi
f = imread('c:\image\boneka.tif');
g = prewitt(f);
subplot(1,2,1);imshow(f)
subplot(1,2,2);imshow(g)
program Operator sobel dengan fungsi
===================================================
function [G] = sobel(F)
% SOBEL Pemerolehan tepi objek pada citra F
% melalui operator Sobel
% Hasil: citra G
[m, n] = size(F);
F=double(F);
G=zeros(m,n);
for y=2 : m-1
for x=2 : n-1
G(y, x) = sqrt(...
(F(y-1,x+1)+2*F(y,x+1)+F(y+1,x+1) - ...
F(y-1,x-1)-F(y,x-1)-F(y+1,x-1))^2 + ...
(F(y-1,x-1)+2*F(y-1,x)+F(y-1,x+1) - ...
F(y+1,x-1)-2*F(y+1,x)-F(y+1,x+1))^2) ;
end
end
G = uint8(G);
end %akhir fungsi
img = imread('C:\image\lena128.tif');
g = prewitt(img);
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow(g)
program Operator sobel dengan fungsi sudah di modifikasi
===================================================
function [g] = sobel(f)
[m, n] = size(f);
f=double(f);
g=zeros(m,n);
for y=2 : m-1
for x=2 : n-1
g(y, x) = sqrt(...
(f(y-1,x+1)+2*f(y,x+1)+f(y+1,x+1) - ...
f(y-1,x-1)-f(y,x-1)-f(y+1,x-1))^2 + ...
(f(y-1,x-1)+2*f(y-1,x)+f(y-1,x+1) - ...
f(y+1,x-1)-2*f(y+1,x)-f(y+1,x+1))^2) ;
end
end
g = uint8(g);
end %akhir fungsi
img = imread('c:\image\lena128.tif');
g = prewitt(img);
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow(g)
Program freichen dengan fungsi dari buku
========================================
function [G] = freichen(F)
% FREICHEN Pemerolehan tepi objek pada citra F
% melalui operator Frei-Chen
% Hasil: citra G
[m, n] = size(F);
akar2 = sqrt(2);
F=double(F);
G=zeros(m,n);
for y=2 : m-1
for x=2 : n-1
G(y, x) = sqrt(...
(F(y-1,x+1)+akar2*F(y,x+1)+F(y+1,x+1) - ...
F(y-1,x-1)-F(y,x-1)-F(y+1,x-1))^2 + ...
(F(y-1,x-1)+akar2*F(y-1,x)+F(y-1,x+1) - ...
F(y+1,x-1)-akar2*F(y+1,x)-F(y+1,x+1))^2) ;
end
end
G = uint8(G);
end %akhir fungsi
img = imread('c:\image\lena128.tif');
g = prewitt(img);
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow(g)
Program freichen dengan fungsi sudah di modifikasi
========================================
function [g] = freichen(f)
[m, n] = size(f);
akar2 = sqrt(2);
f=double(f);
g=zeros(m,n);
for y=2 : m-1
for x=2 : n-1
g(y, x) = sqrt((f(y-1,x+1)+akar2*f(y,x+1)+f(y+1,x+1) - ...
f(y-1,x-1)-f(y,x-1)-f(y+1,x-1))^2 + ...
(f(y-1,x-1)+akar2*f(y-1,x)+f(y-1,x+1) - ...
f(y+1,x-1)-akar2*f(y+1,x)-f(y+1,x+1))^2) ;
end
end
g = uint8(g);
end %akhir fungsi
img = imread('c:\image\lena128.tif');
g = freichen(img);
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow(g)
Program Laplacian dengan fungsi dari buku
=======================================
function [G] = laplacian(F)
% LAPALACIAN2 Pemperoleh tepi objek pada citra F
% melalui operator Laplacian #1
% Hasil: citra G
[m, n] = size(F);
G=zeros(m,n); % Semua berisi nol
F=double(F);
for y=2 : m-1
for x=2 : n-1
G(y, x) = 8 * F(y,x) - ...
(F(y-1,x)+ F(y,x-1)+F(y,x+1)+F(y+1,x) + ...
F(y-1,x-1)+ F(y-1,x+1)+F(y+1,x-1)+F(y+1,x+1));
end
end
G = uint8(G);
end %akhir fungsi
img = imread('c:\image\lena128.tif');
g = laplacian(img);
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow(g)
Program Laplacian dengan fungsi sudah di modif
=======================================
function [g] = laplacian(f)
[m, n] = size(f);
g=zeros(m,n);
f=double(f);
for y=2 : m-1
for x=2 : n-1
g(y, x) = 8 * f(y,x) - ...
(f(y-1,x)+ f(y,x-1)+f(y,x+1)+f(y+1,x) + ...
f(y-1,x-1)+ f(y-1,x+1)+f(y+1,x-1)+f(y+1,x+1));
end
end
g = uint8(g);
end %akhir fungsi
f = imread('c:\image\boneka.tif');
g = laplacian(f);
subplot(1,2,1);imshow(f)
subplot(1,2,2);imshow(g)
Program Laplacian tanpa fungsi sudah di modif
=======================================
img = imread('c:\image\lena128.tif');
f=img;
[m, n] = size(f);
g=zeros(m,n);
f=double(f);
for y=2 : m-1
for x=2 : n-1
g(y, x) = 8 * f(y,x) - ...
(f(y-1,x)+ f(y,x-1)+f(y,x+1)+f(y+1,x) + ...
f(y-1,x-1)+ f(y-1,x+1)+f(y+1,x-1)+f(y+1,x+1));
end
end
g = uint8(g);
subplot(1,2,1);imshow(img)
subplot(1,2,2);imshow(g)
9. Restorasi Citra
Program derau gaussian dengan fungsi
==================================================
function [g] = gaussian(f, sigma, mu)
% g = Menghasilkan citra yang telah diberi derau menggunakan gaussian.
% f = citra masukan berskala keabuan
% sigma = standar deviasi fungsi gaussian
% mu = rerata fungsi Gaussian
if nargin < 3
mu = 0; % Nilai bawaan mu
end
if nargin < 2
sigma = 1; % Nilai bawaan standar deviasi
end
[m, n] = size(f);
g = double(f);
for i=1 : m
for j=1 : n
derau = randn * sigma + mu;
g(i,j) = round(f(i, j) + derau);
if g(i,j) >= 255
g(i,j) = 255;
elseif g(i,j) <= 0
g(i,j) = 0;
end
end
end
g = uint8(g);
end %akhir fungsi
f = imread('c:\image\kartun.tif');
g = gaussian(f, 150);
subplot(1,2,1);imshow(f), title('citra masukan')
subplot(1,2,2);imshow(g), title('citra keluaran')
Program garam dan merica dengan fungsi
=================================
function [g] = gamer(f, probabilitas)
if nargin < 2
probabilitas = 0.05; % Nilai bawaan
end;
[m, n] = size(f);
g = double(f);
for i=1 : m
for j=1 : n
nilai_acak = rand;
if nilai_acak <= probabilitas / 2
g(i,j) = 0;
elseif (nilai_acak > probabilitas / 2) && ...
(nilai_acak <= probabilitas)
g(i,j) = 255;
end
end
end
g = uint8(g);
end %akhir fungsi
f = imread('c:\image\boneka.tif');
g = gamer(f, 0.05);
subplot(1,2,1);imshow(f), title('citra masukan')
subplot(1,2,2);imshow(g), title('citra keluaran')
Program derau periodik dengan fungsi
=======================================================
function [G] = drperiodik(F, a, bx, by)
% DRPERIODIK Menghasilkan citra yang teklah ditambahi
% derau periodis.
%
% F = Citra berskala keabuan
% b = penegas derau (1, 2, 3, ...)
% ax dan ay menentukan kemiringan derau
if nargin < 4
a = 3; bx = 3; by = 5;
end
[m, n] = size(F);
for i=1:m
for j=1:n
X(i,j) = j;
Y(i,j) = i;
end
end
derau = a * sin(X/bx + Y/by) + 1;
G = uint8(double(F) + derau);
end %akhir fungsi
f = imread('c:\image\hotmaabu.jpg');
g = drperiodik(f, 10,3,4);
subplot(1,2,1);imshow(f), title('citra masukan')
subplot(1,2,2);imshow(g), title('citra keluaran')
program penghilangan derau rerata aritmatik dengan fungsi
=============================================
function [G] = filarithmean(F, ukuran)
% FILARITHMEAN Melakukan penghilangan derau dengan
% menggunakan filter rerata aritmetik
% F = Citra berskala keabuan
% ukuran = ukuran jendela
% G = Citra hasil pemrosesan
if nargin < 2
ukuran = 5;
end
[m, n] = size(F);
setengah = floor(ukuran / 2);
F = double(F);
G = zeros(m-2*setengah, n-2*setengah);
for i=1+setengah : m-setengah
for j=1+setengah: n-setengah
jum = 0;
for p = -setengah : setengah
for q = -setengah : setengah
jum = jum + F(i+p, j+q);
end
end
G(i-setengah, j-setengah) = jum / (ukuran * ukuran);
end
end
G = uint8(G);
end %akhir fungsi
F = imread('C:\Image\hotmagaussian.png');
G = filarithmean(F);
subplot(1,2,1);imshow(F)
subplot(1,2,2);imshow(G)
Program filter harmonik dengan fungsi
=======================================================
function [G] = filharmonik(F, ukuran)
% FILHARMONIK Melakukan penghilangan derau dengan
% menggunakan filter rerata harmonik
% F = Citra berskala keabuan
% ukuran = ukuran jendela
% G = Citra hasil pemrosesan
if nargin < 2
ukuran = 3;
end
[m, n] = size(F);
setengah = floor(ukuran / 2);
F = double(F);
G = zeros(m-2*setengah, n-2*setengah);
for i=1+setengah : m-setengah
for j=1+setengah: n-setengah
jum = 0;
for p = -setengah : setengah
for q = -setengah : setengah
jum = jum + 1 / F(i+p, j+q);
end
end
G(i-setengah, j-setengah) = (ukuran * ukuran) / jum;
end
end
G = uint8(G);
end %akhir fungsi
F = imread('C:\Image\hotmagaussian.png');
G = filharmonik(F, 5);
subplot(1,2,1);imshow(F)
subplot(1,2,2);imshow(G)
==================================================
function [g] = gaussian(f, sigma, mu)
% g = Menghasilkan citra yang telah diberi derau menggunakan gaussian.
% f = citra masukan berskala keabuan
% sigma = standar deviasi fungsi gaussian
% mu = rerata fungsi Gaussian
if nargin < 3
mu = 0; % Nilai bawaan mu
end
if nargin < 2
sigma = 1; % Nilai bawaan standar deviasi
end
[m, n] = size(f);
g = double(f);
for i=1 : m
for j=1 : n
derau = randn * sigma + mu;
g(i,j) = round(f(i, j) + derau);
if g(i,j) >= 255
g(i,j) = 255;
elseif g(i,j) <= 0
g(i,j) = 0;
end
end
end
g = uint8(g);
end %akhir fungsi
f = imread('c:\image\kartun.tif');
g = gaussian(f, 150);
subplot(1,2,1);imshow(f), title('citra masukan')
subplot(1,2,2);imshow(g), title('citra keluaran')
Program garam dan merica dengan fungsi
=================================
function [g] = gamer(f, probabilitas)
if nargin < 2
probabilitas = 0.05; % Nilai bawaan
end;
[m, n] = size(f);
g = double(f);
for i=1 : m
for j=1 : n
nilai_acak = rand;
if nilai_acak <= probabilitas / 2
g(i,j) = 0;
elseif (nilai_acak > probabilitas / 2) && ...
(nilai_acak <= probabilitas)
g(i,j) = 255;
end
end
end
g = uint8(g);
end %akhir fungsi
f = imread('c:\image\boneka.tif');
g = gamer(f, 0.05);
subplot(1,2,1);imshow(f), title('citra masukan')
subplot(1,2,2);imshow(g), title('citra keluaran')
Program derau periodik dengan fungsi
=======================================================
function [G] = drperiodik(F, a, bx, by)
% DRPERIODIK Menghasilkan citra yang teklah ditambahi
% derau periodis.
%
% F = Citra berskala keabuan
% b = penegas derau (1, 2, 3, ...)
% ax dan ay menentukan kemiringan derau
if nargin < 4
a = 3; bx = 3; by = 5;
end
[m, n] = size(F);
for i=1:m
for j=1:n
X(i,j) = j;
Y(i,j) = i;
end
end
derau = a * sin(X/bx + Y/by) + 1;
G = uint8(double(F) + derau);
end %akhir fungsi
f = imread('c:\image\hotmaabu.jpg');
g = drperiodik(f, 10,3,4);
subplot(1,2,1);imshow(f), title('citra masukan')
subplot(1,2,2);imshow(g), title('citra keluaran')
program penghilangan derau rerata aritmatik dengan fungsi
=============================================
function [G] = filarithmean(F, ukuran)
% FILARITHMEAN Melakukan penghilangan derau dengan
% menggunakan filter rerata aritmetik
% F = Citra berskala keabuan
% ukuran = ukuran jendela
% G = Citra hasil pemrosesan
if nargin < 2
ukuran = 5;
end
[m, n] = size(F);
setengah = floor(ukuran / 2);
F = double(F);
G = zeros(m-2*setengah, n-2*setengah);
for i=1+setengah : m-setengah
for j=1+setengah: n-setengah
jum = 0;
for p = -setengah : setengah
for q = -setengah : setengah
jum = jum + F(i+p, j+q);
end
end
G(i-setengah, j-setengah) = jum / (ukuran * ukuran);
end
end
G = uint8(G);
end %akhir fungsi
F = imread('C:\Image\hotmagaussian.png');
G = filarithmean(F);
subplot(1,2,1);imshow(F)
subplot(1,2,2);imshow(G)
Program filter harmonik dengan fungsi
=======================================================
function [G] = filharmonik(F, ukuran)
% FILHARMONIK Melakukan penghilangan derau dengan
% menggunakan filter rerata harmonik
% F = Citra berskala keabuan
% ukuran = ukuran jendela
% G = Citra hasil pemrosesan
if nargin < 2
ukuran = 3;
end
[m, n] = size(F);
setengah = floor(ukuran / 2);
F = double(F);
G = zeros(m-2*setengah, n-2*setengah);
for i=1+setengah : m-setengah
for j=1+setengah: n-setengah
jum = 0;
for p = -setengah : setengah
for q = -setengah : setengah
jum = jum + 1 / F(i+p, j+q);
end
end
G(i-setengah, j-setengah) = (ukuran * ukuran) / jum;
end
end
G = uint8(G);
end %akhir fungsi
F = imread('C:\Image\hotmagaussian.png');
G = filharmonik(F, 5);
subplot(1,2,1);imshow(F)
subplot(1,2,2);imshow(G)
10. Pengambangan dan Ekstraksi Fitur Bentuk dan Kontur
Program pengambangan dwi-aras dengan fungsi sudah di modif
================================================================
function [g] = ambang(f, t)
% fungsi ambang Menentukan nilai ambang yang digunakan
% untuk melakukan pengambangan
% f = Citra Masukan berskala keabuan
% t = nilai ambang
% Keluaran: g = citra biner
[m, n] = size(f);
for i=1 : m
for j=1:n
if f(i,j) <= t
g(i,j) = 0;
else
g(i,j) = 1;
end
end
end
end %akhir fungsi
f = imread('c:\image\daunku.tif');
g = ambang(f, 100);
subplot(1,2,1);imshow(f)
subplot(1,2,2);imshow(1-g)
f = imread('c:\image\hotmaabuku.png');
g = ambang(f, 100);
subplot(1,2,1);imshow(f),title('citra Masukan')
subplot(1,2,2);imshow(1-g),title('citra Keluaran')
f = imread('c:\image\boneka.tif');
g = ambang(f, 100);
subplot(1,2,1);imshow(f),title('citra Masukan')
subplot(1,2,2);imshow(1-g),title('citra Keluaran')
Program pengambangan aras jamak dengan fungsi sudah di modif
================================================
function [g] = arasjamak(f, t1, t2)
% Pengambanan dengan dua nilai ambang
% f = Citra berskala keabuan
% t1 = nilai ambang bawah
% t2 = nilai ambang atas
% Masukan : f = citra biner
% Keluaran: g = citra biner
[m, n] = size(f);
for i=1 : m
for j=1:n
if f(i,j) <= t1 || f(i,j) >= t2
g(i,j) = 0;
else
g(i,j) = 1;
end
end
end
end %akhir fungsi
f = imread('c:\image\benda.png');
g = arasjamak(f, 50, 100);
subplot(1,2,1);imshow(f), title('citra masukan')
subplot(1,2,2);imshow(g), title('citra keluaran dengan t1=50 dan t2=100')
Program pengambangan adaftif dengan fungsi sudah dimodif
======================================================
function [g] = adapmean(f, w, c)
% adapmean Melakukan pengambangan adaptif dengan menggunakan mean.
% f = variabel Citra masukan berskala keabuan
% w = ukuran jendela
% c = nilai konstan
% g = variabel citra keluaran
if nargin == 0
disp('Penggunaan adapmean(Citra, ukuran, konstanta');
return;
end
if nargin == 1
w = 2;
c = 0;
end
if nargin == 2
c = 0;
end
% Lakukan pemrosesan citra
[m, n] = size(f);
delta = floor(w/2);
if c < 0
g = zeros(m, n); % Diasumsikan berlatarbelakang 0
else
g = ones(m, n); % Diasumsikan berlatarbelakang 1
end
f=double(f);
for y=1+delta : m-delta
for x=1+delta : n-delta
rerata = 0.0;
jum = w * w;
for p=1 : w
for q=1 : w
rerata = rerata + f(y-round(w/2)+p, ...
x-round(w/2)+q);
end
end
rerata = floor(rerata / jum) - c;
if f(y,x) >= rerata
g(y, x) = 1;
else
g(y, x) = 0;
end
end
end
end %akhir fungsi
f = imread('c:\image\daunku.tif');
g = adapmean(f, 2, 0);
subplot(1,2,1);imshow(f) , title('citra masukan')
subplot(1,2,2);imshow(g) , title('citra keluaran dengan w=2 dan c=0')
f = imread('c:\image\hotmaabuku.png');
g = adapmean(f, 2, 0);
subplot(1,2,1);imshow(f) , title('citra masukan')
subplot(1,2,2);imshow(g) , title('citra keluaran dengan w=2 dan c=0')
f = imread('c:\image\maryamah.png');
g = adapmean(f, 13, 15);
subplot(1,2,1);imshow(f) , title('citra masukan')
subplot(1,2,2);imshow(g) , title('citra keluaran dengan w=13 dan c=15')
program segmentasi warna denga fungsi
===========================
function [RGB] = segwarna(nama_file)
% SEGWARNA Digunakan untuk melakukan segmentasi citra
% berdasarkan warna
% F = citra berwarna
% Keluaran:
% G = citra berwarna yang telah disegmentasi
Img = imread(nama_file);
[tinggi, lebar, dim] = size(Img);
if dim < 3
error('Masukan harus berupa citra berwarna');
end
% Konversi ke HVS
[H,S,L] = RGBkeHSL(Img(:,:,1),Img(:,:,2),Img(:,:,3));
for y=1: tinggi
for x=1: lebar
h = H(y,x);
% Ubah warna
if h < 11
h = 0;
elseif h < 32
h = 21;
elseif h < 54
h = 43;
elseif h < 116
h = 85;
elseif h < 141
h = 128;
elseif h < 185
h = 170;
elseif h < 202
h = 191;
elseif h < 223
h = 213;
elseif h < 244
h = 234;
else
h = 0;
end
% Ubah komponen H
H(y,x) = h;
% Ubah komponen S
if S(y,x) >= 200
S(y,x) = 255;
elseif S(y,x) <= 20
S(y,x) = 0;
else
S(y,x) = 128;
end
% Ubah komponen L
if L(y,x) >= 200
L(y,x) = 255;
elseif L(y,x) <= 20
L(y,x) = 0;
else
L(y,x) = 128;
end
end
end
[R, G, B] = HSLkeRGB(H, S, L);
RGB(:,:,1) = R;
RGB(:,:,2) = G;
RGB(:,:,3) = B;
return
end %akhir fungsi
G = segwarna('C:\Image\bangunan.png'); imshow(G)
Program tanda tangan kontur dengan fungsi
==========================================
function [Jarak] = tandatangan(BW)
% TANDATANGAN Digunakan untuk memperoleh jarak-jarak antara
% piksel dalam batas objek dri citra biner BW
% Keluaran: Jarak - Berisi sejumlah pasangan Y, X
% yang menyatakan jarak
[m,n] = size(BW);
Batas = double(BW);
[jum, z] = size(Batas);
[pusat_x, pusat_y] = centroid(BW);
Jarak = zeros(1,m);
for p=1 : jum
Jarak(p) = sqrt((Batas(p,1)-pusat_y)^2 + ...
(Batas(p,2)-pusat_x)^2);
end
% Plot jarak
X = 1: p;
plot(X, Jarak);
end %akhir fungsi
img =imread('C:\Image\ikan-1.png');
jarak = tandatangan(img, 0.5);
function [G] = fiturFourier(F, n)
% FITURFOURIER Memperoleh fitur Fourier sebanyak n buah.
% Masukan:
% F : Deskriptor Fourier yang lengkap
% n : Jumlah fitur yang dikehendaki
% Keluaran:
% G : Deskriptor Fourier sebanyak n buah
jum = length(F);
if jum > n
K1 = fftshift(F);
delta = round((jum-n) / 2);
K2 = K1(1 + delta : n+delta);
G = ifftshift(K2);
else
G = F;
end
end %akhir fungsi
F=imread('c:\image\ikan-5.png')
G = fiturFourier(F, 20);
imshow(G)
function [G] = plotFD(F)
% PLOTFD Menampilkan kontur berdasarkan deskriptor Fourier.
% Masukan: F = Deskriptor Fourier
jum = length(F);
if jum > 0
G = ifft(F); % Transformasi balik
G = [G; G(1)]; % Tambahkan elemen pertama
plot(G);
axis off;
end
end %akhir fungsi
F=imread('c:\image\ikan-1.png');
G=plotFD(F) ;
imshow(G)
function [F] = perolehFD(Kontur)
% PEROLEHFD Memperoleh deskriptor Fourier berdasarkan
% kontur suatu bentuk.
% Masukan: Kontur = kontur objek
% Keluaran: F = deskriptor Fourier
jum = length(Kontur);
% Atur supaya jumlah elemen genap
if rem(jum, 2) == 1
Kontur = [Kontur; Kontur(1,:)];
end
% Peroleh bentuk Fourier kontur
K = Kontur(:, 2) - i * Kontur(:,1);
F = fft(K);
end %akhir fungsi
Img = im2bw(imread('C:\Image\ikan-5.png'), 0.5);
Kontur = inbound_tracing(Img);
F = perolehFD(Kontur);
length(F)
[m,n]=size(Img); m * n
function [] = amatiFD(berkas)
% AMATIFD Digunakan untuk mengamati deskriptor Fourier
% yang telah dinormalisasi.
Img = im2bw(imread(berkas),0.5);
K = inbound_tracing(Img);
F = perolehFD(K);
G = fiturFourier(F,30);
H = normalisasiFD(G);
bar(H);
end %akhir fungsi
amatiFD('C:\Image\guppi-1.png');
================================================================
function [g] = ambang(f, t)
% fungsi ambang Menentukan nilai ambang yang digunakan
% untuk melakukan pengambangan
% f = Citra Masukan berskala keabuan
% t = nilai ambang
% Keluaran: g = citra biner
[m, n] = size(f);
for i=1 : m
for j=1:n
if f(i,j) <= t
g(i,j) = 0;
else
g(i,j) = 1;
end
end
end
end %akhir fungsi
f = imread('c:\image\daunku.tif');
g = ambang(f, 100);
subplot(1,2,1);imshow(f)
subplot(1,2,2);imshow(1-g)
f = imread('c:\image\hotmaabuku.png');
g = ambang(f, 100);
subplot(1,2,1);imshow(f),title('citra Masukan')
subplot(1,2,2);imshow(1-g),title('citra Keluaran')
f = imread('c:\image\boneka.tif');
g = ambang(f, 100);
subplot(1,2,1);imshow(f),title('citra Masukan')
subplot(1,2,2);imshow(1-g),title('citra Keluaran')
Program pengambangan aras jamak dengan fungsi sudah di modif
================================================
function [g] = arasjamak(f, t1, t2)
% Pengambanan dengan dua nilai ambang
% f = Citra berskala keabuan
% t1 = nilai ambang bawah
% t2 = nilai ambang atas
% Masukan : f = citra biner
% Keluaran: g = citra biner
[m, n] = size(f);
for i=1 : m
for j=1:n
if f(i,j) <= t1 || f(i,j) >= t2
g(i,j) = 0;
else
g(i,j) = 1;
end
end
end
end %akhir fungsi
f = imread('c:\image\benda.png');
g = arasjamak(f, 50, 100);
subplot(1,2,1);imshow(f), title('citra masukan')
subplot(1,2,2);imshow(g), title('citra keluaran dengan t1=50 dan t2=100')
Program pengambangan adaftif dengan fungsi sudah dimodif
======================================================
function [g] = adapmean(f, w, c)
% adapmean Melakukan pengambangan adaptif dengan menggunakan mean.
% f = variabel Citra masukan berskala keabuan
% w = ukuran jendela
% c = nilai konstan
% g = variabel citra keluaran
if nargin == 0
disp('Penggunaan adapmean(Citra, ukuran, konstanta');
return;
end
if nargin == 1
w = 2;
c = 0;
end
if nargin == 2
c = 0;
end
% Lakukan pemrosesan citra
[m, n] = size(f);
delta = floor(w/2);
if c < 0
g = zeros(m, n); % Diasumsikan berlatarbelakang 0
else
g = ones(m, n); % Diasumsikan berlatarbelakang 1
end
f=double(f);
for y=1+delta : m-delta
for x=1+delta : n-delta
rerata = 0.0;
jum = w * w;
for p=1 : w
for q=1 : w
rerata = rerata + f(y-round(w/2)+p, ...
x-round(w/2)+q);
end
end
rerata = floor(rerata / jum) - c;
if f(y,x) >= rerata
g(y, x) = 1;
else
g(y, x) = 0;
end
end
end
end %akhir fungsi
f = imread('c:\image\daunku.tif');
g = adapmean(f, 2, 0);
subplot(1,2,1);imshow(f) , title('citra masukan')
subplot(1,2,2);imshow(g) , title('citra keluaran dengan w=2 dan c=0')
f = imread('c:\image\hotmaabuku.png');
g = adapmean(f, 2, 0);
subplot(1,2,1);imshow(f) , title('citra masukan')
subplot(1,2,2);imshow(g) , title('citra keluaran dengan w=2 dan c=0')
f = imread('c:\image\maryamah.png');
g = adapmean(f, 13, 15);
subplot(1,2,1);imshow(f) , title('citra masukan')
subplot(1,2,2);imshow(g) , title('citra keluaran dengan w=13 dan c=15')
program segmentasi warna denga fungsi
===========================
function [RGB] = segwarna(nama_file)
% SEGWARNA Digunakan untuk melakukan segmentasi citra
% berdasarkan warna
% F = citra berwarna
% Keluaran:
% G = citra berwarna yang telah disegmentasi
Img = imread(nama_file);
[tinggi, lebar, dim] = size(Img);
if dim < 3
error('Masukan harus berupa citra berwarna');
end
% Konversi ke HVS
[H,S,L] = RGBkeHSL(Img(:,:,1),Img(:,:,2),Img(:,:,3));
for y=1: tinggi
for x=1: lebar
h = H(y,x);
% Ubah warna
if h < 11
h = 0;
elseif h < 32
h = 21;
elseif h < 54
h = 43;
elseif h < 116
h = 85;
elseif h < 141
h = 128;
elseif h < 185
h = 170;
elseif h < 202
h = 191;
elseif h < 223
h = 213;
elseif h < 244
h = 234;
else
h = 0;
end
% Ubah komponen H
H(y,x) = h;
% Ubah komponen S
if S(y,x) >= 200
S(y,x) = 255;
elseif S(y,x) <= 20
S(y,x) = 0;
else
S(y,x) = 128;
end
% Ubah komponen L
if L(y,x) >= 200
L(y,x) = 255;
elseif L(y,x) <= 20
L(y,x) = 0;
else
L(y,x) = 128;
end
end
end
[R, G, B] = HSLkeRGB(H, S, L);
RGB(:,:,1) = R;
RGB(:,:,2) = G;
RGB(:,:,3) = B;
return
end %akhir fungsi
G = segwarna('C:\Image\bangunan.png'); imshow(G)
Program tanda tangan kontur dengan fungsi
==========================================
function [Jarak] = tandatangan(BW)
% TANDATANGAN Digunakan untuk memperoleh jarak-jarak antara
% piksel dalam batas objek dri citra biner BW
% Keluaran: Jarak - Berisi sejumlah pasangan Y, X
% yang menyatakan jarak
[m,n] = size(BW);
Batas = double(BW);
[jum, z] = size(Batas);
[pusat_x, pusat_y] = centroid(BW);
Jarak = zeros(1,m);
for p=1 : jum
Jarak(p) = sqrt((Batas(p,1)-pusat_y)^2 + ...
(Batas(p,2)-pusat_x)^2);
end
% Plot jarak
X = 1: p;
plot(X, Jarak);
end %akhir fungsi
img =imread('C:\Image\ikan-1.png');
jarak = tandatangan(img, 0.5);
function [G] = fiturFourier(F, n)
% FITURFOURIER Memperoleh fitur Fourier sebanyak n buah.
% Masukan:
% F : Deskriptor Fourier yang lengkap
% n : Jumlah fitur yang dikehendaki
% Keluaran:
% G : Deskriptor Fourier sebanyak n buah
jum = length(F);
if jum > n
K1 = fftshift(F);
delta = round((jum-n) / 2);
K2 = K1(1 + delta : n+delta);
G = ifftshift(K2);
else
G = F;
end
end %akhir fungsi
F=imread('c:\image\ikan-5.png')
G = fiturFourier(F, 20);
imshow(G)
function [G] = plotFD(F)
% PLOTFD Menampilkan kontur berdasarkan deskriptor Fourier.
% Masukan: F = Deskriptor Fourier
jum = length(F);
if jum > 0
G = ifft(F); % Transformasi balik
G = [G; G(1)]; % Tambahkan elemen pertama
plot(G);
axis off;
end
end %akhir fungsi
F=imread('c:\image\ikan-1.png');
G=plotFD(F) ;
imshow(G)
function [F] = perolehFD(Kontur)
% PEROLEHFD Memperoleh deskriptor Fourier berdasarkan
% kontur suatu bentuk.
% Masukan: Kontur = kontur objek
% Keluaran: F = deskriptor Fourier
jum = length(Kontur);
% Atur supaya jumlah elemen genap
if rem(jum, 2) == 1
Kontur = [Kontur; Kontur(1,:)];
end
% Peroleh bentuk Fourier kontur
K = Kontur(:, 2) - i * Kontur(:,1);
F = fft(K);
end %akhir fungsi
Img = im2bw(imread('C:\Image\ikan-5.png'), 0.5);
Kontur = inbound_tracing(Img);
F = perolehFD(Kontur);
length(F)
[m,n]=size(Img); m * n
function [] = amatiFD(berkas)
% AMATIFD Digunakan untuk mengamati deskriptor Fourier
% yang telah dinormalisasi.
Img = im2bw(imread(berkas),0.5);
K = inbound_tracing(Img);
F = perolehFD(K);
G = fiturFourier(F,30);
H = normalisasiFD(G);
bar(H);
end %akhir fungsi
amatiFD('C:\Image\guppi-1.png');
11. Aplikasi Temu Kembali
Program Temu kembali citra masih debug
=========================================
% DBFITUR Berguna untuk menghitung fitur
% menggunakan momen Zernike dan kemudian
% menyimpannya ke dalaa file .MAT
% Citra yang akan menjadi acuan
Citra{1} = 'C:\Image\ikan-4.png';
Citra{2} = 'C:\Image\ikan-2.png';
Citra{3} = 'C:\Image\ikan-3.png';
Citra{4} = 'C:\Image\ikan-1.png';
Citra{5} = 'C:\Image\guppi-4.png';
Citra{6} = 'C:\Image\guppi-2.png';
Citra{7} = 'C:\Image\guppi-3.png';
Citra{8} = 'C:\Image\guppi-1.png';
% Proses menghitung momen Zernike
Fitur = [];
for i=1 : length(Citra)
disp(sprintf('Menghitung fitur citra %s', Citra{i}));
Image = im2bw(imread(Citra{i}), 0.5);
Z.momen = zermoment(Image, 10);
Z.nama = Citra(i);
Fitur(i).momen = Z.momen;
Fitur(i).nama = Z.nama;
end
% Simpan ke file dbfitur.mat
save('dbfitur', 'Fitur');
clear Citra Fitur;
function queryzer2(berkas)
% QUERYZER2 Berguna untuk melakukan query dengan
% menggunakan momen Zernike dengan membaca
% data acuan dari file dbfitur.
% load dbfitur : Memuat database fitur
% Proses momen Zernike dan hitung jarak City-block
Query = im2bw(imread(berkas), 0.5);
ZQuery = zermoment(Query, 10);
Hasil = [];
jumFitur = length(Fitur(1).momen);
for i=1 : length(Fitur)
nama = Fitur(i).nama;
jarak = 0;
for j=1 : jumFitur
jarak = jarak + abs(ZQuery(j) - Fitur(i).momen(j));
end
Hasil(i).nama = Fitur(i).nama;
Hasil(i).jarak = jarak;
end
% Urutkan data pada array Hasil
jum = length(Hasil);
for p = 2: jum
x = Hasil(p);
% Sisipkan x ke dalam data[1..p-1]
q = p - 1;
ketemu = 0;
while ((q >= 1) && (~ketemu))
if (x.jarak < Hasil(q).jarak)
Hasil(q+1) = Hasil(q);
q = q - 1;
else
ketemu = 1;
end
Hasil(q+1) = x;
end
end
% Tampilkan hasil secara visual
disp('Hasil pencocokan dapat dilihat pada jendela gambar');
subplot(3,3,1);
G = imread(berkas); imshow(G);
title('Query');
for i=1 : jum
subplot(3,3, i+1);
G = imread(char(Hasil(i).nama)); imshow(G);
title(num2str(Hasil(i).jarak));
end
end %akhir fungsi
queryzer2('C:\Image\ikan-1.png')
=========================================
% DBFITUR Berguna untuk menghitung fitur
% menggunakan momen Zernike dan kemudian
% menyimpannya ke dalaa file .MAT
% Citra yang akan menjadi acuan
Citra{1} = 'C:\Image\ikan-4.png';
Citra{2} = 'C:\Image\ikan-2.png';
Citra{3} = 'C:\Image\ikan-3.png';
Citra{4} = 'C:\Image\ikan-1.png';
Citra{5} = 'C:\Image\guppi-4.png';
Citra{6} = 'C:\Image\guppi-2.png';
Citra{7} = 'C:\Image\guppi-3.png';
Citra{8} = 'C:\Image\guppi-1.png';
% Proses menghitung momen Zernike
Fitur = [];
for i=1 : length(Citra)
disp(sprintf('Menghitung fitur citra %s', Citra{i}));
Image = im2bw(imread(Citra{i}), 0.5);
Z.momen = zermoment(Image, 10);
Z.nama = Citra(i);
Fitur(i).momen = Z.momen;
Fitur(i).nama = Z.nama;
end
% Simpan ke file dbfitur.mat
save('dbfitur', 'Fitur');
clear Citra Fitur;
function queryzer2(berkas)
% QUERYZER2 Berguna untuk melakukan query dengan
% menggunakan momen Zernike dengan membaca
% data acuan dari file dbfitur.
% load dbfitur : Memuat database fitur
% Proses momen Zernike dan hitung jarak City-block
Query = im2bw(imread(berkas), 0.5);
ZQuery = zermoment(Query, 10);
Hasil = [];
jumFitur = length(Fitur(1).momen);
for i=1 : length(Fitur)
nama = Fitur(i).nama;
jarak = 0;
for j=1 : jumFitur
jarak = jarak + abs(ZQuery(j) - Fitur(i).momen(j));
end
Hasil(i).nama = Fitur(i).nama;
Hasil(i).jarak = jarak;
end
% Urutkan data pada array Hasil
jum = length(Hasil);
for p = 2: jum
x = Hasil(p);
% Sisipkan x ke dalam data[1..p-1]
q = p - 1;
ketemu = 0;
while ((q >= 1) && (~ketemu))
if (x.jarak < Hasil(q).jarak)
Hasil(q+1) = Hasil(q);
q = q - 1;
else
ketemu = 1;
end
Hasil(q+1) = x;
end
end
% Tampilkan hasil secara visual
disp('Hasil pencocokan dapat dilihat pada jendela gambar');
subplot(3,3,1);
G = imread(berkas); imshow(G);
title('Query');
for i=1 : jum
subplot(3,3, i+1);
G = imread(char(Hasil(i).nama)); imshow(G);
title(num2str(Hasil(i).jarak));
end
end %akhir fungsi
queryzer2('C:\Image\ikan-1.png')
Sekian Terima Kasih, Semoga Bermanfaat :)
Tidak ada komentar:
Posting Komentar