code won't run, but gives no error
Mostra commenti meno recenti
I have written the following code that involves multiple functions. Basically, I'm tracking a moving animal to export mobile and immobile into excel. Evidently this is wrong because the code is not running. However, I do not know what the problem is because it detects nothing wrong and gives no error message.
Here is the code:
close all
clear all
clc
% path of the folder containing the videos
videoFolder = 'currentFolder';r
videoFiles = dir(fullfile(videoFolder, '*.mp4'));
% loop through each video file and process them
for i = 1:length(videoFiles)
% loading the video file
video = VideoReader(fullfile(videoFolder, videoFiles(i).name));
% parameters for motion detection
threshold = 50; % pixel threshold for motion detection
numFrames = video.NumFrames;
frameRate = video.FrameRate;
immobileTime = 0;
% variables to store data
startTime = []; % start time of each mobile phase
endTime = []; % end time of each mobile phase
% loop through each frame of the video
for j = 1:numFrames
frame = read(video, j);
grayFrame = rgb2gray(frame);
if j > 1
diffFrame = abs(grayFrame - prevFrame);
else
diffFrame = zeros(size(grayFrame));
end
motionMask = diffFrame > threshold;
numPixelsWithMotion = sum(motionMask(:));
percentPixelsWithMotion = numPixelsWithMotion / numel(motionMask);
if percentPixelsWithMotion < 0.05 % rodent immobile if less than this
immobileTime = immobileTime + (1 / frameRate); % add the duration of the immobile phase
else % more than 0.05, its mobile
startTime = [startTime, j];
if j > 1 % if this is not the first mobile phase
endTime = [endTime, j-1];
end
end
prevFrame = grayFrame;
end
endTime = [endTime, numFrames];
mobileTime = endTime - startTime;
outputFile = fullfile(videoFolder, [videoFiles(i).name(1:end-4) '_results.csv']);
resultsTable = table(mobileTime', immobileTime, 'VariableNames', {'MobileTime_ms', 'ImmobileTime_s'});
writetable(resultsTable,outputFile);
end
Risposta accettata
Più risposte (1)
Shandilya
il 8 Mag 2023
Spostato: Walter Roberson
il 9 Mag 2023
0 voti
1 Commento
Walter Roberson
il 9 Mag 2023
Spaces in folder names can indeed cause problems unless you are careful with quoting or escaping the spaces.
Categorie
Scopri di più su Shifting and Sorting Matrices in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!