Search code examples
imagematlabimage-processingrgbpicturefill

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


IF i have pixel location for abject as [x, y] = find(Bw2 == 0); then how i remove it then filling with Original background

this my code

[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

Solution

  • I'm guessing that Bw2 is a binary mask of the same size as RGB (without the 3rd dimension). I am also guessing you want to replace all pixels in RGB with a fixed background color if the corresponding pixel in Bw2 is zero.

    If so, you can do this:

    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);