sgman
sgman

Reputation: 89

Finding 3rd, or any other highest area value of image segment in matlab

I am writing the code according to this https://in.mathworks.com/help/images/correcting-nonuniform-illumination.html

basically after my step of doing say

grain_areas = [graindata.Area]
[area, id] = max(grain_areas)

from these lines i will get the segment of the image which has the highest area, however i want the segment which has say in this case the 3rd highest area.

Upvotes: 0

Views: 54

Answers (2)

Nicky Mattsson
Nicky Mattsson

Reputation: 3052

If you are using MATLAB 2017b or newer, you can just use the function maxk which will give you the kth largest number.

[area, id] = maxk(grain_areas,3);

Upvotes: 1

Matt
Matt

Reputation: 1362

Use sort with the 'descend' options to get the areas from largest to smallest. Then you can access the nth largest area as you please

grain_areas_sorted = sort(grain_areas, 'descend');
grain_areas_sorted(3)

ans =

   236

Upvotes: 1

Related Questions