code won't run, but gives no error
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Shandilya
am 2 Mai 2023
Kommentiert: Walter Roberson
am 9 Mai 2023
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
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 2 Mai 2023
Bearbeitet: Walter Roberson
am 2 Mai 2023
Either there is no folder in the current directory that is literally named currentFolder or else the folder literally named currentFolder exists but has no .mp4 files.
Note that in your code 'currentFolder' will not be replaced by the name of the current folder: it will be treated as the character vector ['c' 'u' 'r' 'r' 'e' 'n' 't' 'F' 'o' 'l' 'd' 'e' 'r']
2 Kommentare
Walter Roberson
am 4 Mai 2023
Please try this code and report back the kind of information it tells you
videoFolder = '/Users/shandi/Desktop/Dr Sareesh';
if ~exist(videoFolder, 'dir')
error('videoFolder directory "%s" does not exist', videoFolder);
end
videoFiles = dir(fullfile(videoFolder, '*.mp4'));
numfiles = length(videoFiles);
if numfiles == 0
error('videoFolder directory "%s" has no .mp4 files', videoFolder);
end
for i = 1 : numfiles
thisfile = fullfile(videoFiles(i).folder, videoFiles(i).name);
if ~exist(thisfile, 'file')
warning(sprintf('file found in directory but reportedly does not exist: "%s"', thisfile));
else
created_reader = false;
try
video = VideoReader(thisfile);
created_reader = true;
catch ME:
end
if created_reader
fprintf('reader okay for file "%s"\n', thisfile);
else
warning(sprintf('failed creating reader for file "%s"', thisfile));
end
clear video
end
end
Weitere Antworten (1)
Shandilya
am 8 Mai 2023
Verschoben: Walter Roberson
am 9 Mai 2023
1 Kommentar
Walter Roberson
am 9 Mai 2023
Spaces in folder names can indeed cause problems unless you are careful with quoting or escaping the spaces.
Siehe auch
Kategorien
Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!