How to create an empty array or empty struct ?

33 visualizaciones (últimos 30 días)
ahmad Al sarairah
ahmad Al sarairah el 13 de Oct. de 2019
Comentada: Rik el 14 de Oct. de 2019
I have an image with 643x643 pixels , and i need to divide it to 100 blocks , each blocks with size 64x64 .
First,i need to create an empty array containing 100 cells and then store the 100 cropped images in it.More clearly B{0,0} is the first cropped image with 64x64, and B{0,1} is the second cropped image with 64x64 ,and so on.
i am using this code :
currentimage = imread('C:\Users\user\Desktop\D.gif');
[row,col,~]=size( currentimage );
height=fix(row/10);
width=fix(col/10);
for i=0:9
for j=0:9
B{i,j}=imcrop(currentimage,[(j*height) (i*width) height width]);
end
end

Respuestas (1)

Ekemini Stephen
Ekemini Stephen el 13 de Oct. de 2019
Hi Ahmad, I have edited your code to make it work. You need to store the 64*64 cropped images section in different fields within a structure. First, at each "i" of your outer for loop you need to create an empty structure with a field name and cropped the image within the inner for loop, and as this loop exits, you store the cropped image in the first created structure field. As your outer for loop continue, your images will be properly stored in the fields of that one structure. Look at the code below.
%% Import image file
currentimage = imread('C:\Users\lenovo\Desktop\FILES\Eket_MAT\ek.jpg');
[row,col,~]=size( currentimage );
height = fix(row/10);
width = fix(col/10);
%% Loop through image, crop and store
for i = 1:10
ss = ["a", string(i)]; % Define your field name
ss1 = join(ss,"a"); % Join the strings to create a valid name
data.(ss1) = {}; % Create the fields within the structure that holds each
% of your cropped image
for j = 1:10
B = imcrop(currentimage,[(i*width) (j*height) width height]); % Crop image
end
data.(ss1) = B; % Store each cropped image within the created field at each "i"
end
  3 comentarios
ahmad Al sarairah
ahmad Al sarairah el 14 de Oct. de 2019
Thank you mr.stephn . I have applied the code above, but the result is only 10 cropped images (aa1-aa10). I need to divide the original images into 100 pieces (aa1-aa100) and each piece is 64x64 .
I used the function (fix) to be an integer division (64) ,So all the cropped images have the same measurement (64x64).
Rik
Rik el 14 de Oct. de 2019
Please continue the discussion in the other thread.
@Ekemini Stephen: number struct field are almost as bad as numbered variables. You should avoid them if possible. As this problem can be solved with normal indexing (or blockproc, depending on the end goal of this code), that is a preferable solution, as that makes it easier to loop through the split elements.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by