% % % Split Spot Function for Imaris % % Copyright Bitplane AG 2006 % % % Installation: % % - Copy this file into the XTensions folder in the Imaris installation directory % - You will find this function in the Image Processing menu % % % % % % Matlab::BPSpotsSplit(%i) % % % % % % % Matlab::BPSpotsSplit(%i) % % % % % % % Description: % % Split one spots object in sub groups of disconnected spots. % Two spots groups are disconnected if the minimal distance between % their elements is greater than the user-defined threshold. % % function BPSpotsSplit(aImarisApplicationID, aThreshold) % connect to Imaris Com interface if ~isa(aImarisApplicationID, 'COM.Imaris_Application') vImarisServer = actxserver('ImarisServer.Server'); vImarisApplication = vImarisServer.GetObject(aImarisApplicationID); else vImarisApplication = aImarisApplicationID; end % the user has to create a scene with some spots vSurpassScene = vImarisApplication.mSurpassScene; if isequal(vSurpassScene, []) msgbox('Please create some Spots in the Surpass scene!'); return; end % get the spots vSpots = vImarisApplication.mFactory.ToSpots(vImarisApplication.mSurpassSelection); % search the spots if not previously selected if ~vImarisApplication.mFactory.IsSpots(vSpots) for vChildIndex = 1:vSurpassScene.GetNumberOfChildren vDataItem = vSurpassScene.GetChild(vChildIndex - 1); if isequal(vSpots, []) if vImarisApplication.mFactory.IsSpots(vDataItem) vSpots = vDataItem; end end end % did we find the spots? if isequal(vSpots, []) msgbox('Please create some spots!'); return; end end % get the spots coordinates [vSpotsXYZ,vSpotsTime,vSpotsRadii] = vSpots.Get; vNumberOfSpots = length(vSpotsRadii); vSpotsName = vSpots.mName; vSpots.mVisible = false; % ask for threshold if nargin<2 vQuestion = {sprintf(['Split the spots in sub groups that have distance > threshold. \n', ... 'Please enter the threshold value:']) ... }; vAnswer = inputdlg(vQuestion,'Split spots',1,{'0'}); if isempty(vAnswer), return; end vThreshold = str2double(char(vAnswer)); else vThreshold = aThreshold; end vProgressDisplay = waitbar(0,'Splitting spots'); % create new group vSpotsGroup = vImarisApplication.mFactory.CreateDataContainer; vSpotsGroup.mName = sprintf('%s elements [%.2f]', vSpotsName, vThreshold); if vThreshold == 0 for vSpot = 1:vNumberOfSpots vNewSpots = vImarisApplication.mFactory.CreateSpots; vNewSpots.Set(vSpotsXYZ(vSpot,:), vSpotsTime(vSpot), vSpotsRadii(vSpot)); vNewSpots.mName = sprintf('%s [%i]', vSpotsName, vSpot); vNewSpots.SetColor(rand,rand,rand,0); vSpotsGroup.AddChild(vNewSpots); waitbar(vSpot/vNumberOfSpots) end vSurpassScene.AddChild(vSpotsGroup); close(vProgressDisplay); return; end % build the neighbor matrix vSpotsXYZT = vSpotsXYZ'; vNeighborMatrix = false(vNumberOfSpots,vNumberOfSpots); tic for vSpotsIndex = 1:vNumberOfSpots vXYZ = diag(vSpotsXYZ(vSpotsIndex,:))*ones(3,vNumberOfSpots); vDistanceList = sqrt(sum( (vXYZ - vSpotsXYZT).^2 ))<=vThreshold; vNeighbors = vDistanceList | vNeighborMatrix(vSpotsIndex,:); vNeighborMatrix(vNeighbors,vNeighbors) = 1; waitbar(0.3*vSpotsIndex/vNumberOfSpots) end toc vNumberOfGroups = 0; vSpots = 1:vNumberOfSpots; while length(vSpots)>0 vSpotsIndex = vSpots(1); vNeighbors = vNeighborMatrix(vSpotsIndex,vSpots); vGroupIndices = vSpots(vNeighbors); vNewSpots = vImarisApplication.mFactory.CreateSpots; vNewSpots.Set(vSpotsXYZ(vGroupIndices,:), vSpotsTime(vGroupIndices), ... vSpotsRadii(vGroupIndices)); vNumberOfGroups = vNumberOfGroups + 1; vNewSpots.mName = sprintf('%s [%i]{%i}', vSpotsName, vNumberOfGroups, ... length(vGroupIndices)); vNewSpots.SetColor(rand,rand,rand,0); vSpotsGroup.AddChild(vNewSpots); vSpotsRemaining = vNeighborMatrix(vSpotsIndex,vSpots)==0; vSpots = vSpots(vSpotsRemaining); waitbar(0.3+0.7*(1-length(vSpots)/vNumberOfSpots)) end vSurpassScene.AddChild(vSpotsGroup); close(vProgressDisplay);