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?

Réponses (1)

Titus Edelhofer
Titus Edelhofer le 14 Fév 2012

0 votes

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

Catégories

En savoir plus sur Printing and Saving dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by