Using a for loop to put a number of 2D arrays in a directory into a single 3D array

조회 수: 5 (최근 30일)
Hi,
I have a number of 2D arrays (image files) in a directory which I am trying to open sequentially in a for loop so I can window them down, and then put them all into a 3D array. I know this is a simple question, I' having trouble creating the 3D array while opening each 2D array in the for loop. So for instance If I have 3 arrays each 512 x 512 in a directory I just want to loop through all three files and make a 512 x 512 x 3 array.
for f_index = 1 :(total_files)
%get the name of the file
name_string = strcat(datapath,dirout(f_index + 2).name);
%read the data in
data_frame= read_image_raw(name_string,512,512);
%concatenate each frame with the last
results = cat(3,data_frame(:));
end
I realize this is a beginners question. Thanks.

답변 (2개)

Arash Rabbani
Arash Rabbani 2019년 11월 12일
If your images are PNG and located in a folder, just run this code on that folder. 'A' is the resulted matrice:
D=dir('*.png');
Image_Size=[512,512];
A=zeros(Image_Size(1), Image_Size(2),numel(D));
for I=1:numel(D)
IMG=imread(D(I).name);
if ndims(IMG)>=3; IMG=rgb2gray(IMG); end
A(:,:,I)=IMG;
end

Jeremy
Jeremy 2019년 11월 11일
편집: Jeremy 2019년 11월 11일
I misread at first, so I'm editing:
If you read in 3 2D arrays you can concatenate them - if A,B,C are each 2D then
D = A;
D(:,:,2) = B;
D(:,:,3) = C;
  댓글 수: 1
Mike Bakeman
Mike Bakeman 2019년 11월 11일
This is a weird CCD image and I have to use a particular subroutine to open each image which I'm trying to do in a for loop. After which I window out all the bad parts of the 2D data. That's all coded, I just need to create the 3D array and then I can do my statistics (that's all coded as well).
Thanks

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by