How can I find consecutive integers in a vector?

3 Ansichten (letzte 30 Tage)
Andrea Zuccotto
Andrea Zuccotto am 4 Feb. 2017
Beantwortet: Walter Roberson am 4 Feb. 2017
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?

Antworten (2)

Image Analyst
Image Analyst am 4 Feb. 2017
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 am 4 Feb. 2017
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.

Kategorien

Mehr zu Get Started with Image Processing Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by