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

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 中查找有关 Printing and Saving 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by