T~ Hanya seorang blogger nubie yang suka nulis asal-asalan ...

Script Matlab Deteksi Tepi, Kontur, Pelabelan Objek Citra Biner Terbaru

Script Matlab Deteksi Tepi, Kontur, Pelabelan Objek Citra Biner

1. Script Matlab Deteksi Tepi dengan Model Canny

Ganti nama file landing.png dengan nama file citra biner kalian masing-masing. Lalu, jalankan langsung script matlab nya. Hasilnya seperti digambar bawah ini;

Hasil citra biner setelah menggunakan script Deteksi Tepi model Canny
clear all;
clc;
 
%Input image
img = imread ('landing.png');
%Show input image
figure, imshow(img);
img = rgb2gray(img);
img = double (img);
 
%Value for Thresholding
T_Low = 0.075;
T_High = 0.175;
 
%Gaussian Filter Coefficient
B = [2, 4, 5, 4, 2; 4, 9, 12, 9, 4;5, 12, 15, 12, 5;4, 9, 12, 9, 4;2, 4, 5, 4, 2 ];
B = 1/159.* B;
 
%Convolution of image by Gaussian Coefficient
A=conv2(img, B, 'same');
 
%Filter for horizontal and vertical direction
KGx = [-1, 0, 1; -2, 0, 2; -1, 0, 1];
KGy = [1, 2, 1; 0, 0, 0; -1, -2, -1];
 
%Convolution by image by horizontal and vertical filter
Filtered_X = conv2(A, KGx, 'same');
Filtered_Y = conv2(A, KGy, 'same');
 
%Calculate directions/orientations
arah = atan2 (Filtered_Y, Filtered_X);
arah = arah*180/pi;
 
pan=size(A,1);
leb=size(A,2);
 
%Adjustment for negative directions, making all directions positive
for i=1:pan
    for j=1:leb
    if (arah(i,j)<0) 
    arah(i,j)=360+arah(i,j);
    end;
    end;
end;
 
arah2=zeros(pan, leb);
 
%Adjusting directions to nearest 0, 45, 90, or 135 degree
for i = 1  : pan
    for j = 1 : leb
    if ((arah(i, j) >= 0 ) && (arah(i, j) < 22.5) || (arah(i, j) >= 157.5) && (arah(i, j) < 202.5) || (arah(i, j) >= 337.5) && (arah(i, j) <= 360))
    arah2(i, j) = 0;
    elseif ((arah(i, j) >= 22.5) && (arah(i, j) < 67.5) || (arah(i, j) >= 202.5) && (arah(i, j) < 247.5))
    arah2(i, j) = 45;
    elseif ((arah(i, j) >= 67.5 && arah(i, j) < 112.5) || (arah(i, j) >= 247.5 && arah(i, j) < 292.5))
    arah2(i, j) = 90;
    elseif ((arah(i, j) >= 112.5 && arah(i, j) < 157.5) || (arah(i, j) >= 292.5 && arah(i, j) < 337.5))
    arah2(i, j) = 135;
    end;
    end;
end;
 
figure, imagesc(arah2); colorbar;
 
%Calculate magnitude
magnitude = (Filtered_X.^2) + (Filtered_Y.^2);
magnitude2 = sqrt(magnitude);
 
BW = zeros (pan, leb);
 
%Non-Maximum Supression
for i=2:pan-1
    for j=2:leb-1
    if (arah2(i,j)==0)
    BW(i,j) = (magnitude2(i,j) == max([magnitude2(i,j), magnitude2(i,j+1), magnitude2(i,j-1)]));
    elseif (arah2(i,j)==45)
    BW(i,j) = (magnitude2(i,j) == max([magnitude2(i,j), magnitude2(i+1,j-1), magnitude2(i-1,j+1)]));
    elseif (arah2(i,j)==90)
    BW(i,j) = (magnitude2(i,j) == max([magnitude2(i,j), magnitude2(i+1,j), magnitude2(i-1,j)]));
    elseif (arah2(i,j)==135)
    BW(i,j) = (magnitude2(i,j) == max([magnitude2(i,j), magnitude2(i+1,j+1), magnitude2(i-1,j-1)]));
    end;
    end;
end;
 
BW = BW.*magnitude2;
figure, imshow(BW);
 
%Hysteresis Thresholding
T_Low = T_Low * max(max(BW));
T_High = T_High * max(max(BW));
 
T_res = zeros (pan, leb);
 
for i = 1  : pan
    for j = 1 : leb
    if (BW(i, j) < T_Low)
    T_res(i, j) = 0;
    elseif (BW(i, j) > T_High)
    T_res(i, j) = 1;
    %Using 8-connected components
    elseif ( BW(i+1,j)>T_High || BW(i-1,j)>T_High || BW(i,j+1)>T_High || BW(i,j-1)>T_High || BW(i-1, j-1)>T_High || BW(i-1, j+1)>T_High || BW(i+1, j+1)>T_High || BW(i+1, j-1)>T_High)
    T_res(i,j) = 1;
    end;
    end;
end;
 
edge_final = uint8(T_res.*255);
%Show final edge detection result
figure, imshow(edge_final);

2. Script Matlab Kontur

Buat function baru dengan nama kontur.m . Lalu, jalankan langsung function tersebut.

%% Create some matrix and the vectors.
a = 36;
b = a;
data = fspecial('gaussian', b, b/5);
x = linspace(0,1,b);
y = linspace(5,20,a);
m = mean(mean(data));
%% Plot the data in a imagesc and contour courves at mm, mm/2 and mm*2 using contour.
subplot(1,2,1)
imagesc(x,y,data)
pbaspect([1 1 1])
hold on;
contour(x,y,data,[m m/2 m*2],'r','linewidth',3);
xlabel('x')
ylabel('y')
title('Contour')
%% Plot the data in a imagesc and contour courves at mm, mm/2 and mm*2 using contourEdges.
subplot(1,2,2)
imagesc(x,y,data)
pbaspect([1 1 1])
hold on;
contourEdges(x,y,data,[m m/2 m*2],'r','linewidth',3);
xlabel('x')
ylabel('y')
title('ContourEdges')
%%
set(findall(0,'type','axes'),'FontSize',20)

3. Script Matlab Pelabelan Objek

Cara menggunakan script ini pada matlab sebagai berikut;

  • Buat function baru, masukkan script dibawah ini dan beri nama pelabelan.m
  • Sebagai contoh, saya akan memasukkan input seperti ini,

>> A = [
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0
0 1 1 0 1 1 0 0 0 1 1 1 0 0 0 0 0 1 1 0 0 0 1 0
0 1 1 0 1 1 0 0 1 1 1 1 1 0 0 0 0 0 1 1 0 0 1 0
0 1 1 1 1 1 0 0 0 1 1 1 0 0 0 0 0 0 0 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 0
0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 0 1 1 0 0 1 0
0 0 0 0 0 0 0 1 1 1 1 1 0 0 0 0 0 1 1 0 0 0 1 0
0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0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6 6 6 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6 6 6 6 6 0 0 0
7 7 7 7 0 0 0 0 0 0 0 0 0 0 0 0 0 6 6 6 0 0 0 0
7 7 7 7 7 7 7 0 0 0 0 0 0 0 0 0 0 0 6 0 0 0 0 0
7 7 7 7 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

function G = pelabelan(F)
% Memberi label pada area di dalam citra biner F
% dengan menggunakan 4-ketetanggan
% Hasil berupa citra G
% Bentuk Antrean awal
Maks_antre = 50000;
Antrean = cell(Maks_antre,1);
depan = 1;
belakang = 1;
G = double(F); % Agar bisa diisi dengan nilai selain 0 dan 1
[m, n] = size(G);
label = 2;
for i=1 : m
 for j=1 : n
 if G(i, j) == 1
 % Kosongkan antrean
 depan = 1;
 belakang = 1;

 % Bentuk simpul dan masukkan ke dalam antrean
 simpul.y = i;
 simpul.x = j;
 if belakang == Maks_antre
 if depan == 1
 error('Kapasitas antrian penuh');
 else
 Antrean{obj.belakang} = simpul;
belakang = 1;
 end
 else
 if belakang + 1 == depan
 error('Kapasitas antrian penuh');
 else
 Antrean{belakang} = simpul;
belakang = belakang + 1;
 end
 end
 while belakang ~= depan % Selama antrean tidak kosong
 %Ambil dan hapus data pada Antrean

simpul = Antrean{depan};
 if depan == 50000
 depan = 1;
 else
 depan = depan + 1;
 end

 if simpul.x > 0 && simpul.x <= n && ...
 simpul.y > 0 && simpul.y <= m && ...
G(simpul.y, simpul.x) == 1
 G(simpul.y, simpul.x) = label;
 x = simpul.x; y = simpul.y;

 simpul.y = y-1; simpul.x = x;
% Sisipkan ke Antrean
if belakang == Maks_antre
 if depan == 1
 error('Kapasitas antrian penuh');
 else
 Antrean{obj.belakang} = simpul;
belakang = 1;
 end
 else
 if belakang + 1 == depan
 error('Kapasitas antrian penuh');
 else
 Antrean{belakang} = simpul;
belakang = belakang + 1;
 end
 end

 simpul.y = y+1; simpul.x = x;
% Sisipkan ke Antrean
if belakang == Maks_antre
 if depan == 1
 error('Kapasitas antrian penuh');
 else
 Antrean{obj.belakang} = simpul;
belakang = 1;
 end
 else
 if belakang + 1 == depan
 error('Kapasitas antrian penuh');
 else
 Antrean{belakang} = simpul;
belakang = belakang + 1;
 end
 end

 simpul.y = y; simpul.x = x-1;
% Sisipkan ke Antrean
if belakang == Maks_antre
 if depan == 1
 error('Kapasitas antrian penuh');
 else
 Antrean{obj.belakang} = simpul;
belakang = 1;
 end
 else
 if belakang + 1 == depan
 error('Kapasitas antrian penuh');
 else
 Antrean{belakang} = simpul;
belakang = belakang + 1;
 end
 end
simpul.y = y; simpul.x = x+1;
% Sisipkan ke Antrean
if belakang == Maks_antre
 if depan == 1
 error('Kapasitas antrian penuh');
 else
 Antrean{obj.belakang} = simpul;
belakang = 1;
 end
 else
 if belakang + 1 == depan
 error('Kapasitas antrian penuh');
else
 Antrean{belakang} = simpul;
 belakang = belakang + 1;
 end
 end
 end
 end

 label = label + 1;
 end
 end
end

Sumber:

  • https://www.mathworks.com/matlabcentral/fileexchange/46859-canny-edge-detection
  • https://www.mathworks.com/matlabcentral/fileexchange/69633-contouredges
  • https://www.mathworks.com/matlabcentral/fileexchange?q=contour+edge
T~ Hanya seorang blogger nubie yang suka nulis asal-asalan ...

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *