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
Cesar Hernandez Reyes
Cesar Hernandez Reyes 2019 年 12 月 6 日
Just realized what to do. Thank you

サインインしてコメントする。

その他の回答 (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