Berikut adalah contoh fungsi Matlab untuk operasi penghitungan gradien orde satu:
I = doubleimreadcameraman.tif; Gradien orde satu pada arah horizontal
gx = [-1 1]; Ix = conv2I,gx,same;
Gradien orde satu pada arah vertikal gy = [-1;1];
Iy = conv2I,gy,same;
Magnitudo gradien J = sqrtIx.2+Iy.2;
Gambar hasil figure,imagescI ,colormapgray,colorbarvert;
figure,imagescIx,colormapgray,colorbarvert; figure,imagescIy,colormapgray,colorbarvert;
figure,imagescJ ,colormapgray,colorbarvert;
Dari operator gradien konvensional di atas, dapat diturunkan berbagai operator gradien berikut:
1.1 Operator Selisih Terpusat
Operator selisih terpusat juga dikenal sebagai
Centered Difference Edge Detector Mask
, dan dinyatakan sebagai kernel:
I = doubleimreadcameraman.tif; Konvolusi dengan operator selisih terpusat
d1x = [-1 0 1]; d1y = [-1;0;1];
Ix = conv2I,d1x,same; Iy = conv2I,d1y,same;
J = sqrtIx.2+Iy.2; Gambar Hasil
figure,imagescI ,colormapgray,colorbarvert; figure,imagescIx,colormapgray,colorbarvert;
figure,imagescIy,colormapgray,colorbarvert; figure,imagescJ ,colormapgray,colorbarvert;
1.2 Operator Roberts
Operator Roberts memiliki ukuran kernel sebesar 2×2, yang direpresentasikan sebagai:
Contoh perintah menggunakan operator Roberts:
I = doubleimreadcameraman.tif; Konvolusi dengan operator Roberts
robertshor = [0 1; -1 0]; robertsver = [1 0; 0 -1];
Ix = conv2I,robertshor,same; Iy = conv2I,robertsver,same;
J = sqrtIx.2+Iy.2;
Gambar Hasil figure,imagescI ,colormapgray,colorbarvert;
figure,imagescIx,colormapgray,colorbarvert; figure,imagescIy,colormapgray,colorbarvert;
figure,imagescJ ,colormapgray,colorbarvert;
Contoh perintah menggunakan operator Roberts matlab
toolbox
1
:
I = imreadcameraman.tif; J = edgeI,roberts;
figure,imagescI,colormapgray,colorbarvert; figure,imagescJ,colormapgray,colorbarvert;
1.3 Operator Prewitt
Contoh perintah menggunakan operator Prewitt:
I = doubleimreadcameraman.tif; Konvolusi dengan operator Prewitt
prewitthor = [-1 0 1; -1 0 1; -1 0 1]; prewittver = [-1 -1 -1; 0 0 0; 1 1 1];
Ix = conv2I,prewitthor,same; Iy = conv2I,prewittver,same;
J = sqrtIx.2+Iy.2; Gambar Hasil
figure,imagescI ,colormapgray,colorbarvert; figure,imagescIx,colormapgray,colorbarvert;
figure,imagescIy,colormapgray,colorbarvert; figure,imagescJ ,colormapgray,colorbarvert;
Contoh perintah menggunakan operator Prewitt matlab
toolbox
:
I = imreadcameraman.tif; J = edgeI,prewitt;
figure,imagescI,colormapgray,colorbarvert; figure,imagescJ,colormapgray,colorbarvert;
1
Perintah
edge
pada toolbox Matlab untuk mensimulasikan operator Prewitt, Roberts, Sobel, dan lainnya memiliki konsep dasar yang sama dengan operasi konvolusi kernel setiap operator secara manual. Bedanya,
perintah
edge
menambahkan suatu skema
thresholding
secara otomatis, sehingga dihasilkan citra keluaran yang bersifat biner bernilai 0 atau 1.
1.4 Operator Sobel