From aa01b9609df71a2d434db4480023858c850f9ee6 Mon Sep 17 00:00:00 2001 From: 291ce4321ac <42886494+291ce4321ac@users.noreply.github.com> Date: Fri, 10 Mar 2023 20:08:48 -0600 Subject: [PATCH] update to 2.4.2 --- conversiontools/chancount.m | 31 ++++++++++++++++++++ conversiontools/flipd.m | 56 ++++++++++++++++++++++++++++++++++++ conversiontools/imclamp.m | 29 +++++++++++++++++++ conversiontools/splitalpha.m | 21 ++++++++++++++ conversiontools/splitchans.m | 26 +++++++++++++++++ 5 files changed, 163 insertions(+) create mode 100644 conversiontools/chancount.m create mode 100644 conversiontools/flipd.m create mode 100644 conversiontools/imclamp.m create mode 100644 conversiontools/splitalpha.m create mode 100644 conversiontools/splitchans.m diff --git a/conversiontools/chancount.m b/conversiontools/chancount.m new file mode 100644 index 0000000..fedc74c --- /dev/null +++ b/conversiontools/chancount.m @@ -0,0 +1,31 @@ +function [ncchans nachans] = chancount(inpict) +% COUNTVECTOR = CHANCOUNT(INPICT) +% [NUM_COLOR_CHANS NUM_ALPHA_CHANS] = CHANCOUNT(INPICT) +% simple convenience tool for fetching info about an image +% assumes I/IA/RGB/RGBA/RGBAAA channel arrangements +% assumes image dimensioning is [height width channels frames] +% +% INPICT an image array of any class +% +% if only one output argument is specified, the output will be a 2-element vector +% +% See also: framecount, imsize + +numchans = size(inpict,3); +if ~ismember(numchans,[1 2 3 4 6]) + error('CHANCOUNT: expected image to be I/IA/RGB/RGBA/RGBAAA. What is this %d-channel image supposed to be?',numchans) +end +hasalpha = 1-mod(numchans,2); +ncchans = min(numchans-hasalpha,3); +nachans = numchans-ncchans; + +if nargout < 2 + ncchans = [ncchans nachans]; +end + +end + + + + + diff --git a/conversiontools/flipd.m b/conversiontools/flipd.m new file mode 100644 index 0000000..2ca004f --- /dev/null +++ b/conversiontools/flipd.m @@ -0,0 +1,56 @@ +function out = flipd(varargin) +% OUT=FLIPD(A,{DIM}) +% Flip an array. This is a wrapper for flip() and flipdim() to provide a consistent +% syntax across versions with the speed benefit of flip() where it's available. +% +% flip() is ~2-3x as fast as flipdim() for large arrays, but it's only available +% post-R2013b. Furthermore, flip() supports implicit dimension selection, but +% flipdim() does not. flipd() allows the use of a single function without concern +% for which features will be available. +% +% A is an array +% DIM specifies the dimension to flip along. If unspecified, DIM is selected +% as follows. If A is a vector, it is flipped along its length. If A is a 2D +% array, it is flipped columnwise. If A is ND, it is flipped along its first +% non-singleton dimension. +% +% Output class is inherited from A +% +% See also: flip, flipdim + +% flip is 3x faster than flipdim for large 4D stacks, but it's new (R2013b) +% 10-15ms for version checking with verLessThan() makes this insignificant for small arrays +% but ifversion() is much faster + +if ifversion('<','R2013b') + if numel(varargin) == 1 + A = varargin{1}; + if isrow(A) + dim = 2; + elseif ndims == 2 + % if column vector or 2D array + dim = 1; + else + % if ND array + dim = find(size(A) > 1,1); + end + out = flipdim(A,dim); %#ok<*DFLIPDIM> + else + out = flipdim(varargin{:}); + end +else + out = flip(varargin{:}); +end + +% ifversion() is fast enough to be beneficial even with smaller arrays +% syntax varies slightly between flip and flipdim +% flipdim requires dim; flip has defaults + + + + + + + + + diff --git a/conversiontools/imclamp.m b/conversiontools/imclamp.m new file mode 100644 index 0000000..b58158c --- /dev/null +++ b/conversiontools/imclamp.m @@ -0,0 +1,29 @@ +function outpict = imclamp(inpict,varargin) +% OUTPICT = IMCLAMP(INPICT,{LIMIT}) +% Clamp data values to stay within a closed interval. This is +% more readable than min(max(myvariable,lim1),lim2), especially +% in cases where the limits are already in vector form. +% +% INPICT is any numeric array +% LIMITS optionally specifies the interval extent (default [0 1]) +% This parameter is a 2-element vector with ascending values. +% +% Output class is inherited from input. +% +% Webdocs: http://mimtdocs.rf.gd/manual/html/imclamp.html +% See also: simnorm, max, min, imrange + +limits = [0 1]; + +if numel(varargin)>0 + limits = varargin{1}; + if numel(limits) ~= 2 + error('IMCLAMP: limit vector must have 2 elements') + end + if diff(limits)<=0 + error('IMCLAMP: lower limit must be less than upper limit') + end +end + +outpict = min(max(inpict,limits(1)),limits(2)); + diff --git a/conversiontools/splitalpha.m b/conversiontools/splitalpha.m new file mode 100644 index 0000000..b8ea863 --- /dev/null +++ b/conversiontools/splitalpha.m @@ -0,0 +1,21 @@ +function [colorchans alphachans] = splitalpha(inpict) +% [COLOR ALPHA] = SPLITALPHA(INPICT) +% Split an image into its color and alpha channels. +% +% INPICT is an I/IA/RGB/RGBA/RGBAAA image of any standard image class. +% Multiframe images are supported. +% +% Output class is inherited from the input. +% +% Example of using splitalpha()/joinalpha() to handle IA/RGBA images +% [inpict alpha] = splitalpha(inpict); +% outpict = blockify(inpict,[10 10]); +% outpict = joinalpha(outpict,alpha); +% +% Webdocs: http://mimtdocs.rf.gd/manual/html/splitalpha.html +% See also: joinalpha, splitchans, chancount + +[nc,~] = chancount(inpict); + +alphachans = inpict(:,:,nc+1:end,:); +colorchans = inpict(:,:,1:nc,:); diff --git a/conversiontools/splitchans.m b/conversiontools/splitchans.m new file mode 100644 index 0000000..acb9881 --- /dev/null +++ b/conversiontools/splitchans.m @@ -0,0 +1,26 @@ +function varargout = splitchans(inpict) +% [CH1 CH2 CH3 ...] = SPLITCHANS(INPICT) +% Split a multichannel image into its color channels. +% This tool is effectively a direct replacement for IPT imsplit(), +% though splitchans() also allows underspecified and multiframe inputs. +% +% INPICT is an image of any standard image class. +% Multiframe (4D) images are supported. +% CH1, CH2, etc are the dim3 pages of INPICT +% +% If the number of output arguments exceeds the number of image channels +% the excess outputs will be returned as empty without error. +% +% Output class is inherited from INPICT +% +% Webdocs: http://mimtdocs.rf.gd/manual/html/splitchans.html +% See also: splitalpha, imsplit, chancount + +nchans = size(inpict,3); +for c = 1:nargout + if c <= nchans + varargout{c} = inpict(:,:,c,:); %#ok<*AGROW> + else + varargout{c} = []; + end +end