필터 지우기
필터 지우기

abysmal performance of code

조회 수: 4 (최근 30일)
Frederik Vogel
Frederik Vogel 2023년 11월 19일
댓글: Frederik Vogel 2023년 12월 7일
The code is supposed to take a series of snapshopts fom 2 individual ip cameras and save these images in 2 seperate files.
However instead of a set of pictures being taken every second or so the time in between pictures is more in the range of 5 seconds which is a bit too long for my project. If you have any idea on how to speed it up let me know.
mkdir irbilder1
mkdir norbilder1
camA = videoinput("gentl", 1, "BayerBG8"); % Modify properties based on your first camera
camB = videoinput("gentl", 2, "Mono8"); % Modify properties based on your second camera
camB.ReturnedColorspace = "rgb";
src = getselectedsource(camB);
src.AtmosphericTemperature = 223;
src.CMOSBitDepth = "bit8bit";
src.ReflectedTemperature = 223;
src.WindowTemperature = 223;
Frames Aufnehmen und ggf in Grauwert konvertieren
nummer=1;
ende=10
while nummer<=ende
snapA=getsnapshot(camA);
snapA=im2gray(snapA);
snapB=getsnapshot(camB);
snapB=im2gray(snapB);
itr=string(nummer)
nameA="nom"+itr+".png"
BezA=fullfile("norbilder1",nameA)
nameB="IRB"+itr+".png"
BezB=fullfile("irbilder1",nameB)
imwrite(snapA,BezA);
imwrite(snapB,BezB);
beep
nummer=nummer+1;
end

답변 (1개)

Pratik
Pratik 2023년 11월 29일
Hi Frederik,
As per my understanding, you are trying to capture images from two cameras simultaneously and save them to a folder, but the process is too slow.
After analysing the performance of the code, the bottleneck seems to be the “getsnapshot” function. Each call to the function is taking approximately 2 seconds.
To optimise this process, it would be more efficient to record a video rather than taking individual photos. This way, one can easily extract frames at desired intervals, ensuring that the necessary images are captured.
For comparison, time taken to click 10 images by a single camera is around 24.76 seconds. By capturing frames from a video, 10 frames can be captured in ~9 seconds.
Please refer to the below code snippet to record a video using a camera and access its frames:
% Set up the video input object
v = videoinput("winvideo", 1, "YUY2_1280x720"); % modify property based on your camera
% Set the properties for the video object
% Set frames per trigger to 5 seconds worth of frames
% Assuming the default frame rate is 20 frames per second
fps = 20; % Default frames per second, change if different
duration = 5; % Duration of recording in seconds
framesPerTrigger = duration * fps;
set(v, 'FramesPerTrigger', framesPerTrigger);
% Set the trigger repeat to 0 to only run acquisition once
set(v, 'TriggerRepeat', 0);
% Set the returned color space to RGB
set(v, 'ReturnedColorSpace', 'rgb');
tic; % Start timer for this iteration
% Start the video acquisition
start(v);
% Wait until the acquisition is finished
wait(v, duration + 5);
% Retrieve the frames and timestamps
[frames, timestamps] = getdata(v, v.FramesAvailable);
% Stop the video object
stop(v);
elapsedTime = toc; % End timer for this iteration
fprintf('Time taken for iteration: %.6f seconds\n', elapsedTime);
% Extract and display a frame
for i = 1:2*duration
% Calculate the frame index
frameIndex = i * fps/2;
% Extract the frame
frame = frames(:,:,:,frameIndex);
% the frame can be saved after preprocessing
% Display the frame
imshow(frame);
title(sprintf('Captured frame %d', i));
% Pause for a moment to display the frame
pause(1);
end
% Clean up by deleting the video object
delete(v);
Please refer to the following documentation of ‘videoinput’ for more information:
Hope this helps!
  댓글 수: 1
Frederik Vogel
Frederik Vogel 2023년 12월 7일
Hey Patrik,
thank you for your attempt, however i have tried this approach as well. however this system will take a video from one camera terminate the video and then record a second subsequent sequence from the second camera.

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

카테고리

Help CenterFile Exchange에서 MATLAB Support Package for IP Cameras에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by