Matlab multiple segments extraction

I am dealing with some matlab issue. Can someone help?
Suppose i have a sequence: A=[1 2 3 4 29 31 0 0 0 0 0 35 4 5 61 7 0 0 0 1 2 31 91 65], i need to extract nonzero segments seperately. For eg: my output should be s1=[1 2 3 4 29 31]; s2=[35 4 5 61 7]; s3=[1 2 31 91 65].

 採用された回答

Image Analyst
Image Analyst 2012 年 10 月 6 日

0 投票

If you have the Image Processing Toolbox, you can use PixelValues, which you can get from passing A into regionprops():
% Sample data:
A=[1 2 3 4 29 31 0 0 0 0 0 35 4 5 61 7 0 0 0 1 2 31 91 65];
% Make the measurements. Get all the regions.
regions = regionprops(A>0, A, 'PixelValues')
% Pull them out from the structure into separate variables, if desired.
s1 = regions(1).PixelValues
s2 = regions(2).PixelValues
s3 = regions(3).PixelValues
In the command window:
regions =
3x1 struct array with fields:
PixelValues
s1 =
1 2 3 4 29 31
s2 =
35 4 5 61 7
s3 =
1 2 31 91 65

6 件のコメント

Sunil
Sunil 2012 年 10 月 6 日
Great.. Thnx.. will install image processing TB...But what if i have this sequence??? i.e sequence which contains both positive and negative values? A=[1 -2 3 4 -29 31 0 0 0 0 0 35 -4 5 61 7 0 0 0 1 -2 -31 91 65];
Image Analyst
Image Analyst 2012 年 10 月 6 日
Use
regions = regionprops(A~=0, A, 'PixelValues')
Sunil
Sunil 2012 年 10 月 6 日
Thnx...
Sunil
Sunil 2012 年 10 月 25 日
@Image Analyst. Sorry my question was not clear earlier. My problem is: I have a sequence A=[1 2 3 -4 0 29 31 0 100 0 0 0 0 35 4 5 61 7 0 0 0 0 1 2 31 91 65]. Then i need to extract nonzero segments seperately like s1=[1 2 3 -4 0 29 31 0 100], s2=[35 4 5 61 7] and s3=[1 2 31 91 65] i.e A~=[0 0 0 0]. It should not break for one zero but it should break for 4 or more zeros.
Image Analyst
Image Analyst 2012 年 10 月 26 日
編集済み: Image Analyst 2012 年 10 月 26 日
Use bwareaopen() to get rid of stretches of 3 or less.
% Sample data:
A=[1 2 3 -4 0 29 31 0 100 0 0 0 0 35 4 5 61 7 0 0 0 0 1 2 31 91 65]
% Find the zeros.
zeroLocations = A == 0
% Get rid of single zeros 0, pairs 0 0, or triples 0 0 0.
zeroLocations = bwareaopen(zeroLocations, 4)
% Find segments. A segment may have single zeros.
nonZeroLocations = ~zeroLocations
% Make the measurements. Get all the regions.
regions = regionprops(nonZeroLocations, A, 'PixelValues')
% Pull them out from the structure into separate variables, if desired.
s1 = regions(1).PixelValues
s2 = regions(2).PixelValues
s3 = regions(3).PixelValues
Sunil
Sunil 2012 年 11 月 21 日
Thank you.... it worked very well...

サインインしてコメントする。

その他の回答 (2 件)

Matt J
Matt J 2012 年 10 月 6 日
編集済み: Matt J 2012 年 10 月 6 日

0 投票

Do you have the Image Processing Toolbox? If so, just use regionprops(...,'PixelIdx')
Otherwise,

1 件のコメント

Sunil
Sunil 2012 年 10 月 6 日
It did not work as mine is not a repeated sequence... Its random.

サインインしてコメントする。

Sunil
Sunil 2012 年 10 月 6 日
編集済み: Image Analyst 2012 年 10 月 6 日

0 投票

Is there a simple way to do it without using Image Processing Toolbox?

1 件のコメント

Image Analyst
Image Analyst 2012 年 10 月 6 日
Nothing simple comes to mind. You could try the brute force / intuitive way of using for loops to find the regions and using cell arrays to store the regions once you've found them. I guess you could consider that simple but it will be several lines of code.

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by