code won't run, but gives no error
古いコメントを表示
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
採用された回答
その他の回答 (1 件)
Shandilya
2023 年 5 月 8 日
移動済み: Walter Roberson
2023 年 5 月 9 日
0 投票
1 件のコメント
Walter Roberson
2023 年 5 月 9 日
Spaces in folder names can indeed cause problems unless you are careful with quoting or escaping the spaces.
カテゴリ
ヘルプ センター および File Exchange で Startup and Shutdown についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!