MATLAB: Occupancy detection from a column of zeros and ones

findImage Processing Toolboxindexing

Hello All,
I have some data which is basically some 0s and 1s like below in one column:
1
1
1
0
1
0
0
0
1
0
1
1
1
1
1
1
0
0
1
1
1
1
1
0
0
0
1
0
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
0
1
1
1
1
1
1
1
1
1
0
1
0
0
1
0
1
0
1
1
1
0
0
1
0
0
0
1
1
0
0
1
0
1
1
1
1
0
1
1
1
1
0
0
1
0
0
0
0
These show occupancy status of a location. When it is zero it means that the place is unoccupied, and when it is 1 it means that the place is occupied. The problem is that these data are not accurate. So the location is occupied if and only if there are at least 3 consecutive 1s happening, and it is unoccupied it there are at least 3 consecutive zeros happening. Can you please tell me how I can identify occupancy and non-occupancy data using this criteria? Thanks, Masih

Best Answer

Use bwareaopen() or bwareafilt(). Both are in the Image Processing Toolbox.
occupancy = [...
1
1
1
0
1
0
0
0
1
0
1
1
1
1
1
1
0
0
1
1
1
1
1
0
0
0
1
0
1
1
0
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
1
0
0
1
1
1
1
1
1
1
1
1
0
1
0
0
1
0
1
0
1
1
1
0
0
1
0
0
0
1
1
0
0
1
0
1
1
1
1
0
1
1
1
1
0
0
1
0
0
0
0]
% Get rid of regions 2 or less, leaving occupied elements only.
occupancy = bwareaopen(occupancy, 3)
% The inverse is the non occupied elements.
nonOccupied = ~occupancy
Related Question