for loop over video data
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi guys,
I want to extract data from a video, which is saved as a 4d Array. My plan is to use a for loop to extract data from the first 30x30 pixels of 50 frames and process these. After that I want to do the same with the next 30x30 pixels.
Right now I have this, but it is not working as I thought it would
for i = 1:50
for r = 1:29:size(data,2)
for c = 1:29:size(data,3)
if c+29 <= 960 && r+29 <= 540
red = mean(data(i,r:r+29,c:c+29,1),"all");
red(red==0) = [];
green = mean(data(i,r:r+29,c:c+29,2), 'all');
green(red==0) = [];
blue = mean(data(i,r:r+29,c:c+29,3), 'all');
blue(red==0) = [];
%%% Processing
end
end
end
end
Thank you in advance :)
2 commentaires
Réponse acceptée
Voss
le 9 Mar 2023
Does this work as expected?
[n_frames,n_rows,n_columns,n_color_channels] = size(data);
block_size = 30;
n_frames_to_process = 50;
for i = 1:n_frames_to_process
for r = 1:block_size:n_rows-block_size+1
for c = 1:block_size:n_columns-block_size+1
red = mean(data(i, r:r+block_size-1, c:c+block_size-1, 1),"all");
red(red==0) = [];
green = mean(data(i, r:r+block_size-1, c:c+block_size-1, 2), 'all');
green(green==0) = [];
blue = mean(data(i, r:r+block_size-1, c:c+block_size-1, 3), 'all');
blue(blue==0) = [];
%%% Processing
end
end
end
Changes made:
- Changed the increment of the 2 innermost for loops from 29 to 30. Note that r = 1:29:size(data,2) gives r = [1 30 59 88 ...], but it sounds like you want r = [1 31 61 91 ...], which is r = 1:30:size(data,2). Similarly for c.
- Subtracted 29 from the end point of the 2 innermost for loops, to avoid having the if inside the loops.
- Changed green(red==0) = []; to green(green==0) = []; (and the same for blue), which seems to make more sense, since red is already not 0.
- Made relevant constants into variables (n_frames, n_rows, n_columns, n_color_channels, block_size, n_frames_to_process) to avoid hard-coding relevant values in the code.
Note that after running this line
red = mean(data(i, r:r+block_size-1, c:c+block_size-1, 1),"all");
red is a scalar, so that this
red(red==0) = [];
is the same as
if red == 0
red = [];
end
I point that out in case you are expecting red not to be a scalar at that point.
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!