Cari Blog Ini

Selasa, 01 Februari 2011

Tutorial Machine Vision 2

Sudah cukup lama liburan dan tidak membuat entri, akhirnya ada waktu juga untuk membuat entri. Selanjutnya pada tutorial machine vision 2 ini saya akan memberi tahu bagaimana mengolah image dan menampilkan hasilnya yang telah diolah. Pada kesempatan ini juga, saya akan memberi bagaimana mengolah image tanpa API matlab, walaupun ada yang menggunakan API matlab seperti pada proses Fast Fourier Transform.

Tutorial 3 Membuat Histogram dan Menampilkan Image
 
a=imread('jeby.jpg');
subplot(2,2,1),imshow(a),title('RGB');
b=RGB2Gray(a);
i=histeq(b);
subplot(2,2,2), imshow(i),title('Histogram Equalizer');
subplot(2,2,3), imhist(i),title('Histogram');
background = imopen(i,strel('disk',5));
subplot(2,2,4), imshow(background),title('strel');






Tutorial 4 Mengolah Data Histogram


a=imread('jeby.jpg')
b=rgb2gray(a);
subplot(4,3,1), imhist(b),title('a')
subplot(4,3,2), imhist(b,5),title('b')
subplot(4,3,3), imhist(b,1),title('c')
subplot(4,3,4), imhist(b,10),title('d')
axis([0 255 0 15000])
set(gca,'xtick',0:50:255)
set(gca,'ytick',0:2000:15000)
subplot(4,3,5), imhist(b),title('e')
p=imhist(b)/numel(b);
h1=b(1:10:256);
horz=1:10:256;
bar(horz,h1)
axis([0 255 0 15000])
set(gca,'xtick',0:50:255)
set(gca,'ytick',0:2000:15000)
g=histeq(b);
subplot(4,3,6),imshow(b),title('f')
subplot(4,3,7),imhist(b),title('g')
ylim('auto')
g=histeq(b,256)
subplot(4,3,8),imshow(g),title('h')
subplot(4,3,9),imshow(a),title('i')
subplot(4,3,10),imhist(g),title('j'), subplot(4,3,11), imhist(b),title('k')
ylim('auto')



Tutorial 5 Membuat Fast Fourier Transform


a=imread('jeboy.jpg');
B=0.2989*a(:,:,1)+0.587*a(:,:,2)+0.114*a(:,:,3);subplot(2,2,1),imshow(B)
b=fft2(B);
B=fftshift(b);
subplot(2,2,2),imshow(log(abs(B)),[])
[x y]=size(B);
H=zeros(x,y);
H(.5*x-10:.5*x+10,.5*y-10:.5*y+10)=1;
subplot(2,2,3),imshow(H,[])
D=B.*H;
D=fftshift(D);
E=real(ifft2(D));
subplot(2,2,4),imshow(E,[])





Tutorial 6 Membuat Image Yang Telah di Olah

Pada tutorila ke-6 ini saya akan menggunakan Binary, Erosion, Dilation, Edge, Salt and Pepper

%%Algorithma
clear, close all

%%memanggil gambar
Z=imread('robofish.jpg');
subplot(3,4,1), imshow(Z),title('Gambar asli'),pixval on;
tic
%%mengubah RGB menjadi Grayscale
G=0.2989*Z(:,:,1)+0.587*Z(:,:,2)+0.114*Z(:,:,3);
subplot(3,4,2),imshow(G), title('Gambar Grayscale'),pixval on;

%%mencari threshold
f=imhist(G);
h=[0:255];
thres=(h*f)/(sum(f));
H=G;
[k l]=size(H);
for i=1:1:k
    for j=1:1:l
        if H(i,j)>thres
            H(i,j)=255;
        else
            H(i,j)=0;
        end
    end
end
subplot(3,4,3),imshow(H), title('Grayscale Threshold'),pixval on;

%%mengubah grayscale menjadi binary
B=im2bw(G);
subplot(3,4,4),imshow(B), title('Gambar Binary'),pixval on;

%%menggunakan errosion
[m n]=size(B);
Bb=B;
d=m-3+1;
e=n-3+1;

for i=1:1:d
    for j=1:1:e
        if (sum(sum(B(i:3+(i-1),j:3+(j-1))))-sum(B(i+1,j+1)))<8
            Bb(i+1,j+1)=0;
        else
            Bb(i+1,j+1)=B(i+1,j+1);
        end
        j=0;
    end
end
subplot(3,4,5),imshow(Bb),title('Gambar Errosion'),pixval on


%%menggunakan dilation

Dd=B;
for i=1:1:d
    for j=1:1:e
        if (sum(sum(B(i:3+(i-1),j:3+(j-1))))-sum(B(i+1,j+1)))>0
            Dd(i+1,j+1)=1;
        else
            Dd(i+1,j+1)=B(i+1,j+1);
        end
        j=0;
    end
end
subplot(3,4,6),imshow(Dd),title('Gambar Dilation'),pixval on


%%menggunakan edge
Ee=B;
for i=1:1:d
    for j=1:1:e
        if (sum(sum(B(i:3+(i-1),j:3+(j-1))))-sum(B(i+1,j+1)))==8
            Ee(i+1,j+1)=0;
        else
            Ee(i+1,j+1)=B(i+1,j+1);
        end
        j=0;
    end
end
subplot(3,4,7),imshow(Ee),title('Gambar Edge'),pixval on


%%menggunakan salt
Ff=B;
for i=1:1:d
    for j=1:1:e
        if (sum(sum(B(i:3+(i-1),j:3+(j-1))))-sum(B(i+1,j+1)))==8
            Ff(i+1,j+1)=1;
        else
            Ff(i+1,j+1)=B(i+1,j+1);
        end
        j=0;
    end
end
subplot(3,4,8),imshow(Ff),title('Gambar Salt'),pixval on


%%menggunakan Pepper
Gg=B;
for i=1:1:d
    for j=1:1:e
        if (sum(sum(B(i:3+(i-1),j:3+(j-1))))-sum(B(i+1,j+1)))==0
            Gg(i+1,j+1)=0;
        else
            Gg(i+1,j+1)=B(i+1,j+1);
        end
        j=0;
    end
end
subplot(3,4,9),imshow(Gg),title('Gambar Pepper'),pixval on

%%menggunakan Salt-Pepper
Hh=B;
for i=1:1:d
    for j=1:1:e
        if (sum(sum(B(i:3+(i-1),j:3+(j-1))))-sum(B(i+1,j+1)))==0
            Hh(i+1,j+1)=0;
        elseif (sum(sum(B(i:3+(i-1),j:3+(j-1))))-sum(B(i+1,j+1)))==8
            Hh(i+1,j+1)=1;
        else
            Hh(i+1,j+1)=B(i+1,j+1);
        end
        j=0;
    end
end
subplot(3,4,10),imshow(Hh),title('Gambar Salt-Pepper'),pixval on

%%menggunakan Edge yang telah di salt-pepper
Ii=Hh;
for i=1:1:d
    for j=1:1:e
        if (sum(sum(Hh(i:3+(i-1),j:3+(j-1))))-sum(Hh(i+1,j+1)))==8
            Ii(i+1,j+1)=0;
        else
            Ii(i+1,j+1)=Hh(i+1,j+1);
        end
        j=0;
    end
end
subplot(3,4,11),imshow(Ii),title('Gambar Edge yang telah di salt-pepper'),pixval on
toc




Tutorial 7 Menggunakan Median Filter


%%Function dari median filter
function median(I)
%I=imread('*:\Y.bmp');

figure, imshow(I),title('Gambar RGB');
I=0.2989*I(:,:,1)+0.587*I(:,:,2)+0.114*I(:,:,3);
figure, imshow(I),title('Gambar Grayscale');
[M,N]=size(I);
l=9;
w=(3-1)/2; %faktor mask 3 x 3
h=(3-1)/2; %faktor mask 3 x 3
for x=1:M
    for y=1:N
        if y<=w||y>=N-w||x<=h||x>=M-h
            K(x,y)=I(x,y);
        else
            for m=x-h:x+h
                for n=y-w:y+w
                    T(m-x+h+1,n-y+w+1)=I(m,n);
                end
            end
            temp=T(:);
            for m=1:l
                for n=1:l-m
                    if temp(n)>temp(n+1)
                        t=temp(n);
                        temp(n)=temp(n+1);
                        temp(n+1)=t;
                    end
                end
            end
            K(x,y)=temp((l+1)/2);
        end
    end
end

figure,imshow(K),title('Gambar Median Filter'), pixval on

 


 


 





Tutorial 8 Menggunakan Gaussian Filter



%%Convolution
clc, clear all, close all

rgb=imread('jeboy.jpg');
figure, imshow(rgb),title('Gambar RGB');

gr=0.2989*rgb(:,:,1)+0.587*rgb(:,:,2)+0.114*rgb(:,:,3);

figure, imshow(gr),title('Gambar Grayscale');

%matrik a diagonal edge detection mask
ed=[1 2 1;
    2 4 2;
    1 2 1];

ed=(1/16)*ed;

[k l]=size(gr);
[m n]=size(ed);
d=k-m+1;
e=l-n+1;

z1=zeros(d, e);

for i=1:d
    for j=1:e
        sum=0;
        for x=1:m
            for y=1:n
                sum=sum+gr(i-x+m,j-y+n)*ed(x,y);
            end
        end
        z1(i,j)=sum;
    end
end
figure, imshow(z1, []),title('Gambar Gaussian Convolusion');




Tutorila 9 Menggunakan Rotasi


%%Program Rotasi
clc,clear all
%%rotate
im1 = imread('jeboy.jpg');imshow(im1); 
[m,n,p]=size(im1);
thet = pi/4;

for t=1:m
   for s=1:n
      i = uint16((t-m/2)*cos(thet)-(s-n/2)*sin(thet)+m/2);
      j = uint16((t-m/2)*sin(thet)+(s-n/2)*cos(thet)+n/2);
      if i>0 && j>0 && i<=m && j<=n          
         im2(t,s,:)=im1(i,j,:);
      end
   end
end
figure,imshow(im2);


 


 Tutoral 10 Menggunakan Laplacian Filter



%%Program laplacian
f=imread('harimau.jpg');
f=im2double(f);
r=f(:,:,1);
g=f(:,:,2);
b=f(:,:,3);
[m n]=size(r);
for i=1:m
    for j=1:n
        ip=i+1;
        im=i-1;
        jm=j-1;
        jp=j+1;
        if(im<1)
            im=i;
        elseif (ip>m)
            ip=i;
        end
        if(jm<1)
            jm=j;
        elseif (jp>n)
            jp=j;
        end
        rt(i,j)=-4*r(i,j)+ 1*(r(i,jm)+r(i,jp)+r(ip,j)+r(im,j));
        gt(i,j)=-4*g(i,j)+ 1*(g(i,jm)+g(i,jp)+g(ip,j)+g(im,j));
        bt(i,j)=-4*b(i,j)+ 1*(b(i,jm)+b(i,jp)+b(ip,j)+b(im,j));
       end
end
rt=r-rt;
gt=g-gt;
bt=b-bt;
T=cat(3,rt,gt,bt);
subplot(1,2,1), imshow(f),title('Original Image');
subplot(1,2,2), imshow(T),title('Sharpened Image');





Tutorial 11 Menggunakan Intenshity Shift



%%program
clear all,clc
im=imread('harimau.jpg');
gr=0.2989.*im(:,:,1)+0.578.*im(:,:,2)+0.114.*im(:,:,3);
subplot(1,2,1),imshow(gr),title('Grayscale');
[m n]=size(gr);
k=10;
W=100;
jd=gr;
for i=1:1:m
    for j=1:1:n
        if gr(i,j)+k<0
            jd(i,j)=0;
        elseif gr(i,j)+k>=0 && gr(i,j)<=W
            jd(i,j)=im(i,j)+k;
        elseif gr(i,j)+k>W
            jd(i,j)=W;
        end
    end
end

subplot(1,2,2),imshow(jd),title('Intensity Shift');






Tutorial 12 Menggunakan Intenshity Multiple



%%program
clear all,clc
im=imread('harimau.jpg');
gr=0.2989.*im(:,:,1)+0.578.*im(:,:,2)+0.114.*im(:,:,3);
subplot(1,2,1),imshow(gr),title('Grayscale');
[m n]=size(gr);
k=10;
W=100;
jd=gr;
for i=1:1:m
    for j=1:1:n
        if gr(i,j)*k<0
            jd(i,j)=0;
        elseif gr(i,j)*k>=0 && gr(i,j)<=W
            jd(i,j)=im(i,j)*k;
        elseif gr(i,j)*k>W
            jd(i,j)=W;
        end
    end
end

subplot(1,2,2),imshow(jd),title('Intensity Shift');

 





Tutorial 13 Menggunakan Prewit Deteksi Tepi



%%Program Prewit
clear all,close all,clc
%A=input('masukkan gambar anda:\n');
A2=imread('harimau.jpg');
R=A2(:,:,1);
G=A2(:,:,2);
B=A2(:,:,3);

sobx=[-1 0 1; -1 0 1; -1 0 1];
soby=[1 1 1; 0 0 0; -1 -1 -1];
k=im2double(R);
[r,t]=size(k);
for(d=2:r-1);
    for(f=2:t-1);
        w0=k(d,f);
        w1=k(d,f+1);
        w2=k(d-1,f+1);
        w3=k(d-1,f);
        w4=k(d-1,f-1);
        w5=k(d,f-1);
        w6=k(d+1,f-1);
        w7=k(d+1,f);
        w8=k(d+1,f+1);
        p=[w4 w3 w2; w5 w0 w1; w6 w7 w8];
        px=(sum(sum(p.*sobx)))^2;
        py=(sum(sum(p.*soby)))^2;
        pz=sqrt(px+py);
        l1(d,f)=pz;
    end
end

k=im2double(G);
[r,t]=size(k);
for(d=2:r-1);
    for(f=2:t-1);
        w0=k(d,f);
        w1=k(d,f+1);
        w2=k(d-1,f+1);
        w3=k(d-1,f);
        w4=k(d-1,f-1);
        w5=k(d,f-1);
        w6=k(d+1,f-1);
        w7=k(d+1,f);
        w8=k(d+1,f+1);
        p=[w4 w3 w2; w5 w0 w1; w6 w7 w8];
        px=(sum(sum(p.*sobx)))^2;
        py=(sum(sum(p.*soby)))^2;
        pz=sqrt(px+py);
        l2(d,f)=pz;
    end
end

k=im2double(B);
[r,t]=size(k);
for(d=2:r-1);
    for(f=2:t-1);
        w0=k(d,f);
        w1=k(d,f+1);
        w2=k(d-1,f+1);
        w3=k(d-1,f);
        w4=k(d-1,f-1);
        w5=k(d,f-1);
        w6=k(d+1,f-1);
        w7=k(d+1,f);
        w8=k(d+1,f+1);
        p=[w4 w3 w2; w5 w0 w1; w6 w7 w8];
        px=(sum(sum(p.*sobx)))^2;
        py=(sum(sum(p.*soby)))^2;
        pz=sqrt(px+py);
        l3(d,f)=pz;
    end
end
q(:,:,1)=l1;
q(:,:,2)=l2;
q(:,:,3)=l3;
subplot(1,2,1), imshow (A2);
subplot(1,2,2), imshow (q), pixval on;