Create an RGB Image using a loop by choosing a random color for each pixel
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Cesar Hernandez Reyes
le 6 Déc 2019
Commenté : Cesar Hernandez Reyes
le 6 Déc 2019
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
0 commentaires
Réponse acceptée
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
Plus de réponses (1)
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
Voir également
Catégories
En savoir plus sur Modify Image Colors dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!