From 345e830eaca239ebcc0baa5bc8448edcb0235750 Mon Sep 17 00:00:00 2001 From: MPIBR-kretschmerf Date: Mon, 26 Feb 2018 13:47:15 +0100 Subject: [PATCH] initial commit --- CellCounter.m | 124 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 CellCounter.m diff --git a/CellCounter.m b/CellCounter.m new file mode 100644 index 0000000..a27b220 --- /dev/null +++ b/CellCounter.m @@ -0,0 +1,124 @@ +classdef CellCounter < handle + + properties + widget_MouseFigure + widget_ManualCellCounter + widget_PiaSelector + widget_ImageBrowser + widget_FolderBrowser + + fig + currentFileName + distances + + end + + methods + + function obj = CellCounter() + addpath(genpath('./submodules/')); + + obj.fig = figure('Position', [0,0, 400, 800]); + + cellCounterPanel = uipanel('Parent', obj.fig,... + 'Position',[0 .1 1 .1]); + + piaPanel = uipanel('Parent', obj.fig,... + 'Position',[0 .2 1 .1]); + + imagePanel = uipanel('Parent', obj.fig,... + 'Position',[0 .3 1 .45]); + + folderPanel = uipanel('Parent', obj.fig,... + 'Position',[0 .75 1 .25]); + + obj.widget_MouseFigure = MouseFigure(); + + imageAxes = obj.widget_MouseFigure.handle_axes; + + set(imageAxes, 'nextPlot', 'add'); + + obj.widget_ImageBrowser = WidgetImageBrowser('Parent', imagePanel, 'Axes', imageAxes); + obj.widget_PiaSelector = WidgetPiaSelector('Parent', piaPanel, 'Axes', imageAxes); + + obj.widget_ManualCellCounter = WidgetManualCellCounter('Parent', cellCounterPanel, 'Axes', imageAxes); + obj.widget_FolderBrowser = WidgetFolderBrowser('parent', folderPanel); + + addlistener(obj.widget_FolderBrowser, 'event_newFile', @obj.on_newFile); + addlistener(obj.widget_MouseFigure, 'event_clickUp', @obj.on_event_clickUp); + addlistener(obj.widget_MouseFigure, 'event_pressDel', @obj.on_pressDel); + + uicontrol('Style', 'pushbutton',... + 'Parent', obj.fig,... + 'String', 'Export',... + 'Units', 'normalized',... + 'Position', [0,0,1,.1],... + 'Callback', @obj.on_export); + + end + + function obj = on_newFile(obj, ~, event) + obj.currentFileName = event.userdata; + obj.widget_ImageBrowser.read(event.userdata); + obj.widget_ManualCellCounter.reset(); + end + + function obj = on_event_clickUp(obj, ~, event) %Event forwarding + if ~obj.widget_PiaSelector.isCurrentlySelecting + obj.widget_ManualCellCounter.on_event_clickUp(obj, event); + end + end + + function obj = on_pressDel(obj, ~, event) %Event forwarding + obj.widget_ManualCellCounter.on_pressDel(obj, event); + end + + function obj = on_export(obj, ~, ~) + obj.calculateDistances(); + exportMat = [obj.widget_ManualCellCounter.countIndices, obj.widget_ManualCellCounter.locations, obj.distances]; + + [pathstr, name, ~] = fileparts(obj.currentFileName); + exportFileName = [pathstr, filesep, name, '_counts.csv']; + fid = fopen(exportFileName,'w'); + fprintf(fid, 'counter, x, y, distance from pia [px]\n'); + fclose(fid); + + dlmwrite (exportFileName, exportMat, '-append'); + msgbox(['Wrote: ', exportFileName]); + end + + function obj = calculateDistances(obj) + + locations = obj.widget_ManualCellCounter.locations; + piaPos = obj.widget_PiaSelector.piaPos'; + + obj.distances = nan(size(locations,1),1); + for i = 1:size(locations,1) + minDist = Inf; + for j = 1:size(piaPos,1) + dist = pdist([locations(i,:) ; piaPos(j,:)]); + if dist < minDist + minDist = dist; + end + end + obj.distances(i) = minDist; + end + end + + function delete(~) + + if exist('obj', 'var') + if exist(obj.fig, 'figure') + close(obj.fig); + end + delete(obj.widget_MouseFigure); + delete(obj.widget_ManualCellCounter); + delete(obj.widget_PiaSelector); + delete(obj.widget_ImageBrowser); + delete(obj.widget_FolderBrowser); + end + end + + end + +end \ No newline at end of file