I need a code that will scan a binary image (on a plot, x vs. y) and provide me with the coordinates of the first black pixel detected across the y-axis of the entire image.

1 view (last 30 days)
1) code to scan the image. I believe the code will function as an iteration.
2) When x=1 the code will scan along the y-axis to provide me with the first black pixel detected and print the coordinates of the black pixel.
3) step 2 will repeat on x=2 where the scan will continue along the y-axis until the first black pixel is detected and print the coordinates of the black pixel.
4) then the scan will continue moving along the x-axis (x=3), up the black pixel where the y coordinates is found.
I hope I have been clear in my explanation.
  3 Comments
Martin Olowe
Martin Olowe on 27 Feb 2018
Apologies if I have offended you by my request.
I have searched for what previous people have done but I haven't managed to come up with one specific to the function I need. I have converted my image into a binary image by thresholding and cropped which area I want to scan.
I have tried the find function but not familiar with unique. The problem is that I know how the function will operate but I don't know how I will go about writing the code.

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 27 Feb 2018
sum(cumprod(YourMatrix~=0))+1
Will give you the index of the first black pixel in each column. In cases where there were none, the value will be one more than the number of rows.
  3 Comments
Walter Roberson
Walter Roberson on 27 Feb 2018
What does the "bottom" mean relative to an array?
There are two ways of presenting images on-screen. If you use the row indices as Y coordinates, then the array values with lowest row indices appear at the bottom of the display, cartesian coordinates. However it is also common to want to have the array values with lowest row indices appear at the top of the display, "table" style (tables usually increase in value as you go downwards.)
The code I posted will find the black pixels with the lowest index -- cartesian coordinate style.
If you want the searching to be from the "bottom" of the array (highest index first) then you can use
size(YourMatrix,1) - sum(cumprod(flipud(YourMatrix~=0)))
This will give 0 for the locations where there were no black pixels in the column.
Martin Olowe
Martin Olowe on 12 Jun 2020
Hi,
Thanks for your reply. I'm actually still developing this code. I have an attached an image to to ask if the code you have provided will be able to process this image?

Sign in to comment.


Image Analyst
Image Analyst on 12 Jun 2020
I've posted code like this many, many times. Assume mask is your binary image (black or white, true or false, 1 or 0):
[rows, columns] = size(mask)
topRows = zeros(1, columns);
for col = 1 : columns
t = find(mask(:, col) == 1, 1, 'first');
if ~isempty(t)
topRows(col) = t;
end
end
plot(topRows, 'b-', 'LineWidth', 2);
grid on;
xlabel('column', 'FontSize', 20);
ylabel('Row', 'Fontsize', 20);
If you just have a gray scale image, then call imbinarize() first to get mask.

Community Treasure Hunt

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

Start Hunting!