我想创建一个等于图像强度值前25%的阈值掩码。我使用了这段代码,但没有生成所需的值:
img1 = im2double(imread('image1.tif'));
threshold = (0.25);
img1(img1 < threshold) = 0;
img1(img1 > threshold) =1;
答案 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)