Rashid
Rashid

Reputation: 804

get the top 25% intensity value of image in matlab

I want to create a threshold mask equal to the top 25% of the image intensity values. I used this code but does not generate the desired value:

img1 = im2double(imread('image1.tif'));
threshold = (0.25);
img1(img1 < threshold) = 0;
img1(img1 > threshold) =1;

Upvotes: 1

Views: 240

Answers (2)

Divakar
Divakar

Reputation: 221624

Updated version -

img1 = im2double(imread('coins.png')); %// input image

threshold = 0.25;
[simg1,sind] = sort(img1(:),'descend');
idx = find(cumsum(simg1(:)) >= sum(img1(:))*threshold ,1,'first');
mask = false(size(img1));
mask(sind(1:idx))= 1;

figure,imshow(img1)
figure,imshow(mask)

enter image description here

enter image description here

Upvotes: 2

matheburg
matheburg

Reputation: 2180

Try prctile

img1 = im2double(imread('image1.tif'));
threshold = prctile(img1(:),75);
img1(img1 <  threshold) = 0;
img1(img1 >= threshold) = 1;

Upvotes: 5

Related Questions