Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
%CALCULATETFORM estimate the affine transformation to compensate for a not
% well alligned camera image.
%
% TFORM = CALCULATETFORM(BIM, ARENASIZE)
%
% BIM a binary image of the arena. Assuming that the arena is white and the
% largest (white) object in the image.
% ARENASIZE the (real) size of the (square) experimental arena in mm
%
% TFORM the infered geometric transformation.
%
% Friedrich Kretschmer
% Max-Planck Institute for Brain Research
% sciclist@brain.mpg.de
function tform = calculateTForm(bim, arenaSize)
%Find connected components
CC = bwconncomp(bim);
%Deteremine the number of pixels belonging to each component
numPixels = cellfun(@numel, CC.PixelIdxList);
[~, idx] = max(numPixels);
%Determine the largest blob (should be the arena)
CC.NumObjects = 1;
CC.PixelIdxList = {CC.PixelIdxList{idx}};
%Calculate the convex hull for the arena
stats = regionprops(CC, 'ConvexHull');
%Calculate the four corners of the arena
[~, idx] = min(pdist2([0, 0], stats.ConvexHull));
topLeft = stats.ConvexHull(idx,:);
[~, idx] = min(pdist2([size(bim, 2), 0], stats.ConvexHull));
topRight = stats.ConvexHull(idx,:);
[~, idx] = min(pdist2([size(bim, 2), size(bim, 1)], stats.ConvexHull));
bottomRight = stats.ConvexHull(idx,:);
[~, idx] = min(pdist2([0, size(bim, 1)], stats.ConvexHull));
bottomLeft = stats.ConvexHull(idx,:);
arena = [0, 0;...
arenaSize, 0;...
arenaSize, arenaSize;...
0, arenaSize];
dArena = [topLeft; topRight; bottomRight; bottomLeft];
%Estimate the affine transformation
tform = fitgeotrans(dArena, arena, 'affine');
end