就像普通的索引图像一样,在图形的颜色图中。不同之处在于矩阵值是线性的

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

找到具有 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

[x, y] = find(bw2 == 1) xy 是数组,除非只有一个像素是白色。

但是,if i==xif j==y 正在将单个数字与数组进行比较。这是错误的。

正如 Anthony 指出的那样,x 和 y 是数组,因此 i==xj==y 不会按预期工作。此外 RGB(i,j) 仅使用前两个维度,但 RGB 图像具有三个维度。最后,从优化的角度来看,for 循环是不必要的。

%% Create some mock data. 
% Generate a black/white  image. 
bw2 = rand(10);
% Introduce some 1's in the BW image
bw2(1:5,1:5)=1; 
% Generate a RGB image. 
RGB = rand(10,10,3)*255; 

%% Do the math. 
% Build a mask from the bw2 image
bw_mask = bw2 == 1;
% Set RGB at bw_mask pixels to white. 
RGB2 = bsxfun(@plus, bw_mask*255, bsxfun(@times, RGB, ~bw_mask)); % MATLAB 2016a and earlier
RGB2 = bw_mask*255 + RGB .* ~bw_mask; % MATLAB 2016b and later. 

您可以使用逻辑索引。

要使逻辑索引正常工作,您需要掩码 (bw2) 与 RGB 大小相同。
由于 RGB 是 3D 矩阵,因此需要复制 bw2 三次。

示例:

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

结果(只是装饰):


如果您想修复 for 循环实现,您可以按如下方式进行:

[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