for loop over video data

4 visualizaciones (últimos 30 días)
Milena
Milena el 8 de Mzo. de 2023
Respondida: Voss el 9 de Mzo. de 2023
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 comentarios
Voss
Voss el 8 de Mzo. de 2023
What is the size of your 4d Array?
Milena
Milena el 9 de Mzo. de 2023
It is an 199x540x960x3 array, but i only want the first 50 of 199 frames.

Iniciar sesión para comentar.

Respuesta aceptada

Voss
Voss el 9 de Mzo. de 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.

Más respuestas (0)

Categorías

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

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by