在下图中,我用两种不同的阴影填充了两个区域:
如何用像素值(黑色)填充图像的剩余部分(未填充的部分) ?0
MATLAB
谢谢。
假设这是此问题的后续,当您获取 roi 时,除了使用它来创建上面的图像之外,您还可以使用它来制作具有黑色背景的图像。这避免了在图像其他地方具有相同值的任何问题(归功于Bee对上一个问题的回答):
img = im2double(imread('cameraman.tif'));
imshow(img);
roi = imfreehand(gca);
img2 = img;
img3 = zeros(size(img));
img2(roi.createMask) = val_1;
img3(roi.createMask) = val_1;
% and repeat to add additional roi
或者,如果您想稍后再次使用它们,您可以将这些区域存储为单独的 BW 掩码:
imshow(img);
% create masks
roi = imfreehand(gca);
BW = roi.createMask;
roi2 = imfreehand(gca);
BW2 = roi.createMask;
% original image + roi
img2 = img;
img2(BW) = val_1;
img2(BW2) = val_2;
% B&W image
img3 = BW*val_1+BW2*val_2;
Assuming that the values of the pixel values of the two regions are known as val_1
and val_2
, you could do something like so:
val_1
set to 0, the other doing the same with val_2
. This will most likely contain a lot of noise points as well.If the shades are not known beforehand, you could use ginput
as illustrated @TryHard's answer.
试试这个在两个灰色形状中挑选点,然后把其他的都涂黑。
img = imread(myimagefilename);
imshow(img);
% you can skip the following part and set clr1 and clr2 manually
% if you already know the grayscale values in the patches
pts=ginput(2); % <-- pick points within the regions you colored in
clr1=img(pts(1,2),pts(1,1));
clr2=img(pts(2,2),pts(2,1));
img2=img;
img2(find(img~=clr1 & img~=clr2)) = 0;
img2=im2bw(img2,0.2); % <-- 0.2 is the threshold
[xxx idx1]= bwfill(~img2,pts(1,1),pts(1,2),8);
[xxx idx2]= bwfill(~img2,pts(2,1),pts(2,2),8);
idx=setxor(union(idx1,idx2),[1:numel(img)]);
img2 = img;
img2(idx)=0;
imshow(img2)
感觉过于复杂,但它确实有效。它使用两个步骤,首先是粗略的“过滤器”,然后使用通过将最初过滤的图像转换为黑白(需要阈值)生成的掩码进行更彻底的去除,然后进行填充操作以识别补丁中的像素。
您可以使用matlab 文件交换(下方或下载Simple single-seeded region growing
)中的函数(或其等效函数之一)。此函数将创建一个逻辑掩码,您可以使用它来使图像变黑(针对多个区域连续调用此函数)
I = im2double(imread('5Yo8l.png'));
J = segCroissRegion(0.01,I,0,0)
imshow(I+J);
function Phi = segCroissRegion(tolerance,Igray,x,y)
if(x == 0 || y == 0)
imshow(Igray);
[y,x] = ginput(1); %%% beware of the (x,y) mix-up here
end
Phi = false(size(Igray,1),size(Igray,2));
ref = true(size(Igray,1),size(Igray,2));
PhiOld = Phi;
Phi(uint8(x),uint8(y)) = 1;
while(sum(Phi(:)) ~= sum(PhiOld(:)))
PhiOld = Phi;
segm_val = Igray(Phi);
meanSeg = mean(segm_val);
posVoisinsPhi = imdilate(Phi,strel('disk',1,0)) - Phi;
voisins = find(posVoisinsPhi);
valeursVoisins = Igray(voisins);
Phi(voisins(valeursVoisins > meanSeg - tolerance & valeursVoisins < meanSeg + tolerance)) = 1;
end