matlab 画二维方格 不同颜色

2024-12-12 23:55:16
推荐回答(2个)
回答1:

A(:,:,1) = randint(4,4,[0 255]);

A(:,:,2) = randint(4,4,[0 255]);

A(:,:,3) = randint(4,4,[0 255]);

P = uint8(A);

imagesc(P);

axis square;

set(gca,'XTick',[],'YTick',[]);

imwrite(P,'matdata.bmp','bmp');   % p = imread('matdata.bmp'); imagesc(p);

回答2:

Height = 500;
Width = 500;
Num = 4;
R = ones(Height,Width);
G = R;B = R;

StartH = 1;
i = 0;
for ForI = floor(Height/Num):floor(Height/Num):Height
StartW = 1;
for ForJ = floor(Width/Num):floor(Width/Num):Width
r = floor(rand()*255);g = floor(rand()*255);b = floor(rand()*255);
R(StartH:ForI,StartW:ForJ) =r;
G(StartH:ForI,StartW:ForJ) =g;
B(StartH:ForI,StartW:ForJ) = b;
StartW = ForJ+1;
end
StartH = ForI+1;
end
Ima = zeros(Height,Width,3);
Ima(:,:,1) = R;Ima(:,:,2) = G;Ima(:,:,3) = B;
figure,imshow(uint8(Ima))