how to fill a binary image from bottom to top? i'm trying using imfill but the output is wrong. The second picture is want i want.

2 views (last 30 days)
clear all;close all;
%Load and Read Image object = imread('kotak.jpg');
%Resize the image by pixels obstacle= imresize(object,[100 150]);
%Sobel Edge Detection Igray = rgb2gray(obstacle); Iedge = im2uint8(edge(Igray,'sobel',0.05)); Iedge = repmat(Iedge,[1 1 3]); Ifinal = obstacle + Iedge;
%Convert to Grayscale Image & %Sobel gradient mask G = rgb2gray(Ifinal); C=double(G);
for i=1:size(C,1)-2 for j=1:size(C,2)-2 %Sobel mask for x-direction: Gx=((2*C(i+2,j+1)+C(i+2,j)+C(i+2,j+2))-(2*C(i,j+1)+C(i,j)+C(i,j+2))); %Sobel mask for y-direction: Gy=((2*C(i+1,j+2)+C(i,j+2)+C(i+2,j+2))-(2*C(i+1,j)+C(i,j)+C(i+2,j)));
%The gradient of the image
%B(i,j)=abs(Gx)+abs(Gy);
G(i,j)=sqrt(Gx.^2+Gy.^2);
end
end
%Adjust the brightness of Grayscale Image A = imadjust(G)
%Convert the Sobel Edge Detection Grayscale adjusted image to binary image level = graythresh(A); BW = im2bw(A,level); BW = bwareaopen(BW, 50);
% fill against the bottom and left borders. bw_d = padarray(padarray(BW,[1 0],1,'post'),[0 1],1,'pre'); bw_d_filled = imfill(bw_d,'holes'); bw_d_filled = bw_d_filled(1:end-1,2:end);
% allocate space for thresholded image loop = zeros(size(BW));
% loop over all rows and columns for ii=1:size(BW,1) for jj=1:size(BW,2) % get pixel value pixel=BW(ii,jj); % check pixel value and assign new value if pixel==0 new_pixel=1; else pixel==1; new_pixel=0;
end
% save new pixel value in thresholded image
loop(ii,jj)=new_pixel;
end
end
%Flood fill binary image of Sobel Edge Detection flood = imcomplement(imfill(imcomplement(loop),'holes'));
%"logical OR" all these images together. bwfilled =flood & bw_d_filled;
%Dilate image of Binary Sobel Edge Detection se = strel('line',11,90); dilation=imdilate(bwfilled,se);
% Display result figure; subplot (331); imshow(obstacle); title('Original Image'); subplot (332); imshow(Ifinal); title('Sobel Edge RGB'); subplot(333); imshow(G); title('Grayscale+Sobel Gradient Mask'); subplot(334); imshow(A); title('Brightness'); subplot(335); imshow(BW); title('Sobel Edge Binary'); subplot(336); imshow(bw_d_filled); title('Bottom Fill Left Image'); subplot(337); imshow(flood); title('Flood Image'); subplot(338); imshow(bwfilled); title('Fill Image');
<<
>>

Accepted Answer

Nurul Najmah
Nurul Najmah on 2 Apr 2015
here is my input image https://NURULNAJMAH.opendrive.com/files?NTdfMTEwMTNfUExKRkc

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!