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

조회 수: 20 (최근 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
  댓글 수: 2
Cesar Hernandez Reyes
Cesar Hernandez Reyes 2019년 12월 6일
How would I create a larger image? It is 10 x 10 right now, but I would like it 400 x 400

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

JESUS DAVID ARIZA ROYETH
JESUS DAVID ARIZA ROYETH 2019년 12월 6일
no te sirve así?
for i=1:10
imagen=uint8(randi(255,10,10,3));
figure
imshow(imagen)
end
  댓글 수: 1
Cesar Hernandez Reyes
Cesar Hernandez Reyes 2019년 12월 6일
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