MATLAB: Counting the duration of numbers in rows of a binary matrix

binaryimageImage Processing ToolboxMATLABpixel counts

Similar to another question I asked here ( https://www.mathworks.com/matlabcentral/answers/412569-counting-the-number-of-gaps-in-a-binary-image ) I would like to count the duration of sequential values, separately for both black and white pixels. Currently the matrix I imported (attached) has black pixels with a value of 0, white are 255.
For example, if I have an example matrix:
255 255 255 0 0 255 0 255 255 255 0 0 0 255
255 0 0 0 0 255 255 255 255 0 255 255 0 255
I'd like to return for counting white pixels something like:
[2,1,3]
[4,1,1]
and for counting black pixels:
[3,1,3,1]
[1,4,2,1]
Is there a simple way to do this?

Best Answer

If by "duration" you mean length in the row, try this:
for row = 1 : rows
props = regionprops(binaryImage(row, :), 'Area'
allAreas{row} = [props.Area];
end
It has to be a cell array because there could be different number of regions in each row.