for loop not working
Mostrar comentarios más antiguos
I have 2 frames of a video containing 700 balls.
I want to create a series of subimages, defined by the coordinates of the balls, so I have 700 individual ball images.
The code below generates 1 ball image, but I cannot get it to loop through the rest of the 699 ball coordinates.
Can anybody help me adapt this loop?
r = 20; %radius of whole ball
for k = 1:numFrames
%subimage the balls in frames 1 and 2 by shrinking them to the coordinates of the balls
%should produce a series of images (700 ish) showing each individual
%ball
subframe1 = frame_1(round(yb_1{k})-r:round(yb_1{k})+r, round(xb_1{k})-r:round(xb_1{k})+r); %Issue - not getting all balls
%check its worked
figure(4)
imshow(subframe1)
end
4 comentarios
Brent Kostich
el 28 de Mayo de 2020
Is numFrames equal to 700? Or is numFrames a vector?
C.G.
el 29 de Mayo de 2020
Rik
el 29 de Mayo de 2020
Did you store the ball coordinates as yb_1, yb_2 etc? If you did, then you should really change that. It forces you to write horrible code, as you are discovering now.
C.G.
el 29 de Mayo de 2020
Respuestas (1)
Rik
el 29 de Mayo de 2020
0 votos
Then you are stuck with first undoing that mistake. You should convert those coordinate variables to a single array. You can use two strategies:
- Save the workspace that contains all those numbered variables (with the save function), then use load to load to a struct. Now you can use S.(sprintf('yb_%d',ball_index)) to retrieve the values and store it in the array.
- Use eval to evaluate that char. Use it only once to fix this mistake. If you can fix the source that would be even better.
Now you have an array with the ball coordinates, so it is trivial to write an inner loop that loops over all the balls.
Why are numbered variables like this so bad? (I have taken the liberty of adapting some of Stephen Cobeldick's thoughts on this topic) Introspective programing (e.g. eval, assignin and other functions like it) is slow, and the MATLAB documentation warns specifically against it. Using eval is slow, makes debugging difficult, and removes all code helper tools (code checking, syntax hints, code completion, variable highlighting, etc.). A longer discussion can be found here. If you are not an expert and you think you need eval, there are probably better solutions to your problem. Also, if you are not an expert, you might find interesting and helpful hints on this page (and if you are an expert, you can still learn from it).
8 comentarios
C.G.
el 29 de Mayo de 2020
Rik
el 29 de Mayo de 2020
Ah, this code makes more sense than yb_1, yb_2, yb_3, ... , yb_700.
Since the yb_1 variable looks like a vector, you should loop through the elements to crop every ball separately.
If you don't know how to implement this, I would strongly suggest doing a good Matlab tutorial like Onramp. It is worth it to invest some time in understanding the tools you're using, especially a tool so versatile as Matlab.
Brent Kostich
el 29 de Mayo de 2020
I see that you are trying to take each center-point and grab the pixels around it, 20 in each coordinate direction. However, the xb_1, yb_1 vectors were generated using all ten frames, you are just applying your index filtering to frame1 (which doesn't seem to be defined precisely). So that will pose a problem to start with.
Like Rik said, you are probably trying to loop through the elements of yb_1 and xb_1 to extract all of the balls from a single frame.
For example, if you want 700 seperate images, your loop will have to run that many times. Here is a very generic place to start:
xb_coords = xb_1{1}; % the '1' index takes all x_coords from frame 1
yb_coords = yb_1{1}; % " y_coords "
num_balls = length(xb_coords); % the number of xb_coords is the number of balls detected
for i = 1:num_balls
ball_xcoord = xb_coords(i);
ball_ycoord = yb_coords(i);
% code that will extract image from each ball center x, y coordinates
% ...
end
Since you are very new to MATLAB let me encourage you by saying your goal is very attainable, but it will require you to understand the details of what you are doing. If you know the author of the code you shared above, I would recommend you reach out and have him/her explain the steps of that code if you do not understand it fully.
Start with a simple way to accomplish your task (like the example above), and worry about efficiency later.
C.G.
el 29 de Mayo de 2020
C.G.
el 3 de Jun. de 2020
Rik
el 3 de Jun. de 2020
It actually is looping through all balls, you just don't see it, because the imshow will overwrite the previous image.
Rik
el 3 de Jun. de 2020
How would you like them to be ordered? You can use montage, but then you have to make sure all subimages are in the same 4D array:
%before your loop:
montage_data=zeros(2*r+1,2*r+1,1,numballs);%row,col,color_channel,balls
%in your loop:
montage_data(:,:,:,i)=subframe1;
Categorías
Más información sobre Image Arithmetic en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!