Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jlian committed May 24, 2015
0 parents commit 5d7d8f1
Show file tree
Hide file tree
Showing 36 changed files with 4,725 additions and 0 deletions.
1 change: 1 addition & 0 deletions .dropbox
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"ns": 677615433, "tag": "shared"}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Data
Icon
.DS_Store
*.sublime-project
*.sublime-workspace
9 changes: 9 additions & 0 deletions .ipynb_checkpoints/pytrack_test-checkpoint.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"metadata": {
"name": "",
"signature": "sha256:5eea78c4a82cf7843b080601e5d46c05d35982c43bb39098da90a82d13f5bd49"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": []
}
Empty file added Icon
Empty file.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 John Lian

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# MATLAB Particle Tracker
This is an application that can be used to track particles with a series of images. It uses algorithms adopted from the [The Matlab Particle Tracking Code Repository](http://site.physics.georgetown.edu/matlab/).

Detailed manual coming soon!
26 changes: 26 additions & 0 deletions default_config.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
% parameter structure for particle tracking
% color = imread(fullfile(img.path, img.name));
% a = rgb2gray(color);
% b = bpass(a, param.ln, param.d);
% pk = pkfnd(b, param.th, param.sz);
% cnt = cntrd(b, pk, param.szz);

% param = struct;

param.fps = 600; % frames per second with which the footage was captured

% image analysis parameters
param.ln = 1; % characteristic lengthscale of noise in pixels.
param.d = 10; % diameter of blobs
param.th = 40; % threshhold
param.sz = 11; % set to be slighly larger than diameter of particle
param.szz = param.sz + 2; % for cnt

% particle tracking parameters
param.mem = 10; % number of times steps that a particle can be lost for
param.dim = 2; % default is 2
param.good = 10; % eliminates trajectories with less than this many positions
param.quiet = 1; % default is 1
param.maxdisp = 20; % max number of pixels a particle would move in one time interval

param.pxperm = 0.8636/432;
43 changes: 43 additions & 0 deletions lib/analyseImgs.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
function [ positionList ] = analyseImgs( imgs, param )
% analyse the input image and outputs the positions of the centroids
% of each particle in the image
% input: imgs - full file path + file name of the image
% param: the parameters needed i.e. sz, d, etc.
% output: an array listing the scrambled coordinates and data
% of the different particles at different times, such that:
% positionlist(0:d-1,*): contains the d coordinates and
% data for all the particles, at the different times. must be positve
% positionlist(d,*): contains the time t that the position
% was determined, must be integers (e.g. frame number. These values must
% be monotonically increasing and uniformly gridded in time.

i = 1;
positionList = [];

for img = imgs'

color = imread(fullfile(img.path, img.name));

% make sure the code can handle already gray images
[~, ~, numberOfColorChannels] = size(color);
if numberOfColorChannels > 1
a = rgb2gray(color);
else
a = color; % It's already gray.
end

% % for some reason matlab cannot read images properly!!!!
% a = 255-a;

b = bpass(a, param.ln, param.d);
pk = pkfnd(b, param.th, param.sz);
cnt = cntrd(b, pk, param.szz);
positionList = [positionList; create_positionList(cnt,i)];
i = i + 1;

end

return;

end

122 changes: 122 additions & 0 deletions lib/animate.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
function animate(y, T, param)
%% math
% makes animation of all the particles
L = 0.8636;

%% graphics

% set up first frame
figure('Color', 'white');
set(gcf,'Position',[100 678 640 360]);

%% animation sub plot
subplot(1,5,1);

% vertical line
plot([0;0],[0 L],'k');

hold on
par = plot(0,y(1,:),'sr','MarkerSize',3,'MarkerFaceColor','r');
hold off

ht = title(sprintf('Time: %0.2f sec', T(1)));
ylim([0 L]); axis 'auto x'; ylabel('Position (m)');

%% the position sub plot
subplot(1,5,[2 3 4 5]);

% plot only the displacements i.e. odd columns
pFig = plot(T, y, 'LineWidth', 1);


% n moving markers
mar = line(T(1), y(1,:), 'Marker', '.', 'MarkerSize', 10);
axis([0 T(length(T)) 0 L]);
xlabel('Time (s)');
ah = gca;

% get figure size
ahh = gcf;
pos = get(gcf, 'Position');
width = pos(3); height = pos(4);

% preallocate data (for storing frame data)
mov = zeros(height, width, 1, length(T), 'uint8');

%% animation loop

display('Animating...');

% loop through by changing XData and YData
for id = 1:length(T)
% update graphics data. This is more efficient than recreating plots.
% convert to cell otherwise the vector-set function doesn't work
set(mar, 'XData', T(id), {'YData'}, num2cell(y(id, :))');
set(par,{'YData'}, num2cell(y(id, :))');

set(ht, 'String', sprintf('%0.2f s, frame(%d)', T(id),id));

% get frame as an image
f = getframe(ahh);

% create a colormap for the first frame. For the rest of the frames,
% use the same colormap
if id == 1
[mov(:,:,1,id), map] = rgb2ind(f.cdata, 256, 'nodither');
else
mov(:,:,1,id) = rgb2ind(f.cdata, map, 'nodither');
end
end

%% gif

gif_button = uicontrol('Style', 'pushbutton', 'String', 'GIF',...
'Position', [20 20 50 20],...
'Callback', {@GIF,param,mov, map});
% the pushbutton string callback
% calls the gif function to make gif

%% export

export_button = uicontrol('Style', 'pushbutton', 'String', 'Save',...
'Position', [20 50 50 20],...
'Callback', {@newPlot,param,ah});
% the pushbutton string callback
% calls the ploter and export

if ~exist(fullfile(param.path, 'Analysis'), 'dir')
mkdir(param.path, 'Analysis')
end


end

function GIF(~,~,param, mov, map)

gif_fps = param.fps/10;

outGIF = fullfile(param.path, 'Analysis','Animation.gif');

display('Saving as GIF...');

imwrite(mov, map, outGIF, 'LoopCount', inf, 'DelayTime', 1/gif_fps);

fprintf('GIF saved as:\n "%s"\n', outGIF);

end

function newPlot(~,~,param, gca)

display('Saving...');

outPlot = fullfile(param.path, 'Analysis','TrajectoriesPlot');
set(get(gca,'YLabel'),'String','Position (m)')
set(get(gca,'XLabel'),'String','Time (s)')

export_fig(gca, outPlot, '-pdf','-png','-m2');

fprintf('Plot saved as:\n "%s"\n', outPlot);


end

15 changes: 15 additions & 0 deletions lib/chooseDiag.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function reply = chooseDiag( prompt )
% Construct a questdlg with two options
choice = questdlg( prompt, ...
'Choices', ...
'Yes','No','Yes');
% Handle response
switch choice
case 'Yes'
reply = 1;
case 'No'
reply = 2;
end

end

66 changes: 66 additions & 0 deletions lib/convert.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
classdef convert

properties (Constant)
% 1 pixel = 0.0001461870504 meters
a = 0.8636/923;

% 1 frame = 1/600 seconds
b = 1/600;

% set the equillibrium position in meters
eq = 0;

% set mass of one magnet in kg
m = 0.009601;

% set the offset of the bottom fixed magent in number of pixels
o = 20.5;

% set the pull force in lbs
pf = 11.33;
end

methods (Static)

function output = pixelsToMeters(X, pxperm)
for i=1:size(X,2)
for j = 1:size(X,1)
output(j,i) = X(j,i)*pxperm;
end
end
end

function output = framesToSeconds(Y)
for i=1:size(Y,2)
output(i) = Y(i)*convert.b;
end
end

function output = velToMetric(vel)
for i=1:size(vel,2)
output(i) = vel(i)*convert.a/convert.b;
end
end

function output = accToMetric(acc)
for i=1:size(acc,2)
output(i) = acc(i)*convert.a/convert.b/convert.b;
end
end

function output = NTolb(force)
for i=1:size(force,2)
output(i) = force(i)*0.22481;
end
end

function output = metersToInches(disp)
for i=1:size(disp, 2)
output(i) = disp(i)*39.3701;
end
end

end

end

12 changes: 12 additions & 0 deletions lib/create_positionList.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
function [ positionList ] = create_positionList( cnt, index )
% create positionlist vector by appending data to the end
% for track.m

positionList(:,1) = cnt(:,2); % x-coordinate
positionList(:,2) = cnt(:,1); % y-coordinate
positionList(1:size(cnt,1),3) = index; % frame number

return;

end

5 changes: 5 additions & 0 deletions lib/export_fig/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/.ignore
*.txt
*.asv
*~
*.mex*
27 changes: 27 additions & 0 deletions lib/export_fig/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
Copyright (c) 2014, Oliver J. Woodford
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

* Neither the name of the {organization} nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Loading

0 comments on commit 5d7d8f1

Please sign in to comment.