SEGMENTASI CITRA - andyusuf-informatika

New Post

Minggu, 30 Juli 2017

SEGMENTASI CITRA

Segmentasi Citra

Segmentasi citra merupakan  proses yang ditujukan untuk mendapatkan objek-objekyang terkandung di dalam citra atau membagi citra ke dalam beberapa daerah dengan setiap objek atau daerah memiliki kemiripan atribut. Pada  citra yang mengandung hanya satu objek, objek dibedakan dari latarbelakangnya. Contoh ditunjukkan pada Gambar10.1. Pada citra yang mengandung sejumlah objek, proses untuk memilah semua objek tentu saja lebih kompleks.


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)


Salah satu hasil dari coding diatas :

Terima Kasih dan Semangat Belajar

Tidak ada komentar:

Posting Komentar