How can I change small amounts of zeros in to ones in binary code?

2 views (last 30 days)
The code below makes a signal with peaks binary. Due to some disturbances in the setup, some peaks have within the peak zero values. How can I change the zeros in to ones if the amount of consecutive zeros<20? Or how can I disregard peaks (turn them into zeros) which are to close to eachother.((steps between peaks)<20)
nrows = 65000; ncols=146;
G = results(nrows,ncols);
for c = 1:ncols
for r=1:nrows
if results(r,c) <= 4.657
G(r,c) = 1;
else
G(r,c) = 0;
end
end
end

Accepted Answer

Image Analyst
Image Analyst on 29 Dec 2014
Edited: Image Analyst on 29 Dec 2014
Do you have the Image Processing Toolbox? If you do it's pretty simple. Just ask regionprops() for the 'Area' to get the lengths of the stretches of zeros, and 'PixelIdxList' to get their indexes. Then set those indexes of stretched less than 20 in length to 1.
I can give you code if you give me some code that creates some demo data.
  3 Comments
Image Analyst
Image Analyst on 29 Dec 2014
I'm heading out for a few hours. In the meantime, see my Image Segmentation Tutorial. It will show you how to do it. http://www.mathworks.com/matlabcentral/fileexchange/25157-image-segmentation-tutorial---blobsdemo--
In brief, something like this (untested).
binarySignal = G == 0; % list of all elements that have value 0.
labeledSignal = bwlabel(binarySignal);
measurements = regionprops(labeledSignal, 'Area', 'PixelIdxList');
allAreas = [measurements.Area];
indexesOfSmallAreas = allAreas < 20; % Blob numbers, not indexes of G.
for k = 1 : length(indexesOfSmallAreas);
index = indexesOfSmallAreas(k);
theseElements = measurements(index).PixelIdxList;
% Set elements in the original G signal to 1.
G(theseElements) = 1; % Or whatever value you want them to be.
end
julia
julia on 30 Dec 2014
Thanks a lot. It works now only for the first too small area but I think, I can work out the rest.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!