Help me to optimize this code to perform better Image Processing
조회 수: 2 (최근 30일)
이전 댓글 표시
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.
path='/Users/sathwikchowda/Desktop/morselab/matlab-testing/videos/1Hz-Transmission.avi';
video_files = dir(fullfile(path,'*.avi'));
movieFullFileName = fullfile(path,video_files.name);
tic
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);
disp(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
threshold = mean(meanBlueLevels);
Levels = table(meanRedLevels,meanBlueLevels,meanGreenLevels,meanGrayLevels, 'VariableNames',{'Red-Pixel-Intensity','Blue-Pixel-Intensity','Green-Pixel-Intensity','Gray-Levels'});
writetable(Levels,'5Hz-PI-info.csv');
type 5Hz-PI-info.csv
% bit-stream to csv file
if (meanBlueLevels(frame) ~=0)
if (meanBlueLevels(frame) >= threshold )
bits = 1;
else
bits = 0;
end
% dlmwrite('5Hz-binary.csv', bits, '-append')
writematrix(bits,'5Hz-binary.csv','WriteMode','append')
T3 = table(pos_rect(1),pos_rect(2),pos_rect(3),pos_rect(4),'VariableNames',{'X-coordinates','Y-coordinates','Width','Height'});
writetable(T3,'5Hz-ROI-position-coordinates.csv');
type 5Hz-ROI-position-coordinates.csv
end
progressIndication = sprintf('Processed frame %4d of %d.', frame, numberOfFrames);
disp(progressIndication)
end
toc
댓글 수: 0
채택된 답변
Jan
2021년 4월 10일
편집: Jan
2021년 4월 10일
The command mean(X) is surprisingly much slower than sum(X)/numel(X). So replace:
mean(mean(thisFrame(:, :, 1)))
by
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
disp(Levels)
It will not matter the speed, but the clarity of the code to simplify
progressIndication = sprintf('Processed frame %4d of %d.', frame, numberOfFrames);
disp(progressIndication)
to
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;
else
bits(frame) = 0;
end
is
bits(frame) = (meanBlueLevels(frame) >= threshold);
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Image Segmentation and Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!