MATLAB: Fill data and Make an image White

digital image processingimageimage analysisimage processingImage Processing Toolbox

Hello
I'm new in image Processing . I have a binary image. I want to fill the missing data and after filling the missing data I want to make the lower part white as shown in figure. Can anyone here help me? My images are attached.

Best Answer

This maybe a bad answer but it works: I tried to find the left and right cliffs in the image and connected them to obtain the required output.
img = rgb2gray(imread('hill1.jpg'));
img = img>50;
% figure, imshow(img,[])
filterImg = bwareaopen(img,25);
% figure, imshow(filterImg,[])
columnsWithAllZeros = all(img == 0);
% get left and right indexes
left_row = find(columnsWithAllZeros,1,'first')-1;
left_column = find(img(:,left-1),1,'first');
left_index = [left_row, left_column];
right_row = find(columnsWithAllZeros,1,'last')+1;
right_column = find(img(:,right+1),1,'first');
right_index = [right_row, right_column];
% draw a line into existing image to fill gap
hLine = imline(gca, [left_index; right_index]);
singleLineBinaryImage = hLine.createMask();
filterImg(singleLineBinaryImage) = 1;
% fill bottom

output_img = zeros(size(img));
for col = 1: size(img,2)
first_nnz_row = find(filterImg(:,col),1,'first');
output_img(first_nnz_row:end,col) = 1;
end
figure,
subplot(131), imshow(img);
subplot(132), imshow(filterImg);
subplot(133), imshow(output_img);
If you are ok with some distortion in shape, dilation works too.
dilate_img = imdilate(filterImg, strel('disk',15));
figure, imshow(dilate_img)
% fill bottom
output_img = zeros(size(img));
for col = 1: size(img,2)
first_nnz_row = find(dilate_img(:,col),1,'first');
output_img(first_nnz_row:end,col) = 1;
end
figure, imshow(output_img)
Hope this helps
Related Question