Search code examples
imagematlabfindrgbpixel

Like an ordinary indexed image,in the figure’s colormap. The difference is that the matrix values are linearly


find pixels location that have value/color =white

for i=1:row
    for j=1:colo
      if i==x    if the row of rgb image is the same of pixel location row


      end
    end
end
end
what's Wrong

Solution

  • You can use logical indexing.

    For logical indexing to work, you need the mask (bw2) to be the same size as RGB.
    Since RGB is 3D matrix, you need to duplicate bw2 three times.

    Example:

    %Read sample image.
    RGB = imread('autumn.tif');
    
    %Build mask. 
    bw2 = zeros(size(RGB, 1), size(RGB, 2));
    bw2(1+(end-30)/2:(end+30)/2, 1+(end-30)/2:(end+30)/2) = 1;
    
    %Convert bw2 mask to same dimensions as RGB
    BW = logical(cat(3, bw2, bw2, bw2));
    
    RGB(BW) = 255;
    
    figure;imshow(RGB);
    

    Result (just decoration):
    enter image description here


    In case you want to fix your for loops implementation, you can do it as follows:

    [x, y] = find(bw2 == 1);
    [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
                    RGB(i,j,1)=255; %set Red color channel to 255
                    RGB(i,j,2)=255; %set Green color channel to 255
                    RGB(i,j,3)=255; %set Blue color channel to 255
                end
            end
        end
    end