how to reduce blob analysis rate in video so that object counting can happen every 3 seconds
4 views (last 30 days)
Show older comments
I would like to do object counting but my code is checking for region of intereste every 1/30 second due to 30 frames per second because of this everytime it is detecting the object, any way i can solve this problem?
clc;
%% Setup of video
vidReader=vision.VideoFileReader('hvsstop2.mp4');
vidReader.VideoOutputDataType='double';
mywriter=VideoWriter('mymovie.mp4');
open(mywriter);
%% structural element
diskelem=strel('disk',2);
hblob=vision.BlobAnalysis('MinimumBlobArea',1500,'MaximumBlobArea',4000);
vidPlayer = vision.DeployableVideoPlayer;
while ~isDone(vidReader)
%read frame
vidframe=step(vidReader);
%rgb to hsv color space
I=rgb2hsv(vidframe);
%htextins=insertText(I,'position',[20,20],'Color',[255 255 0],'Fontsize',30);
% Define thresholds for channel 1 based on histogram settings
channel1Min = 0.615;
channel1Max = 0.962;
% Define thresholds for channel 2 based on histogram settings
channel2Min = 0.000;
channel2Max = 0.058;
% Define thresholds for channel 3 based on histogram settings
channel3Min = 0.723;
channel3Max = 1.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min ) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min ) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min ) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
%using morphological operations
ibwopen=imopen(BW,diskelem);
%extract the blobs from the frame
[areaOut,centroidOut, bboxOut]=step(hblob,ibwopen);
%draw a box around detected objects
%ishape=insertShape(vidframe,'Rectangle',bboxOut,'ShapeColor','black');
%iannotate = insertObjectAnnotation(vidframe,"rectangle",bboxOut,'cardboard',TextBoxOpacity=0.9,FontSize=18);
iannotate_vid = insertObjectAnnotation(vidframe,"rectangle",...
bboxOut,'versatilis',TextBoxOpacity=0.9,FontSize=30);
%paly in video player
vidPlayer(iannotate_vid);
writeVideo(mywriter,iannotate_vid);
end
%%release
release(vidReader)
release(hblob)
release(vidPlayer)
close(mywriter)
%release(ishape)
0 Comments
Accepted Answer
Milan Bansal
on 26 Jul 2024
Edited: Milan Bansal
on 26 Jul 2024
Hi Ashokraj
I understand that you wish to detect objects and count them every 3 seconds instead of 30 times in a single second as you are doing now.
Here an assumption has to be made that the frame rate of the video is 30 frames per second. So in 3 seconds there will be 90 frames. To resolve the issue, in the while loop, introduce a frame counter such that the object detection and counting will be done after every 90 frames. Here is how you can modify your code to achieve this:
clc;
%% Setup of video
vidReader = vision.VideoFileReader('hvsstop2.mp4');
vidReader.VideoOutputDataType = 'double';
mywriter = VideoWriter('mymovie.mp4');
open(mywriter);
%% Structural element
diskelem = strel('disk',2);
hblob = vision.BlobAnalysis('MinimumBlobArea',1500,'MaximumBlobArea',4000);
vidPlayer = vision.DeployableVideoPlayer;
frameCount = 0;
frameSkip = 90; % Number of frames to skip (30 FPS * 3 seconds)
while ~isDone(vidReader)
frameCount = frameCount + 1;
% Read frame
vidframe = step(vidReader);
% Process every 90th frame
if mod(frameCount, frameSkip) == 0
% RGB to HSV color space
I = rgb2hsv(vidframe);
% Define thresholds for channels based on histogram settings
channel1Min = 0.615;
channel1Max = 0.962;
channel2Min = 0.000;
channel2Max = 0.058;
channel3Min = 0.723;
channel3Max = 1.000;
% Create mask based on chosen histogram thresholds
sliderBW = (I(:,:,1) >= channel1Min) & (I(:,:,1) <= channel1Max) & ...
(I(:,:,2) >= channel2Min) & (I(:,:,2) <= channel2Max) & ...
(I(:,:,3) >= channel3Min) & (I(:,:,3) <= channel3Max);
BW = sliderBW;
% Using morphological operations
ibwopen = imopen(BW, diskelem);
% Extract the blobs from the frame
[areaOut, centroidOut, bboxOut] = step(hblob, ibwopen);
% Annotate the detected objects
iannotate_vid = insertObjectAnnotation(vidframe, "rectangle", ...
bboxOut, 'versatilis', TextBoxOpacity=0.9, FontSize=30);
% Play in video player
vidPlayer(iannotate_vid);
% Write to video file
writeVideo(mywriter, iannotate_vid);
else
% Play the unprocessed frame to keep video in sync
vidPlayer(vidframe);
writeVideo(mywriter, vidframe);
end
end
%% Release resources
release(vidReader);
release(hblob);
release(vidPlayer);
close(mywriter);
Hope this helps!
1 Comment
Image Analyst
on 26 Jul 2024
Instead of "if mod" why not just call read with the frame number of the one you want to extract and analyze?
thisFrame = read(vidReader, frame);
That way you're faster since you don't need to read and throw away frames you're not interested in.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!