zeros為創建一個值為零的數組;
如matrix1=zeros(4,5);%4*5的矩陣,矩陣中每個元素都為0
matrix2=zeros(4,5,3);%4*5*3的數組,數組中每個元素都為0
下面舉一個將圖像存到數組的例子
對RGB圖片1.jpg,2.jpg;大小為700*500*3
創建4D空數組700*500*3*2
img=zeros(700,500,3,2);
直接將圖片賦予數組會發現不是原圖,代碼及運行結果如下:
img1=imread(strcat('C:\Users\Administrator\Desktop\','1.jpg'));
img2=imread(strcat('C:\Users\Administrator\Desktop\','2.jpg'));
% img1_1=im2double(img1);
% img2_2=im2double(img2);
img=zeros(700,500,3,4);
img(:,:,:,1)=img1;
img(:,:,:,2)=img2;
% img(:,:,:,3)=img1_1;
% img(:,:,:,4)=img2_2;
figure(1)
subplot(4,2,1),imshow(img1);
subplot(4,2,2),imshow(img2);
% subplot(4,2,3),imshow(img1_1);
% subplot(4,2,4),imshow(img2_2);
subplot(4,2,5),imshow(img(:,:,:,1));
subplot(4,2,6),imshow(img(:,:,:,2));
% subplot(4,2,7),imshow(img(:,:,:,3));
% subplot(4,2,8),imshow(img(:,:,:,4));
運行結果:
如果將圖片轉為雙精度型,則可正常,代碼結果如下:
img1=imread(strcat('C:\Users\Administrator\Desktop\','1.jpg'));
img2=imread(strcat('C:\Users\Administrator\Desktop\','2.jpg'));
img1_1=im2double(img1);
img2_2=im2double(img2);
img=zeros(700,500,3,4);
img(:,:,:,1)=img1;
img(:,:,:,2)=img2;
img(:,:,:,3)=img1_1;
img(:,:,:,4)=img2_2;
figure(1)
subplot(4,2,1),imshow(img1);
subplot(4,2,2),imshow(img2);
subplot(4,2,3),imshow(img1_1);
subplot(4,2,4),imshow(img2_2);
subplot(4,2,5),imshow(img(:,:,:,1));
subplot(4,2,6),imshow(img(:,:,:,2));
subplot(4,2,7),imshow(img(:,:,:,3));
subplot(4,2,8),imshow(img(:,:,:,4));
結果