How to create an empty array or empty struct ?
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Antworten (1)
Ekemini Stephen
am 13 Okt. 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 Kommentare
Rik
am 14 Okt. 2019
@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.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!