Create an RGB Image using a loop by choosing a random color for each pixel

20 vues (au cours des 30 derniers jours)
I what to create an RGB image as for every iteration of the loop it chose a differencet color for that particular pixal.
What I have right now only generates a new image after each iteration. So I end up with 10 images of a black square.
What I want my code to do is to create a image that chosses a random number for each pixal.
x = 1:10
y= 1:10;
image=zeros(10,10,3)
m = randi(3,10,1) % creates a vetor that of random numbers from 1:3 that deterimes what color to chose for each pixal.
%count = 0; % was having trouble generating the random number in the loop, so I just put it outside.
for i = 1:10
for j = 1:10
n(k) = m(i)
if n(k) == 1
image(x(i),y(j),1)=0;
image(x(i),y(j),2)=0; % if the random number is 1. This pixal will be black
image(x(i),y(j),3)=0;
if n(k) == 2
image(x(i),y(j),1)=250;
image(x(i),y(j),2)=0; %if the random number is 2. This pixal will be red
image(x(i),y(j),3)=0;
% repeated code for blue
% I would want to have the pixal be Red, Black, or blue
end
end
end
%figure;
imshow(image)
end

Réponse acceptée

Jon Wieser
Jon Wieser le 6 Déc 2019
Modifié(e) : Jon Wieser le 6 Déc 2019
x = 1:10;
y= 1:10;
image=zeros(10,10,3);
m = randi(3,10,10); % creates a vetor that of random numbers from 1:3 that deterimes what color to chose for each pixal.
%count = 0; % was having trouble generating the random number in the loop, so I just put it outside.
for i = 1:10
for j = 1:10
if m(i,j) == 1
image(x(i),y(j),1)=0;
image(x(i),y(j),2)=0; % if the random number is 1. This pixal will be black
image(x(i),y(j),3)=0;
elseif m(i,j) == 2
image(x(i),y(j),1)=250;
image(x(i),y(j),2)=0; %if the random number is 2. This pixal will be red
image(x(i),y(j),3)=0;
elseif m(i,j) == 3
image(x(i),y(j),1)=0;
image(x(i),y(j),2)=0; %if the random number is 3. This pixal will be blue
image(x(i),y(j),3)=250;
% I would want to have the pixal be Red, Black, or blue
end
end
%figure;
imshow(image)
end
  2 commentaires
Cesar Hernandez Reyes
Cesar Hernandez Reyes le 6 Déc 2019
How would I create a larger image? It is 10 x 10 right now, but I would like it 400 x 400
Cesar Hernandez Reyes
Cesar Hernandez Reyes le 6 Déc 2019
Just realized what to do. Thank you

Connectez-vous pour commenter.

Plus de réponses (1)

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH le 6 Déc 2019
no te sirve así?
for i=1:10
imagen=uint8(randi(255,10,10,3));
figure
imshow(imagen)
end
  1 commentaire
Cesar Hernandez Reyes
Cesar Hernandez Reyes le 6 Déc 2019
Sí, pero quiero usar un loop porque Quiero que el color del pixal sea determinado por una función.
Estoy trabajando en el problema de 3 imanes y péndulos
Estoy trabajando en una parte del programa

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by