MATLAB: How to find the leftmost and rightmost white pixel in a binary image in Matlab

image processingImage Processing Toolboximage segmentationMATLAB

I have a binary image in which there is only one nonzero region.How I find the leftmost and rightmost white pixel and then connect them by a line.

Best Answer

Lots of ways. For example call bwconvhull() and then regionprops to get bounding box.
binaryImage = bwconvhull(binaryImage, 'union');
props = regionprops(binaryImage, 'BoundingBox');
leftColumn = props.BoundingBox(1);
topRow = props.BoundingBox(2);
Or simply use find()
[rows, columns] = find(binaryImage);
leftColumn = min(columns);
topRow = min(rows);