fill data and Make an image White
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Alina tom
il 3 Mag 2018
Commentato: Alina tom
il 12 Giu 2018
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.
0 Commenti
Risposta accettata
Gopichandh Danala
il 3 Mag 2018
Modificato: Gopichandh Danala
il 3 Mag 2018
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
14 Commenti
Gopichandh Danala
il 9 Giu 2018
I tried to quickly find all the missing gaps in between two peaks and all at once.
If you also need to account for a gap at end of start to draw a line with a certain slope add the above code I provided to make this suite your requirement.
img = rgb2gray(imread('trial.jpg'));
img = img>50;
% figure, imshow(img,[])
filterImg = bwareaopen(img,25);
figure, imshow(filterImg,[])
columnsWithAllZeros = all(filterImg == 0);
% compute number of gaps
diffs = diff(columnsWithAllZeros);
numChangesTo1 = sum(diffs == 1);
% compute all the left and right row indices
allRows = find(diffs);
leftRows = allRows(1:2:end);
rightRows = allRows(2:2:end)+1;
% compute left and right column indices and draw line into image
for nGaps = 1:numChangesTo1
left_row = leftRows(nGaps); right_row = rightRows(nGaps);
left_column = find(img(:,left_row),1,'first');
left_index = [left_row, left_column];
right_column = find(img(:,right_row+1),1,'first')+1;
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;
end
% 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);
One last suggestion, I think with all the codes I provided, you can combine all this and make it suite for multiple purposes to satisfy all your conditions.
Più risposte (1)
Image Analyst
il 9 Giu 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 Commenti
Vedere anche
Categorie
Scopri di più su Red in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!