Kamis, 07 Februari 2019

Mencari Edge Image


img = false(5); % Use zeros(5) if you prefer
img(3, 2:4) = true; % use 1 instead of true if you prefer
% Get complement of image, with a border round it in case the
% blob is at the boundary
notimg = true(size(img)+2);
notimg(2:end-1, 2:end-1) = ~img;
% Find locations where a non-zero pixel is adjacent to a zero pixel,
% for each cardinal direction in turn
topedges = img & notimg(1:end-2, 2:end-1);
leftedges = img & notimg(2:end-1, 1:end-2);
bottomedges = img & notimg(3:end, 2:end-1);
rightedges = img & notimg(2:end-1, 3:end);
% Sum each set of locations separately, then add to get total perimeter/Keliling:
perim = sum(topedges(:)) + sum(leftedges(:)) + ...
+ sum(bottomedges(:)) + sum(rightedges(:));
% print result
fprintf('Perimeter is %d\n', perim);


img img = 5×5 logical array 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 >> topedges topedges = 5×5 logical array 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 >> rightedges rightedges = 5×5 logical array 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 >> bottomedges bottomedges = 5×5 logical array 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 >> leftedges leftedges = 5×5 logical array 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0

notimg = 7×7 logical array 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1


IMPLEMENTASI:


I = imread('daun.jpg');
[N,M,L]=size(I);
for n=1:N
    for m=1:M
        if (I(n,m,1)<225 && I(n,m,2)< 225 && I(n,m,3)<225)
            bw1(n,m,1)=255;%I(n,m,1:3);
        else
            bw1(n,m,1)=0;
        end
    end
end                             % bw1            210x210              352800  double  
BW = imbinarize(bw1);           % BW             210x210               44100  logical   
bw2 = imfill(BW,'holes');
figure, imshow(bw2);
  
s = regionprops(bw2, 'All');
a=s(1,1);
b=s(2,1);

ps=['Luas Area (FilledImage) ' num2str(a.Area) ' px'];
    disp(ps);
ps=['Titik Centroid' num2str(a.Centroid(1)) ',' num2str(a.Centroid(2))];
    disp(ps);
ps=['Panjang  Area ' num2str(a.MajorAxisLength)];
    disp(ps);
ps=['Lebar Area  ' num2str(a.MinorAxisLength)];
    disp(ps);
ps=['Keliling /Perimeter Area  ' num2str(a.Perimeter)];
    disp(ps);
disp('8 Koordinat Terluar ');    
for i = 1:8
    ps=['Posisi ' num2str(i) ' : ' num2str(a.Extrema(i,1)) ',' num2str(a.Extrema(i,2))];
    disp(ps);
end   

Image=a.Image;
FilledImage=a.FilledImage;
M=s.Extrema;
img=~FilledImage;
notimg = true(size(img)+2);
notimg(2:end-1, 2:end-1) = ~img;

hold on
    plot(a.Centroid(1), a.Centroid(2), 'r*')

    for i = 1:8
        plot(M(i,1), M(i,2), 'y*')
    end
   

    hold off



figure(2)
    leftedges =  img & notimg(2:end-1, 3:end);
    bottomedges =    img & notimg(1:end-2, 2:end-1);
    rightedges =   img & notimg(2:end-1, 1:end-2);
    topedges = img & notimg(3:end,   2:end-1);
    perim = sum(topedges(:)) + sum(leftedges(:)) + sum(bottomedges(:)) + sum(rightedges(:));

    subplot(2,2,1);
        imshow(~rightedges);title('rightedges area');
     subplot(2,2,2);
        imshow(~topedges);title('topedges area');
     subplot(2,2,3);
        imshow(~leftedges);title('leftedges area');
     subplot(2,2,4);
        imshow(~bottomedges);title('bottomedges area');

        



Luas Area (FilledImage) 60789 px
Titik Centroid156.8662,168.5555
Panjang  Area 349.4928
Lebar Area  224.3
Keliling /Perimeter Area  988.042
8 Koordinat Terluar 
Posisi 1 : 26.5,2.5
Posisi 2 : 30.5,2.5
Posisi 3 : 287.5,207.5
Posisi 4 : 287.5,208.5
Posisi 5 : 213.5,328.5
Posisi 6 : 210.5,328.5
Posisi 7 : 18.5,6.5
Posisi 8 : 18.5,5.5


Tidak ada komentar:

Posting Komentar