Custom processing within a video preview window

1 view (last 30 days)
Hi,
I'm trying to perform custom processing on a video preview. However, I'm having trouble utilizing the FramesAcquiredFcn. Although I don't get any errors, the video preview is always the same, unprocessed video preview.
vid = videoinput('pointgrey',1,'Mono8_640x480');
vidRes = get(vid, 'VideoResolution');
imWidth = vidRes(1);
imHeight = vidRes(2);
nBands = get(vid, 'NumberOfBands');
himage = image( zeros(imHeight, imWidth, nBands) );
setappdata(himage,'FramesAcquiredFcn',@mypreview_fcn);
%If I change the name of my processing function (from @mypreview_fcn to anything else, eg @mypreview), I still get no errors, and the same video. Therefore, I tried dropping the handle, @, and just put the name of my function, but this yields an error of "Error using mypreview_fcn Too many output arguments." If I place an output argument into mypreview_fcn, then I get an error that the variable "event" is undefined when I thought it is supposed to be an argument supplied by the preview function.
preview(vid,himage);
my preview function is shown below:
function mypreview_fcn(obj, event, himage)
data = event.Data;
w_s = 5;
SC = stdfilt(data(:,:,1),ones(w_s))./imfilter(double(data(:,:,1)),fspecial('average',w_s),'symmetric');
set(himage,'CData',SC);
end
Please let me know if you have any thoughts, it would be greatly appreciated. Thank you!

Answers (2)

Walter Roberson
Walter Roberson on 21 Jan 2016
set(vid, 'FramesAcquiredFcn',@mypreview_fcn);
  1 Comment
RPA
RPA on 21 Jan 2016
Hi,
Thank you for your response. I inserted your line into my code, but I'm finding that regardless of what I put after the "@" symbol, the preview remains the same, even if I put a function that doesn't exist. The code after incorporating your line looks like this:
vid = videoinput('pointgrey',1,'Mono8_640x480');
vidRes = get(vid, 'VideoResolution');
imWidth = vidRes(1);
imHeight = vidRes(2);
nBands = get(vid, 'NumberOfBands');
himage = image( zeros(imHeight, imWidth, nBands) );
set(vid, 'FramesAcquiredFcn',@mypreview_fcn);
setappdata(himage,'FramesAcquiredFcn',@mypreview_fcn);
%If I change the name of my processing function (from @mypreview_fcn to anything else, eg @mypreview), I still get no errors, and the same video. Therefore, I tried dropping the handle, @, and just put the name of my function, but this yields an error of "Error using mypreview_fcn Too many output arguments." If I place an output argument into mypreview_fcn, then I get an error that the variable "event" is undefined when I thought it is supposed to be an argument supplied by the preview function.
preview(vid,himage);
Perhaps there is something I am missing about the handle (@) syntax. Please let me know if you have any ideas about this. Thank you again!

Sign in to comment.


Petter Hagqvist
Petter Hagqvist on 6 Feb 2019
Very late answer, but I encountered this myself and also found the solution. It might help someone looking for the answer in the future.
You need to add:
start(vid);
Only calling preview() doesn't seem to be enough.

Community Treasure Hunt

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

Start Hunting!