How to accurately process multiple frames in a video file using for and while loops
3 ビュー (過去 30 日間)
古いコメントを表示
I am interested in tracking a white horizontal line that slowly moves up a semicircle (bottom to top) over time. In my code: I import my video > read the frames > crop them and place them into a cell that contains each frame of the freezing droplet. I then select the same column in each frame and wish to analyze the movement of the freezing droplet over time (or frames).
The problem I am having is my code doesn't read all of the rows in each frame completely. It looks like it stops at the last white pixel. I know this because the cell I am building through my for loop (variable A) has different sized matrices in them. Some matrices have more amounts of rows than others. While what I want is for all matrices to have 230 rows (the max height of the droplet) so that I know the exact location of the freezing line in that frame.
I have attached the variable (Crop1) that contains all 700 of the frames that I want to be observing. The following is my code:
nstart = 260;
nend = 700;
load('Crop1.mat')
middle1 = 243;
for t = nstart:nend % nstart and nend are the first and last frames to be observed respectively
i = 1;
while i<230 % 230 is the max height of the droplet
if Crop1{t}(i,middle1) == 1 %Crop1 is a cell made of up of video frames
A{t}(i,1) = Crop1{t}(i,middle1);
%i = 230;
end
i = i + 1;
end
I{t} = [A{t},;];
end
The end goal of this is to obtain a cell that consists of 700 (which is the amount of frames) matrices. These matrices need to have only 1 column and 230 rows. Then, I will be able to pinpoint the location of the freezing line in each matrix and plot it against time.
0 件のコメント
採用された回答
Image Analyst
2018 年 2 月 18 日
7 件のコメント
Image Analyst
2018 年 2 月 19 日
It could be better and more robust, but this is what I could do in a few minutes off the top of my head:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
s = load('crop1.mat')
crop1 = s.Crop1;
startTime = tic; % Start timer.
numberOfCells = length(crop1)
freezingLine = zeros(1, numberOfCells);
for k = 1 : numberOfCells
thisCell = crop1{k};
if ~isempty(thisCell)
subplot(2, 2, 1);
imshow(thisCell);
axis on;
caption = sprintf('Frame %d of %d', k, numberOfCells);
title(caption, 'FontSize', fontSize);
% Extract columns 200-300
croppedImage = thisCell(1:200, 200:300);
subplot(2, 2, 2);
imshow(croppedImage);
axis on;
title(caption, 'FontSize', fontSize);
% Get the bottom most pixel
[rows, columns] = size(croppedImage);
bottomLine = zeros(1, columns);
for col = 1 : columns
bottomLine(col) = find(croppedImage(:, col), 1, 'last');
end
subplot(2, 2, 3);
plot(bottomLine, 'b-', 'LineWidth', 2);
ylim([1, rows]);
xlim([1, columns]);
title('Bottom Pixels', 'FontSize', fontSize);
xlabel('Column', 'FontSize', fontSize);
ylabel('Row', 'FontSize', fontSize);
grid on;
% Throw out outliers by looking at the median absolute deviation.
medianValue = median(bottomLine);
mad = bottomLine - medianValue;
goodIndexes = mad <= 3; % Good locations have a mad of less than or equal to 3.
% Find the mean of the good indexes.
freezingLine(k) = mean(bottomLine(goodIndexes));
subplot(2, 2, 4);
plot(freezingLine, 'b-', 'LineWidth', 2);
grid on;
title('Freezing Line Row', 'FontSize', fontSize);
xlabel('Frame', 'FontSize', fontSize);
ylabel('Row', 'FontSize', fontSize);
drawnow;
end
end
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0, 0.04, 1, 0.96]);
% Stop timer
elapsedTime = toc(startTime);
fprintf('Elapsed time = %.1f seconds\n', elapsedTime);
You can see the "good" part, where the slanted line in the lower right plot it. You could find that somehow and extract it out from the rest of the plot. I didn't have time for that and will leave it to you. Let me know what you think.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Image Processing and Computer Vision についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!