issues with images as global variables

8 views (last 30 days)
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?

Accepted Answer

Guillaume
Guillaume on 1 Jun 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).
  1 Comment
Rowan
Rowan on 1 Jun 2015
thanks for the speedy response, sorry i didn't clarify a couple of things.
basically, there's a very simple reason I can't pass vid as an input argument, it's because this function is being called by simulink. it's part of a control system i'm working on and this function is using the webcam to output an objects position to the simulink model so it can feed back into the controller i'm working on, meaning any inputs into this have to come from the simulink model. but I think your suggestion could be very helpful reguardless.
that said, I tried using the persistent declaration in my own code and it resulted in a similar issue when i tried global declarition: i.e. the outputted image was white

Sign in to comment.

More Answers (1)

Rowan
Rowan on 1 Jun 2015
Okay so I figured out the problem to my issue, with some help from Guillaume 's suggestion. basically the key issue I was having was that if I delared a variable as global or persistent it was coming out as white if I tried to view it.
however, the reason for this appears to be because I declared it as
global f
but then I was trying to assign data to this function like so:
f(:,:,:,1) = peekdata(vid,1)
ordinary, this would mean that the image being saved from the webcam was the first in the image array f with the second image being f(:,:,:,2) and so on.
But! when I was declaring it as global or persistant I think the program was confused as to what I was asking it to do with these variables, and when I tried to view it I think it was giving me maybe some sort of distorted combo of the two images (that may be wrong but I think It might be something along those lines)
The solution therefore seems to be ridiculously simple: just have two difffernt variables, and declare them as global or persistent, say F for image 1 and C for Image 2, or even just declare one persistent variable like Guillaume 's example cause in my case I only need the function to remember one.
  1 Comment
Guillaume
Guillaume on 1 Jun 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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!