Algoritma Edge Detection Pada Sebuah Gambar Berbasis Fuzzy Logic

Pada suatu hari Ali Hasyim ingin membuat sebuah aplikasi algoritma fuzzy logic untuk memenuhi tugas kuliahnya. Dia menemukan sebuah jurnal berikut ini http://oaj.unsri.ac.id/files/wwwijcaonline/journal/number22/pxc387675.pdf

Lalu dia mencoba membuat kode matlabnya. Berikut kode matlab yang telah dibuat.


clc
clear all
vid = videoinput('winvideo',1);
while true
input = getsnapshot(vid);
%input = imread('tubes.bmp');
input = imresize(input,[75 150]);

ukuran = size(input);
baris = ukuran(1);
kolom = ukuran(2);

for bar=2:baris
	for kol=2:kolom
		%===================
		%super fuzzy coming
		%===================
		p(1) = double(input(bar-1, kol-1));
		p(2) = double(input(bar-1, kol));
		p(3) = double(input(bar, kol-1));
		p(4) = double(input(bar, kol));

		for index=1:4
			nilai_black(index) = (255-p(index))/(255);
			nilai_white(index) = p(index)/255;
		end

		for index=1:4
			if nilai_black(index)>nilai_white(index)
				rule(index)=1; %black
				max_val(index) = nilai_black(index);
			else
				rule(index)=2; %white
				max_val(index) = nilai_white(index);
			end
		end

		P4Out_black=0;
		P4Out_edge=0;
		P4Out_white=0;

		if (rule(1) == 1) & (rule(2) == 1) & (rule(3) == 1) & (rule(4) == 1)
			P4Out_black= min([max_val(1) max_val(2) max_val(3) max_val(4)]); %black
		end
		if(rule(1) == 1) & (rule(2) == 1) & (rule(3) == 1) & (rule(4) == 2)
			P4Out_edge = min([max_val(1) max_val(2) max_val(3) max_val(4)]); %edge
		end
		if(rule(1) == 1) & (rule(2) == 1) & (rule(3) == 2) & (rule(4) == 1)
			buff_P4Out_edge = min([max_val(1) max_val(2) max_val(3) max_val(4)]);
			if(buff_P4Out_edge>P4Out_edge) 
				P4Out_edge = buff_P4Out_edge;
			end
			%edge
		end
		if(rule(1) == 1) & (rule(2) == 1) & (rule(3) == 2) & (rule(4) == 2)
			buff_P4Out_edge = min([max_val(1) max_val(2) max_val(3) max_val(4)]);
			if(buff_P4Out_edge>P4Out_edge) 
				P4Out_edge = buff_P4Out_edge;
			end
			%edge
		end
		if(rule(1) == 1) & (rule(2) == 2) & (rule(3) == 1) & (rule(4) == 1)
			buff_P4Out_edge = min([max_val(1) max_val(2) max_val(3) max_val(4)]);
			if(buff_P4Out_edge>P4Out_edge) 
				P4Out_edge = buff_P4Out_edge;
			end
			%edge
		end
		if(rule(1) == 1) & (rule(2) == 2) & (rule(3) == 1) & (rule(4) == 2)
			buff_P4Out_edge = min([max_val(1) max_val(2) max_val(3) max_val(4)]);
			if(buff_P4Out_edge>P4Out_edge) 
				P4Out_edge = buff_P4Out_edge;
			end
			%edge
		end
		if(rule(1) == 1) & (rule(2) == 2) & (rule(3) == 2) & (rule(4) == 1)
			buff_P4Out_edge = min([max_val(1) max_val(2) max_val(3) max_val(4)]);
			if(buff_P4Out_edge>P4Out_edge) 
				P4Out_edge = buff_P4Out_edge;
			end
			%edge
		end
		if(rule(1) == 1) & (rule(2) == 2) & (rule(3) == 2) & (rule(4) == 2)
			P4Out_white = min([max_val(1) max_val(2) max_val(3) max_val(4)]); %white
			%white
		end
		if(rule(1) == 2) & (rule(2) == 1) & (rule(3) == 1) & (rule(4) == 1)
			buff_P4Out_edge = min([max_val(1) max_val(2) max_val(3) max_val(4)]);
			if(buff_P4Out_edge>P4Out_edge) 
				P4Out_edge = buff_P4Out_edge;
			end
			%edge
		end
		if(rule(1) == 2) & (rule(2) == 1) & (rule(3) == 1) & (rule(4) == 2)
			buff_P4Out_edge = min([max_val(1) max_val(2) max_val(3) max_val(4)]);
			if(buff_P4Out_edge>P4Out_edge) 
				P4Out_edge = buff_P4Out_edge;
			end
			%edge
		end
		if(rule(1) == 2) & (rule(2) == 1) & (rule(3) == 2) & (rule(4) == 1)
			buff_P4Out_edge = min([max_val(1) max_val(2) max_val(3) max_val(4)]);
			if(buff_P4Out_edge>P4Out_edge) 
				P4Out_edge = buff_P4Out_edge;
			end
			%edge
		end
		if(rule(1) == 2) & (rule(2) == 1) & (rule(3) == 2) & (rule(4) == 2)
			buff_P4Out_edge = min([max_val(1) max_val(2) max_val(3) max_val(4)]);
			if(buff_P4Out_edge>P4Out_edge) 
				P4Out_edge = buff_P4Out_edge;
			end
			%edge
		end
		if(rule(1) == 2) & (rule(2) == 2) & (rule(3) == 1) & (rule(4) == 1)
			buff_P4Out_edge = min([max_val(1) max_val(2) max_val(3) max_val(4)]);
			if(buff_P4Out_edge>P4Out_edge) 
				P4Out_edge = buff_P4Out_edge;
			end
			%edge
		end
		if(rule(1) == 2) & (rule(2) == 2) & (rule(3) == 1) & (rule(4) == 2)
			buff_P4Out_edge = min([max_val(1) max_val(2) max_val(3) max_val(4)]);
			if(buff_P4Out_edge>P4Out_edge) 
				P4Out_edge = buff_P4Out_edge;
			end
			%edge
		end
		if(rule(1) == 2) & (rule(2) == 2) & (rule(3) == 2) & (rule(4) == 1)
			buff_P4Out_edge = min([max_val(1) max_val(2) max_val(3) max_val(4)]);
			if(buff_P4Out_edge>P4Out_edge) 
				P4Out_edge = buff_P4Out_edge;
			end
			%edge
		end
		if(rule(1) == 2) & (rule(2) == 2) & (rule(3) == 2) & (rule(4) == 2)
			buff_P4Out_white = min([max_val(1) max_val(2) max_val(3) max_val(4)]);
			if(buff_P4Out_white>P4Out_white) 
				P4Out_white = buff_P4Out_white;
			end
			%white
		end

		P4Out_final_value = ((P4Out_black*3)+(P4Out_edge*133)+(P4Out_white*252))/(P4Out_black+P4Out_edge+P4Out_white);
		P4Out_final_warna = uint8(round(P4Out_final_value));
		%===================
		im_out(bar,kol) =P4Out_final_warna; 
	end
end

imshow(im_out);
end

Berikut hasilnya.

Jika kita ingin mendapatkan murni edge/tepinya saja, maka kita rekayasa kode defuzzyfikasinya pada konstanta putih= 3.

P4Out_final_value = ((P4Out_black*3)+(P4Out_edge*133)+(P4Out_white*3))/(P4Out_black+P4Out_edge+P4Out_white);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s