[HELP] Finding the lowest positioned pixel in the figure.

I have a code as follow: which is converted into binary image.
image= [pos1];
BW = im2bw(pos1,map,0.4);
imshow(pos1(:,:)),figure, imshow(BW)
image= [pos2];
BW = im2bw(pos2,map,0.4);
imshow(pos2(:,:)),figure, imshow(BW)
which will return following images
with these images, I want to find the lowest point (as circled with red) and print the location information into one matrix. (only need y axis value) = Height from the bottom of the image. (the first white pixel)
for instance if in lowest point pixel in image1 located at 123,342, and for image 2 located at 134,423
then I want to have matrix that has values of 343, and 423.
I was doing this manually for 10 images. and I gave up as there are 100s of them...
It would be really appreciate if you can help me with this.

 Accepted Answer

Guillaume's answer is good. I just wanted to add: don't do this:
image= [pos1];
All that does it to destroy the built-in function called image(), and nothing useful whatsoever in your code. It can be eliminated without any problem. Never use "image" as the name of a variable.
By the way, another possible method to find the lowest row is
lowestRow1 = find(sum(BW1, 2)>0, 1, 'last'); % Bottom dot in image #1
lowestRow2 = find(sum(BW2, 2)>0, 1, 'last'); % Bottom dot in image #2
output = [lowestRow1, lowestRow2]; % The output array you said you wanted.
Basically you're summing the image horizontally (across columns, which gives a mean vertical profile) and finding the last (lowest) row that has something in it. However I'm not sure why you want 343 and 423 when the lowest rows are 342 and 423 - why add 1 to the first image???

5 Comments

First of all, thank you very much for your help, I could manage to find the lowest point. and yeah it should be 342 and 423.. I have another question with this code,
clc;
clear
cd 'D:\pos1\ '; %mean image location
files = dir('*.mat');
for i=1:100;
load((['Process_shot_f' num2str(i),'.mat']));
image_low = Process_shot_fi;
BW1 = im2bw(image_low,maps,0.4);
lowestRow1 = find(sum(BW1, 2)>0, 1, 'last');
output = [lowestRow1];
end;
this gives me output value of 100th image. whereas I want to have a all the output value from image 1 to image 100. I was writing
BW1=,... BW2=..., BW3= ...............BW40=....
lowestRow1, lowestRow2....lowestRow3.... lowestRow4..... lowestRow40........
output = [lowestRow1, lowestRow2, ......]
for 30 images..
and wonder is there way to shorten this code, so that at the end I have a list of the all output values in one matrix?
I would like to ask how I can get the x coordinate value of the lowest point through this code?
@Xiao MA you can do this
% Find all white rows and columns in the binary image
[yRows, xColumns] = find(binaryImage);
% Find the lowest row, which will have the highest yRows value
[yRowsOfLowestPoint, indexOfLowestRow] = max(yRows);
% Get the corresponding column (x coordinate) at that location
xColumnOfLowestPoint = xColumns(indexOfLowestRow);
@Image Analyst Thank you very much! I'm able to run it successfully and get the results I wanted. Really appreciate!!!

Sign in to comment.

More Answers (1)

Use the two outputs version of find to get the rows and columns of all white pixels and get the max of the rows:
[row, ~] = find(BW);
lowestrow = max(row);

Asked:

YJ
on 22 Oct 2014

Commented:

on 21 May 2022

Community Treasure Hunt

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

Start Hunting!