Using MATLAB to process files in real-time after e

2019-01-15 20:12发布

I am using MATLAB to process image files that are created by a camera and stored in a directory on Windows. I am trying to incorporate feedback into control of the camera and therefore require MATLAB to process an image every time a new image is created and appears in the directory. I have never created a MATLAB program that runs continuously and waits for an event to occur.

From what I've read online it appears my best option is to use a timer object and have the MATLAB program read the contents of the directory repeatedly. Is this a good approach or are there alternative approaches I can implement?

I'm wondering if there is a way the MATLAB program can be "triggered" by the appearance of a file in a directory as opposed to constantly surveying the contents of this directory. I hope there is because as the directory fills up I find that the "dir" command in MATLAB is really slow; slow enough that I may not be able to process images as fast as I require.

As a follow up. Are there any recommendations about how to deploy this program? An idea I like is a simple GUI with a "start" and "stop" button.

Thank you.

2条回答
爷的心禁止访问
2楼-- · 2019-01-15 20:23

You could do the following:

Create timer object, which will check your directory every 10 seconds:

t = timer('TimerFcn', @mycallback, 'Period', 10.0, 'ExecutionMode', 'fixedSpacing');

your 'mycallback' function should look something like this:

DIR_TO_READ = 'C:\incoming-files';
DIR_TO_MOVE_PROCESSED = 'C:\processed-files';

% get list of files.
file_struct = dir(DIR_TO_READ)
% remove '.' and '..' directories
file_struct([file_struct.isdir]) = [];
for j = 1 : numel(file_struct)
    current_file = file_struct(j).name;
    full_filename = fullfile(DIR_TO_READ, current_file)

    % add your processing of the file here
    % e.g.
    bla = imread(full_filename);

    % now move the processed file to the processed file folder
    movefile(full_filename, fullfile(DIR_TO_MOVE_PROCESSED, current_file))

end       

Now you need to start the timer object

start(t);

You can stop the timer object with

stop(t);
查看更多
再贱就再见
3楼-- · 2019-01-15 20:30

The correct way to do this is to purchase the MATLAB Image Acquisition Toolbox (http://www.mathworks.com/products/imaq/). This functionality is intentionally not included in the base Matlab environment.

Alternatively, with some clever programming you could implement a work-around. The build-in MATLAB functions will likely be too slow. Your best bet would be to write the functionality you need in Java (See http://www.exampledepot.com/egs/java.io/GetFiles.html) and then calling your Java code directly from Matlab (see http://www.mathworks.com/help/techdoc/matlab_external/f44062.html).

Fundamentally, unless you are accessing the camera drivers or framegrabber directly, you will always need to implement some kind of directory polling.

查看更多
登录 后发表回答