issues with images as global variables
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Rowan
el 1 de Jun. de 2015
Comentada: Guillaume
el 1 de Jun. de 2015
Okay, so i'll make this as simple as possible. I'm writing a function to get the 2 ost recent images from a continuously running webcam and caculating the position of an object moving in front of the webcam.
I don't have any issues with the position calculation part of the code (apart from maybe the speed of the calculations but that's not important for my question) but i'm having a slight issue with the aqquiring of the images.
firstly, in order to save time I set up the camera object and settings outside of the function, which is fine:
global vid
vid = videoinput('winvideo', 1);
set(vid, 'FramesPerTrigger', 2);
triggerconfig(vid, 'manual');
start(vid);
I do this so the camera can just be running in the background and in my function I can just grab images to use. for my function. I DON'T use the trigger function as I found this takes too long and I want to just grab images whenever I feel like it rather then having to , and instead just use the function
f(:,:,:,1) = peekdata(vid,1);
which just shows me whatever the most recent frame to be observed by the webcam was without having to call trigger.
once again, this isn't an issue I'm just setting some background so I can ask my question. what i'm having trouble with is that I want to get images in my function like this:
function X = ball_pos(~)
global vid
f(:,:,:,2) = peekdata(vid,1);
%
%
%section that calculates the position of the object from
%the acquired images goes here, resulting in a value
%being found for variable x_pos
%
f(:,:,:,1) = f(:,:,:,2);
X = x_pos;
basically, I want to get use the previous most-recent image as the initial image, and acquire the current most-recent webcam image as the final image.
this means however, I need something for f(:,:,:,1) before the first time this function is run which is where i'm stuck. I tried making f into a global variable, but the aqquired images had all skewed brightness/contrast issues basically looking like all-white images. it's possible I could just use peekdata for both image 1 and 2 at the start of the function but that would take too much time inorder to get two separate images (I would like this function to run at least faster than 0.1 seconds).
any advice on resolving this issue?
0 comentarios
Respuesta aceptada
Guillaume
el 1 de Jun. de 2015
I'm not sure what this has to do with global variables. For that matter, the vid global variable is not needed. You could just pass it as an argument to the function instead of making it global. This would be cleaner and less likely to lead to bugs in the future. global variables are dangerous.
I'm not very clear of the flow of your code. Who is calling ball_pos?
Possibly, what you're looking for is something like this:
function X = ball_pos(vid) %X is not a good name. Use something more descriptive
persistent previous_frame;
if isempty(previous_frame)
%first time the function is run. Just acquire a frame and return from the function
previous_frame = peekdata(vid, 1);
X = NaN; %no previous frame yet
return
end
new_frame = peekdata(vids, 1);
%calculate displacement between new_frame and previous_frame
X = x_pos;
%finally, replace previous_frame with new_frame, ready for next call
previous_frame = new_frame;
end
Basically, on each call the function only capture one frame ( new_frame) and calculate the displacement from a previously captured frame ( previous_frame). At the end, the new frame becomes the previous frame. The previous_frame variable is preserved between calls because it is persistent (not global).
Más respuestas (1)
Rowan
el 1 de Jun. de 2015
1 comentario
Guillaume
el 1 de Jun. de 2015
I'm glad your problem is solved. Note that programs don't get confused, they do exactly as you tell them even if what you tell them is not what you really want (unfortunately).
Most likely, the problem was with how you initialised your variable. You should get exactly the same issue whether the variable is global, persistent or just a normal local variable. It's hard to tell you exactly what was wrong without seeing the whole code. It probably does not matter anymore and in my opinion using two variables makes more sense anyway.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!