How can I find consecutive integers in a vector?

3 vues (au cours des 30 derniers jours)
Andrea Zuccotto
Andrea Zuccotto le 4 Fév 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?

Réponses (2)

Image Analyst
Image Analyst le 4 Fév 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 le 4 Fév 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.

Catégories

En savoir plus sur Get Started with Image Processing Toolbox dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by