MATLAB: How to find coordinates of white pixels within Bounding Box

image processing

I have a binary image with several objects. I'm using regionprops to create Bounding Boxes around each object. Now I need to find the coordinates of only white pixels within each Bounding Box. This is what I have so far. Thx for your time!
%%Load Image
image = imread ('testimage.png');
image = im2bw (image, 0.01);
%%Create Bounding Boxes
image = bwlabel (image);
Properties = regionprops (image, 'BoundingBox');
NumberOfBoxes = max(size(Properties));
figure;
imshow (image);
for k = 1:NumberOfBoxes
Box = Properties(k).BoundingBox;
rectangle('Position', [Box(1), Box(2), Box(3), Box(4),],...
'EdgeColor', 'r', 'LineWidth', 2)
end

Best Answer

Use regionprops with 'PixelList' property:
Locations of pixels in the region, returned as a p-by-Q matrix. Each row of the matrix has the form [x y z ...] and specifies the coordinates of one pixel in the region.
If you need those relative to their bounding box for some reason, then you can subtract the bounding box and add 1:
relative_indices = arrayfun(@IDX) bsxfun(@minus, Properties(IDX).PixelList, Properties(IDX).BoundingBox(1:2))+1, 1:length(Properties), 'Uniform', 0);