This repository has been archived by the owner. It is now read-only.
Permalink
Cannot retrieve contributors at this time
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?
OpenFieldAnalysis/calculateTForm.m
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
52 lines (42 sloc)
1.65 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%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 |