How can I find consecutive integers in a vector?

2 ビュー (過去 30 日間)
Andrea Zuccotto
Andrea Zuccotto 2017 年 2 月 4 日
回答済み: Walter Roberson 2017 年 2 月 4 日
Hi everybody. I'm working with a binary image, and for each row and column I have to count how many pixels are 'on' (pixel's value is 1). This information is stored in 2 vectors: one storing the rows value and the second one for columns. Now I've made 2 binary vectors indicating wether the value of the previous vectors is higher than a given Treshold value (in this case value is 1), or not (value is 0).
Now I have to count how many rows/columns consecutively have a value higher than the threshold, so it's about counting how many consecutively 1s are stored in the binary vectors, but I don't know how to do that.
How can i do that, does matlab have a function for it?

回答 (2 件)

Image Analyst
Image Analyst 2017 年 2 月 4 日
What's wrong with the sum() function?
countPerColumn = sum(binaryImage, 1);
countPerRow = sum(binaryImage, 2); % Going across columns within a row.
rowCountsAboveThreshold = sum(countPerRow > threshold);
colCountsAboveThreshold = sum(countPerColumn > threshold);
Of course if your threshold is only 1, then you can use the any() function.

Walter Roberson
Walter Roberson 2017 年 2 月 4 日
RunLength encoding from the File Exchange can tell you how many 1s and 0's in a row in each case.
Another strategy is:
row = reshape(YourVector, 1, []); %need it to be a row for the below
start_pos = strfind([0 row 0], [0 1]);
end_pos = strfind([0 row 0], [1 0]) - 1;
Now start_pos and end_pos are in pairs and indicate the start and end positions of runs. The difference between the two, plus 1, gives the length of the run.

カテゴリ

Help Center および File ExchangeGet Started with Image Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by