Commit 88a13878 authored by p.jhagema's avatar p.jhagema
Browse files

useful functions for figure window

parent f2cb2134
function [ranges, cropSize, cropImg] = cropTool(region, imageOriginal)
%cropTool is an interactive and command-based function to crop an image to arbitrary sizes.
%
% Parameters
% ----------
% region : matlab imrect object or 4-tupil
% cropRegion, if given manually of format: [ymin ymax xmin xmax].
% Otherwise an imrect object.
%
% Returns
% -------
% ranges : numerical array
% cropping region
% cropImg : numerical array
% cropped image
%
%
% Example
% -------
%
% .. code-block:: matlab
%
% image = phantom(512);
% showImage(image)
% autoContrast
%
% HoloTomoToolbox
% Copyright (C) 2019 Institut fuer Roentgenphysik, Universitaet Goettingen
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
% get the current figure
currentFig = gca;
if isempty(currentFig)
error('No figure window open');
end
% define crop region
if (nargin<1)
region = imrect;
wait(region);
end
if isa(region, 'imrect') % region is a imrect
imageOriginal = getimage(currentFig);
% create mask
% matlabs position format:[xmin ymin width height]
dimMask = round(region.getPosition);
cropSize = [dimMask(4) dimMask(3)];
%range format: [ymin ymax xmin xmax]
ranges = [dimMask(2), dimMask(2)+dimMask(4)-1, dimMask(1), dimMask(1)+dimMask(3)-1];
% only consider part of the image which lies within specified region
cropImg = imageOriginal(dimMask(2):dimMask(2)+dimMask(4)-1,dimMask(1):dimMask(1)+dimMask(3)-1);
region.delete
else
% region are pixel coordinates ranges
ranges = ...
imageOriginal(region(1):region(2), region(3):region(4), :);
end
end
function addToolsToToolbar()
% ADDTOOLSTOTOOLBAR adds an button in the toolbar to easily access
% the autoContrast and cropTool function.
%
% ``addToolsToToolbar()``
%
% This function alters the behavior of all figure windows after invocation.
% It alters the default function which is called by matlab upon creation of
% a new figure. Afterwards new figures have a new tool represented by a
% light bulb to adjust the contrast of an image.
%
% Returns
% -------
% This function changes the behavior of all figure created after
% invocation.
%
% See also
% --------
% autoContrast
% cropTool
%
% Example
% -------
%
% .. code-block:: matlab
%
% addToolsToToolbar()
% figure;
% imagesc(rand(128))
% HoloTomoToolbox
% Copyright (C) 2019 Institut fuer Roentgenphysik, Universitaet Goettingen
%
% This program is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% This program is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
%
fig_cmd = ...
['addToolbarExplorationButtons(gcf);'...
'AutoContrastBtn = uipushtool(uitoolbar(gcf),''TooltipString'',''Auto Contrast Tool'', ''ClickedCallback'',''eval(''''autoContrast'''')'');'...
'[img,map] = imread(fullfile(matlabroot,''toolbox'',''matlab'',''icons'',''demoicon.gif''));'...
'AutoContrastBtn.CData = ind2rgb(img,map); '];
fig_cmd = ...
[fig_cmd ...
'CropToolBtn = uipushtool(''TooltipString'',',...
'''Cropping Tool'', ''ClickedCallback'',''eval(''''[cropRegion, cropSize, cropImg] = cropTool;'''')'');'...
'[img,map] = imread(fullfile(matlabroot,''toolbox'',''matlab'',''icons'',''tool_rectangle.gif''));'...
'CropToolBtn.CData = ind2rgb(img,map); '];
set(groot,'defaultFigureCreateFcn',fig_cmd)
end
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment