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?

Answers (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

Asked:

on 14 Feb 2012

Community Treasure Hunt

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

Start Hunting!