Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52ecadf
commit aa01b96
Showing
5 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
|
||
|
||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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,:); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |