就像普通的索引图像一样,在图的颜色映射中。不同的是,矩阵值是线性
查找像素位置具有值/颜色=白色就像普通的索引图像一样,在图的颜色映射中。不同的是,矩阵值是线性
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
您可以使用逻辑索引。
要使逻辑索引工作,需要将掩码(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
这里逻辑'if if(i == x)&& any(j == y)'是错误的,因为'x'和'y'应该成对使用,即'x(1)'只对应' y(1)','x(2)'只对应于'y(2)',依此类推。例如,当用户想使(1,1)和(2,2)变为白色时,你的逻辑将使(1,1),(1,2),(2,1)和(2,2)变成白色。正确的逻辑应该是'如果有(i == x)&& any(j == y(i == x))',其中'j'与那些具有'i'的x坐标的y坐标进行比较。我试图编辑你的答案,但不知道为什么它没有通过。 – Anthony
谢谢你在我的答案中发现错误...我修改了我的帖子。 (编辑没有通过的原因是因为它“违反作者的意图”)。正确的方法是发表评论。谢谢。 – Rotem
抱歉,我当时没有足够的评价来评论。 – Anthony
[x, y] = find(bw2 == 1)
x
和y
被阵列除非只有一个像素是白色的。
但是,if i==x
和if j==y
正在比较单个数字与数组。这是错误的。
谢谢亲爱的澄清 –
正如Anthony指出的那样,x和y是数组,因此i==x
和j==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.
你得到了什么错误,或者这个代码是做什么的,这是不是预期的? – Poelie
好吧,在rgb图像中只放一个白色的piont对象,但我需要在rgb图像上放置所有对象区域的白点或白色掩码 –