select random pixels
Show older comments
I have an image , I have to select random pixels and replace with some values I have. How can I do that?
Answers (1)
Titus Edelhofer
on 14 Feb 2012
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
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!