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.
digital image processingimageimage analysisimage processingImage Processing Toolbox
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;format compact;fontSize = 20;%===============================================================================
% Read in good demo image.
folder = pwd;baseFileName = 'contour.jpg';% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);% Check if file exists.
if ~exist(fullFileName, 'file') % File doesn't exist -- didn't find it there. Check the search path for it.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file') % Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName); uiwait(warndlg(errorMessage)); return; endendrgbImage = imread(fullFileName);% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows, columns, numberOfColorChannels] = size(rgbImage);if numberOfColorChannels > 1 % It's not really gray scale like we expected - it's color.
% Convert it to gray scale by taking only the green channel.
grayImage = rgbImage(:, :, 2); % Take green channel.
else grayImage = rgbImage; % It's already grayscale.
end% Display the image.
subplot(2, 3, 1);imshow(grayImage, []);axis on;title('Gray Scale Image', 'FontSize', fontSize, 'Interpreter', 'None');% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off') % Threshold the image
binaryImage = grayImage > 128;% Display the image.subplot(2, 3, 2);imshow(binaryImage, []);axis on;title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');% Get rid of huge white surround
binaryImage = imclearborder(binaryImage);% Display the image.subplot(2, 3, 3);imshow(binaryImage, []);axis on;title('Border-cleared Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');% Close the image
se = strel('disk', 2, 0);binaryImage = imclose(binaryImage, se);% Display the image.subplot(2, 3, 4);imshow(binaryImage, []);axis on;title('Closed Image', 'FontSize', fontSize, 'Interpreter', 'None');% Fill holes
binaryImage = imfill(binaryImage, 'holes');% Display the image.subplot(2, 3, 5);imshow(binaryImage, []);axis on;title('Final Image', 'FontSize', fontSize, 'Interpreter', 'None');
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;format compact;fontSize = 20;%===============================================================================
% Read in gray scale demo image.
folder = pwd; % Determine where demo folder is (works with all versions).
baseFileName = '91901.jpg';% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);% Check if file exists.
if ~exist(fullFileName, 'file') % The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file') % Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName); uiwait(warndlg(errorMessage)); return; endendrgbImage = imread(fullFileName);% Get the dimensions of the image.
% numberOfColorChannels should be = 1 for a gray scale image, and 3 for an RGB color image.
[rows, columns, numberOfColorChannels] = size(rgbImage)if numberOfColorChannels > 1 % It's not really gray scale like we expected - it's color.
% Use weighted sum of ALL channels to create a gray scale image.
% grayImage = rgb2gray(rgbImage);
% ALTERNATE METHOD: Convert it to gray scale by taking only the green channel,
% which in a typical snapshot will be the least noisy channel.
grayImage = rgbImage(:, :, 2); % Take green channel.
else grayImage = rgbImage; % It's already gray scale.
end% Now it's gray scale with range of 0 to 255.
% Display the image.
subplot(2, 2, 1);imshow(grayImage, []);title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');axis('on', 'image');hp = impixelinfo();%------------------------------------------------------------------------------
% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')drawnow;% Show the histogram
subplot(2, 2, 2);imhist(grayImage);grid on;title('Histogram of Original Image', 'FontSize', fontSize, 'Interpreter', 'None');% Filter and binarize the image, then find top rows.
% Try it out for different filter window widths.
% Note: A filter window width of 37 or larger seems good for this image.
for windowSize = 31 : 2 : 61 % Do a top hat filter.
edgeImage = imtophat(grayImage, true(windowSize)); % Display the image. subplot(2, 2, 3); imshow(edgeImage, []); caption = sprintf('Top Hat Filter with size %d', windowSize); title(caption, 'FontSize', fontSize, 'Interpreter', 'None'); axis('on', 'image'); hp = impixelinfo(); % Binarize the image
binaryImage = imbinarize(edgeImage); % You could call imfill() to fill it, but it's not really necessary.
% Take just the largest blob, in case there are smaller ones.
binaryImage = bwareafilt(binaryImage, 1); % Display the image. subplot(2, 2, 4); imshow(binaryImage, []); title('Binary Image', 'FontSize', fontSize, 'Interpreter', 'None'); axis('on', 'image'); hp = impixelinfo(); % Now go across the columns finding the top row.
topRows = zeros(1, columns); for col = 1 : columns row = find(binaryImage(:, col), 1, 'first'); % IF it found something, record it.
if ~isempty(row) topRows(col) = row; end end % Make missing values the same as the first non-zero column
firstCol = find(topRows, 1, 'first'); topRows(1 : firstCol) = topRows(firstCol); lastCol = find(topRows, 1, 'last'); topRows(lastCol : end) = topRows(lastCol); % You could extend the bottom of the binary image to the bottom of the image, but it's not really necessary.
% Plot it over the image
subplot(2, 2, 1); imshow(grayImage, []); title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None'); hold on; plot(topRows, 'r-', 'LineWidth', 3); hold off; promptMessage = sprintf('Showing filter window size of %d\n\nDo you want to enlarge the window,\nor Accept this size?', windowSize); titleBarCaption = 'Continue?'; buttonText = questdlg(promptMessage, titleBarCaption, 'Next', 'Accept and continue', 'Next'); if contains(buttonText, 'Accept', 'IgnoreCase', true) break; endend% Display top rows in command window
topRows
Best Answer