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

18 次查看(过去 30 天)
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

采纳的回答

Jon Wieser
Jon Wieser 2019-12-6
编辑:Jon Wieser 2019-12-6
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

更多回答(1 个)

JESUS DAVID ARIZA ROYETH
no te sirve así?
for i=1:10
imagen=uint8(randi(255,10,10,3));
figure
imshow(imagen)
end
  1 个评论
Cesar Hernandez Reyes
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

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by