Help me to optimize this code to perform better Image Processing

4 views (last 30 days)
I am working on a Computer Vision project which involves analyzing and exporting Pixel intensities of RGB over a Region of Interest from a frame. Please look into the code i have attached here and suggest me some best ways to optimize this code. It's taking me a minimum of 5 seconds to maximum of 7 seconds per frame, to analyze and export desired data.
video_files = dir(fullfile(path,'*.avi'));
movieFullFileName = fullfile(path,;
videoObject = VideoReader(movieFullFileName);
% Determine how many frames there are.
numberOfFrames = videoObject.NumFrames;
%numberOfFrames = 1000; % <-------------------------------- For changing number of frames
vidHeight = videoObject.Height;
vidWidth = videoObject.Width;
numberOfFramesWritten = 0;
% Loop through the movie, writing all frames out.
% Each frame will be in a separate file with unique name.
meanGrayLevels = zeros(numberOfFrames, 1);
meanRedLevels = zeros(numberOfFrames, 1);
meanGreenLevels = zeros(numberOfFrames, 1);
meanBlueLevels = zeros(numberOfFrames, 1);
bits = zeros(numberOfFrames,1);
thisFrame = read(videoObject, 1);
imshow(thisFrame, []);
axis on;
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
h_rect = imrect();% this helps us to draw over the frame
% Rectangle position is given as [x, y, width, height]
pos_rect = h_rect.getPosition();
% Round off so the coordinates can be used as indices
pos_rect = round(pos_rect);
for frame = 1 : numberOfFrames
% Extract the frame from the movie structure.
thisFrame = read(videoObject, frame);
thisFrame = imcrop(thisFrame,pos_rect);
% Calculate the mean gray level.
grayImage = rgb2gray(thisFrame);
meanGrayLevels(frame) = mean(grayImage(:));
% Calculate the mean R, G, and B levels.
meanRedLevels(frame) = mean(mean(thisFrame(:, :, 1)));
meanGreenLevels(frame) = mean(mean(thisFrame(:, :, 2)));
meanBlueLevels(frame) = mean(mean(thisFrame(:, :, 3)));
threshold = mean(meanBlueLevels);
Levels = table(meanRedLevels,meanBlueLevels,meanGreenLevels,meanGrayLevels, 'VariableNames',{'Red-Pixel-Intensity','Blue-Pixel-Intensity','Green-Pixel-Intensity','Gray-Levels'});
type 5Hz-PI-info.csv
% bit-stream to csv file
if (meanBlueLevels(frame) ~=0)
if (meanBlueLevels(frame) >= threshold )
bits = 1;
bits = 0;
% dlmwrite('5Hz-binary.csv', bits, '-append')
T3 = table(pos_rect(1),pos_rect(2),pos_rect(3),pos_rect(4),'VariableNames',{'X-coordinates','Y-coordinates','Width','Height'});
type 5Hz-ROI-position-coordinates.csv
progressIndication = sprintf('Processed frame %4d of %d.', frame, numberOfFrames);

Accepted Answer

Jan on 10 Apr 2021
Edited: Jan on 10 Apr 2021
The command mean(X) is surprisingly much slower than sum(X)/numel(X). So replace:
mean(mean(thisFrame(:, :, 1)))
sum(thisFrame(:, :, 1), 'all') / (pos_rect(3)*pos_rect(4))
Why do you create a new file '5Hz-PI-info.csv' in each iteration only to type its contents to the screen? This is a pure waste of time, isn't it? The same with '5Hz-ROI-position-coordinates.csv', which is written repeatedly, although it contains the same values in each iteration. I assume this unneeded disk access wastes the most time in your code. Simply omit it. If you simply want to display the contents of the table, use
It will not matter the speed, but the clarity of the code to simplify
progressIndication = sprintf('Processed frame %4d of %d.', frame, numberOfFrames);
fprintf('Processed frame %4d of %d.', frame, numberOfFrames);
I assume this is a bug:
bits = zeros(numberOfFrames,1);
for frame = 1 : numberOfFrames
bits = 1;
Do you mean:
bits(frame) = 1;
By the way: A simplification of:
if (meanBlueLevels(frame) >= threshold )
bits(frame) = 1;
bits(frame) = 0;
bits(frame) = (meanBlueLevels(frame) >= threshold);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!