occupancy detection from a column of zeros and ones
2 views (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Image Analyst
on 6 Jun 2017
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
3 Comments
Image Analyst
on 7 Jun 2017
In a way, it does. That's what occupancy is. You can pass it into find if you want to convert to indexes
indexes = find(occupancy)
If you want it to say exactly "Region n goes from i1 to i2" then use this code:
% Get rid of regions 2 or less, leaving occupied elements only.
occupancy = bwareaopen(occupancy, 3)
% The inverse is the non occupied elements.
nonOccupied = ~occupancy
indexes = find(occupancy)
[bw, numRegions] = bwlabel(occupancy)
props = regionprops(bw, 'PixelList');
for p = 1 : numRegions
thisRegion = props(p).PixelList(:, 2);
fprintf('Region %d goes from element %d to element %d\n', ...
p, thisRegion(1), thisRegion(end));
end
and you'll see it print out:
Region 1 goes from element 1 to element 3
Region 2 goes from element 11 to element 16
Region 3 goes from element 19 to element 23
Region 4 goes from element 32 to element 49
Region 5 goes from element 52 to element 60
Region 6 goes from element 69 to element 71
Region 7 goes from element 84 to element 87
Region 8 goes from element 89 to element 92
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!