fill data and Make an image White
Ältere Kommentare anzeigen
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.

Akzeptierte Antwort
Weitere Antworten (1)
Image Analyst
am 9 Jun. 2018
I would use a conceptually simpler approach. I'd simply find the top rows of the data that is there. Then use interp1() to estimate all the top lines, including a straight line across the "missing" portions (see red line in the figure on the left below). Then scan across filling the image from those top lines down to the bottom of the image. I think it's a lot simpler and more intuitive than Gopichandh's approach. See code below. Note that the first half of the code is just to get a binary image because you did not supply the actual binary image. The main code starts after the %====== line.
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 = '1.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;
end
end
rgbImage = imread(fullFileName);
% Display the image.
subplot(1, 2, 1);
imshow(rgbImage, []);
title('Original Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
hp = impixelinfo();
% 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(:, :, 1); % Take blue channel.
else
grayImage = rgbImage; % It's already gray scale.
end
% Now it's gray scale with range of 0 to 255.
% Threshold it to make it binary
binaryImage = grayImage > 128;
% Display the image.
subplot(1, 2, 1);
imshow(binaryImage, []);
title('Original Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis on;
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;
%=============================================================================
% Now we have our binary image and we can begin the algorithm!
%------------------------------------------------------------------------------
% First, find the top line in each column
topRows = -999 * ones(1, columns); % Initialize
for col = 1 : columns
topPixel = find(binaryImage(:, col), 1, 'first');
if ~isempty(topPixel)
topRows(col) = topPixel;
end
end
% Now interpolate missing values.
missingColumns = topRows < 0;
% Remove missing data.
x = 1 : columns;
x(missingColumns) = [];
topRows(missingColumns) = [];
% Interpolate the missing ones
xInterp = 1:columns;
topRows = round(interp1(x, topRows, xInterp)); % Round to the nearest line (row).
hold on;
plot(xInterp, topRows, 'r-', 'LineWidth', 2);
% Now we know the top line, even for those that were "missing."
% Fill in the image from the top row downwards.
for col = 1 : columns
binaryImage(topRows(col):end, col) = true;
end
% Display the final image.
subplot(1, 2, 2);
imshow(binaryImage, []);
axis on;
impixelinfo;
title('Final Binary Image', 'FontSize', fontSize, 'Interpreter', 'None');

11 Kommentare
Alina tom
am 11 Jun. 2018
Image Analyst
am 11 Jun. 2018
Change this
binaryImage(topRows(col):end, col) = true;
to this:
binaryImage(1:topRows(col), col) = true;
to change all pixels from line 1 to the topRows line to white.
I find this a simpler approach than the other answer. Using interp1() is easier than finding right and left pixels and using imline() and then ORing in a binary image. If you like this approach, maybe you could vote for it.
Alina tom
am 11 Jun. 2018
Image Analyst
am 11 Jun. 2018
Alina tom
am 11 Jun. 2018
Alina tom
am 11 Jun. 2018
Image Analyst
am 11 Jun. 2018
Your images seemed to have white lines 2 pixels thick along the top, right, and bottom edges. So I cropped those off. I also put in a fix where there was no top row at the beginning or end to interpolate to. Fix is in the attached code.

Alina tom
am 12 Jun. 2018
Image Analyst
am 12 Jun. 2018
Interpolate it to what? There's nothing there to interpolate to. Do you just want to extrapolate the first value to the left for some reason? Why? You perhaps should not even do doing anything with data on the left side of the image because there's nothing there. Why do you want to do that???
Alina tom
am 12 Jun. 2018
Alina tom
am 12 Jun. 2018
Kategorien
Mehr zu Images finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!











