如果我有像素位置,如何删除对象并填充背景

how to Remove object and filling with Background if i have pixel locations

如果我有 abject 的像素位置 [x, y] = find(Bw2 == 0); 然后我如何删除它然后填充原始背景

这是我的代码

[x, y] = find(Bw2 == 0);
[row, colo, z]=size(RGB); %size of rgb image
for i=1:row
    for j=1:colo
        if any(i==x)    %if the row of rgb image is the same of pixel location row
            if any(j==y(i==x)) %if the colos of rgb image is the same of pixel loca colo

     Free_image= imfill(RGB,i,j);
            end
        end
    end
end

我猜 Bw2 是一个与 RGB 大小相同的二进制掩码(没有第 3 维)。我还猜测如果 Bw2 中的相应像素为零,您想用固定背景色替换 RGB 中的所有像素。

如果是这样,你可以这样做:

bg_color = [0.5 0.5 0.5];  % this is gray, but can be changed to anything
sz = size(RGB);
numPix = sz(1) * sz(2);
indx = find(Bw2 == 0);
RGB(indx)            = bg_color(1);
RGB(indx + numPix)   = bg_color(2);
RGB(indx + numPix*2) = bg_color(3);