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


Matlab Edge Detection +Crop


 

 rooftops = imread('daun.jpg');
    imshow(rooftops);
    grayImage = rgb2gray(rooftops);
    imshow(grayImage);
    [r c] = size(grayImage);
    switchfactor = grayImage(round(r/2),round(c/2));
    if switchfactor <= 120
        minimumCanny = .0025;
        maximumCanny = .05;
        CFval = .6;
    elseif switchfactor <= 150
        minimumCanny = .005;
        maximumCanny = .075;
        CFval = .575;
    elseif switchfactor <= 180
        minimumCanny = .0075;
        maximumCanny = .1;
        CFval = .55;
    elseif switchfactor <= 210
        minimumCanny = .01;
        maximumCanny = .125; 
        CFval = .525;
    elseif switchfactor <= 240
        minimumCanny = .0125;
        maximumCanny = .15;
        CFval = .5;
    else
        minimumCanny = .015;
        maximumCanny = .2;
    end
    %%Active Contour (Overlaying mask and Contour) Works
    imshow(grayImage);





    hold on
    mask = false(size(grayImage));
    mask(20:end-20,20:end-20) = true;
    visboundaries(mask,'Color','b');
    bw = activecontour(grayImage, mask, 200, 'edge','ContractionBias',CFval);
    visboundaries(bw,'Color','r');
    title('Initial contour (blue) and final contour (red)');
    figure, imshow(bw)



    title('Segmented Image');
    %%Reading the Contour and Specifying the roof
    bwNew = grayImage;
    bwNew(~bw) = 0;
    figure;
    imshow(bwNew);
 E = entropyfilt(bwNew);
    Eim =E;% rescale(E);
    figure
    imshow(Eim)

    BW1 = imbinarize(Eim, .9);
    imshow(BW1);
    gI2 = bwNew;
    gI2(BW1) = 0;
    imshow(gI2)

    BWf1 = edge(gI2,'canny',[minimumCanny, maximumCanny],sqrt(.075));
    BWf2 = edge(bwNew,'canny',[minimumCanny, maximumCanny],sqrt(.075));
    %BWf2 = edge(gI2,'zerocross',1,[20,100]);
    figure;
    imshowpair(BWf1,BWf2,'montage')

    title('Textured                                   Normal');