How to speed up Matlab code for analyzing frames (Video)?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi, I have an infrared camera looking at a near infrared lazer pointer that I keep moving it within a white board, so the only thing seen by the camera is a white thick point moving within the board. My goal here is to trace this lazer point by finding the maximum intensity in every frame (my camera takes 30 frame/second), so the problem here is my code take sometimes to analyze a frame which means I miss finding the highest intensity of several frames therefore I do not get a continuous line in my Matlab figure same way I move my lazer.
Any help regarding speeding up the analysis of frames would be appreciated. Bellow is my code:
%% Running camera obj = videoinput( 'macvideo', 2, 'YCbCr422_1280x720'); set(obj,'ReturnedColorSpace','grayscale'); preview(obj);
%% Trace Lazer by high intensity point
% Allocating Memory frame=zeros(720,1280); i=zeros(1,30);
for i=1:30 frame= getsnapshot(obj); maximum=max(frame(:)); % spy for the highest intensity point spy(frame==maximum,20) axis off axis image hold on end hold off
0 Commenti
Risposte (1)
Image Analyst
il 18 Apr 2014
max() returns the location as the second output argument:
[maxValue, locationOfMaxValue] = max(frame(:));
so then there's no need for the frame==maximum line anymore.
2 Commenti
Image Analyst
il 19 Apr 2014
Yes, that's what I was trying to do for you. It looks like you need a complete turnkey demo. Please run the code below.
% Demo macro to extract frames and find the brightest point in each frame
% Puts a cross up over that location.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures.
clear; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.
fontSize = 14;
% Open the rhino.avi demo movie that ships with MATLAB.
folder = fileparts(which('rhinos.avi'));
% movieFullFileName = fullfile(folder, 'rhinos.avi');
movieFullFileName = fullfile(folder, 'traffic.avi');
% Check to see that it exists.
if ~exist(movieFullFileName, 'file')
strErrorMessage = sprintf('File not found:\n%s\nYou can choose a new one, or cancel', movieFullFileName);
response = questdlg(strErrorMessage, 'File not found', 'OK - choose a new movie.', 'Cancel', 'OK - choose a new movie.');
if strcmpi(response, 'OK - choose a new movie.')
[baseFileName, folderName, FilterIndex] = uigetfile('*.avi');
if ~isequal(baseFileName, 0)
movieFullFileName = fullfile(folderName, baseFileName);
else
return;
end
else
return;
end
end
try
videoObject = VideoReader(movieFullFileName)
% Determine how many frames there are.
numberOfFrames = videoObject.NumberOfFrames;
vidHeight = videoObject.Height;
vidWidth = videoObject.Width;
numberOfFramesWritten = 0;
% Prepare a figure to show the images in the upper half of the screen.
figure;
% screenSize = get(0, 'ScreenSize');
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Loop through the movie.
for frame = 1 : numberOfFrames
% Extract the frame from the movie structure.
thisFrame = read(videoObject, frame);
% Display it
cla;
image(thisFrame);
caption = sprintf('Frame %4d of %d.', frame, numberOfFrames);
title(caption, 'FontSize', fontSize);
drawnow; % Force it to refresh the window.
% Calculate the mean gray level.
grayImage = rgb2gray(thisFrame);
meanGrayLevels(frame) = mean(grayImage(:));
[maxValue, locationOfMaxValue] = max(grayImage(:));
[row, column] = ind2sub([vidHeight, vidWidth], locationOfMaxValue);
hold on;
plot(column, row, 'r+', 'MarkerSize', 100, 'LineWidth', 5);
pause(0.14); % Wait fraction of a second so we can see it.
% Update user with the progress. Display in the command window.
progressIndication = sprintf('Processed frame %4d of %d.', frame, numberOfFrames);
disp(progressIndication);
end
finishedMessage = sprintf('Done with demo! It processed %d frames of\n"%s"', numberOfFrames, movieFullFileName);
disp(finishedMessage); % Write to command window.
uiwait(helpdlg(finishedMessage)); % Also pop up a message box.
catch ME
% Some error happened if you get here.
strErrorMessage = sprintf('Error extracting movie frames from:\n\n%s\n\nError: %s\n\n)', movieFullFileName, ME.message);
uiwait(msgbox(strErrorMessage));
end
Vedere anche
Categorie
Scopri di più su MATLAB Support Package for USB Webcams in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!