How to continue find white pixels from last column to the next lastColumns-1 and so on?

2 views (last 30 days)
% Go across columns of image looking for last white pixel in the column.
[rows, columns] = size(BW);
% Output image
output = false(rows, columns); % Initialize
% Go across columns of image looking for last white pixel in the column.
for lastcol = 1:columns
lastRow = find(BW, 1,'last');
output(lastRow:end) = true;
for col_149 = 1:columns-1
lastRow = find(BW {rows , columns}==1);
output(lastRow:end) = true;
end
end

Answers (2)

Image Analyst
Image Analyst on 18 Apr 2015
Try this:
% Go across columns of image looking for last white pixel in the column.
[rows, columns] = size(BW);
% Output image
output = false(rows, columns); % Initialize
% Go across columns of image looking for last white pixel in the column.
for col = 1:columns
thisColumn = BW(:, col);
lastRow(col) = find(thisColumn, 1,'last');
output(lastRow(col):end, col) = true;
end

Image Analyst
Image Analyst on 19 Apr 2015
We don't need to carry out this same discussion out over 5 threads. You didn't say what the error was so I had to just go ahead and make a full blown demo for you with the binary image you posted in your other thread. Here it is:
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;
% Display binary image.
folder = fileparts(which('bwimg.png')); % Determine where demo folder is (works with all versions).
baseFileName = 'bwimg.png';
% 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;
end
end
% Display image.
subplot(1, 2, 1);
rgbImage = imread(fullFileName);
imshow(rgbImage, []);
title('Original Image', 'FontSize', fontSize);
axis on;
[rows, columns, numberOfColorChannels] = size(rgbImage);
if numberOfColorChannels > 1
BW = rgbImage(:,:,2) > 128;
else
BW = rgbImage > 128;
end
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
% For some reason the first and last row of BW are all 1's.
% The poster saved it incorrectly or something.
% Anyway, zero out the first and last line.
BW(1, :) = false;
BW(end, :) = false;
% Go across columns of image looking for last white pixel in the column.
[rows, columns] = size(BW);
% Initialize last row
lastRow = rows * ones(1, columns);
% Output image
output = false(rows, columns); % Initialize
% Go across columns of image looking for last white pixel in the column.
for col = 1:columns
thisColumn = BW(:, col);
row = find(thisColumn, 1,'last');
if ~isempty(row)
lastRow(col) = find(thisColumn, 1,'last');
output(lastRow(col):end, col) = true;
end
end
subplot(1, 2, 2);
imshow(output, []);
title('Output Image', 'FontSize', fontSize);
axis on;

Community Treasure Hunt

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

Start Hunting!