MATLAB Answers

How do I count the number of pixels from top white to bottom white pixel?

21 views (last 30 days)
I attach the image I want to work with.
I define with red vertical lines the width or the space of the area I want to measure the total number of pixels for every column (e.g. cumsum command). The columns will contain the total number of pixels of only the distance between the top white pixel of the curve and the last bottom white pixel. No beyond the two margins. Only this distance. From top white pixel to bottom white pixel, all pixels included in this black area, and only for the space between those two red vertical lines that denote the first and last white pixels of the white little curve line bottom structure.
I am sure it is not hard. Just a bit fuzzy and complicated but other than that I know it can be done.
Thanks for the time spending reading my query.


Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 15 Sep 2019
Try this:
[rows, columns] = size(binaryImage);
heights = zeros(1, columns);
for col = 1 : columns
thisColumn = binaryImage(:, col);
topRow = find(thisColumn, 1, 'first');
if ~isempty(topRow)
bottomRow = find(thisColumn, 1, 'last');
heights(col) = bottomRow - topRow; % Add 1 if you want.


Show 2 older comments
Stelios Fanourakis
Stelios Fanourakis on 19 Sep 2019
But I don't think it's correct since there is no way to be 500 or more pixels in any column from one white curve to another. I just measured one of the column's pixels somewhere in the middle of the distance and I found 129 pixels. So, no chance to have more than 500 pixels in any of its columns as the plot suggests. So, I doubt it is correct. Maybe a slight modification is needed.
Image Analyst
Image Analyst on 19 Sep 2019
Not sure what you did but it works fine for me. Here is the complete demo:
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 = 15;
% Read in gray scale demo image.
% Let's let the user select from a list of all the demo images that ship with the Image Processing Toolbox.
folder = pwd;
baseFileName = 'edges.png';
% 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);
rgbImage = imread(fullFileName);
% Display the image.
subplot(2, 2, 1);
imshow(rgbImage, []);
title('Original Grayscale Image', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
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(:, :, 3); % Take blue channel.
grayImage = rgbImage; % It's already gray scale.
% Now it's gray scale with range of 0 to 255.
% 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')
% Display the image.
subplot(2, 2, 2);
imshow(grayImage, []);
title('Mask', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
% Make a mask.
mask = imclearborder(grayImage > 128);
% Erase everything down to line 30 to get rid of thick white bar at the top.
mask(1:30, :) = false;
% Display the image.
subplot(2, 2, 3);
imshow(mask, []);
title('Mask', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
heights = zeros(1, columns);
for col = 1 : columns
thisColumn = mask(:, col);
topRow = find(thisColumn, 1, 'first');
if ~isempty(topRow)
bottomRow = find(thisColumn, 1, 'last');
heights(col) = bottomRow - topRow; % Add 1 if you want.
% Display the plot.
subplot(2, 2, 4);
plot(heights, 'b-', 'LineWidth', 2);
grid on;
title('Heights', 'FontSize', fontSize, 'Interpreter', 'None');
xlabel('Column Number', 'FontSize', fontSize, 'Interpreter', 'None');
ylabel('Height', 'FontSize', fontSize, 'Interpreter', 'None');
axis('on', 'image');
0001 Screenshot.png
Stelios Fanourakis
Stelios Fanourakis on 20 Sep 2019
Yes now it works correct. I believe this line made the huge difference. Thanks once again Image Analyst for your precise and valuable contribution to my work.
mask(1:30, :) = false;

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!