Get pixels in a region

Hi,
I'm looking to get the locations of the first white pixel (top left) of each region and the last white pixel in each region (bottom right) of the image supplied. I do not need every white pixel in the region either, just the beginning and end points. I need this to determine the "entry and exit points" of the regions in the image so it can later be compared against a similar image.
If anyone has any suggestions of how this would be done, I'd really appreciate it. I've tried using find() but I only ever manage to get the very first white pixel and the very last in the image.
Thanks.

2 Comments

Your question is ambiguous. Do you need the leftmost white pixel of the top row that has at least one white pixel, or do you need the topmost white pixel of the first column that has at least one white pixel? Likewise, do you need the rightmost white pixel of the bottom row that has at least one white pixel, or do you need the bottommost white pixel of the last column that has at least one white pixel?
Your image has slanted lines so it does make a difference.
Thanks for trying to help Walter, it's columns I need I believe. I think I found a way to achieve what I wanted - I added a Bounding Box to all the regions and, from that, got the coordinates of the top left and top right corners of the bounding box. I'm only interested in the horizontal locations, and not the vertical, so this seems to work.

Sign in to comment.

Answers (1)

From your clarification, it sounds like you could do
[rows, columns] = find(binaryImage);
leftColumn = min(columns);
rightColumn = max(columns);

4 Comments

Thanks for your contribution. I believe I tried something similar to the code you've provided (probably found in one of your answers to a similar question) and it only gives me the very first white pixel found in the image and then the very last found in the image. The project I'm working on requires that I find this for every region.
I think I found a solution by getting the coordinates from bounding boxes around the regions, using the following:
stats = regionprops(binaryImage, 'BoundingBox');
boxes = vertcat(stats.BoundingBox);
lefts = boxes(:,1);
rights = lefts + boxes(:,3);
format shortG
StartEnd = [lefts, rights]
Try it. It will give you the same left and right columns, except that yours will be shifted by half a pixel so that it lies not on pixels but in between them. See my discussion here: http://www.mathworks.com/matlabcentral/answers/271694#comment_348710
I see what you mean. I noticed the outputs were all decimals and I thought it was a little strange, so thank you for clearing that up for me! I don't think the half a pixel either side would matter too much for what I need it for. However, as a work around, I could just apply round and floor to correct the results again, couldn't I?
Sure, do whatever you need to to get the right answer.

Sign in to comment.

Asked:

on 7 Mar 2016

Commented:

on 8 Mar 2016

Community Treasure Hunt

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

Start Hunting!