select random pixels
显示 更早的评论
I have an image , I have to select random pixels and replace with some values I have. How can I do that?
回答(1 个)
Titus Edelhofer
2012-2-14
Hi,
randperm should help you, here some (non tested) example code:
% let A be the image (either NxM or NxMx3):
% number of pixels to be replaced:
k = 42;
% the size:
s = size(A);
if length(s)==3
%RGB
A = reshape(A, s(1)*s(2), 3);
else
A = A(:);
end
% the random pixels:
idx = randperm(size(A,1));
A(idx,:) = 0; % black or other values
% and reshape back
A = reshape(A, s);
Titus
类别
在 帮助中心 和 File Exchange 中查找有关 Image Arithmetic 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!