在matlab中获得图像的前25%强度值

时间:2014-11-07 20:04:53

标签: matlab image-processing

我想创建一个等于图像强度值前25%的阈值掩码。我使用了这段代码,但没有生成所需的值:

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

2 个答案:

答案 0 :(得分:5)

尝试prctile

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

答案 1 :(得分:2)

更新版本 -

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