用像素值“0”填充剩余区域

时间:2013-08-22 10:09:32

标签: matlab

在下图中,我填充了两个具有两种不同色调的区域:

enter image description here

如何使用0中的像素值MATLAB(黑色)填充图像的剩余部分(未填充的部分)?

感谢。

4 个答案:

答案 0 :(得分:2)

假设这是this question的后续操作,当您使用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;

答案 1 :(得分:1)

尝试此选择两个灰色形状中的点并使其他所有颜色变黑。

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)

感觉过于复杂,但确实有效。它使用两个步骤,首先是粗略的&#34;过滤器&#34;然后使用通过将最初过滤的图像转换为B&amp; W(需要阈值)生成的掩模进行更彻底的移除,然后使用填充操作来识别补丁内的像素。

答案 2 :(得分:1)

假设两个区域的像素值的值称为val_1val_2,您可以这样做:

算法

  1. 获取两张图片,其中一张图像除了val_1设置为0外,其他图像都与val_2相同。这也很可能包含很多噪声点。
  2. Erode这两个图像具有适当阈值大小的掩码。这消除了该区域内不存在该值的点。
  3. Grow生成的图像的高区域到原始图像中的连接组件。
  4. 根据需要更改相应图像中高区域的阴影,并添加它们以获得最终图像。
  5. 如果事先不知道阴影,可以使用ginput,如图所示@ TryHard的回答。

答案 3 :(得分:1)

您可以使用matlab文件交换中的Simple single-seeded region growing函数(或其等效函数)(下面或download)。此函数将创建一个逻辑掩码,可用于使图像变黑(对多个区域连续调用此函数)

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