How to speed up Matlab code for analyzing frames (Video)?

조회 수: 5 (최근 30일)
Yassine
Yassine 2014년 4월 18일
댓글: Image Analyst 2014년 4월 19일
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

답변 (1개)

Image Analyst
Image Analyst 2014년 4월 18일
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
Yassine
Yassine 2014년 4월 19일
Thank you for your answer, but I am trying to mark the location of the maximum of every frame in minimum time. For Instance, my camera capture 30 frame per second and my code can analyze only about 10 frames per second. So my goal is to spy 30 frames per second which seems impossible.
Image Analyst
Image Analyst 2014년 4월 19일
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

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by