Commit c518cbd5 authored by Turnhout, M.C. van's avatar Turnhout, M.C. van
Browse files

add option for normalised third dye (vis ruifrok)

parent 6aebd33e
function [amounts, P, Q, R, RGB, A, K, iOD] = cld_decon(dyes, im, varargin)
% [amounts, P, Q, R, RGB, A, K, iOD] = cld_decon(dyes, im, 'rod', Imax)
% [amounts, P, Q, R, RGB, A, K, iOD] = cld_decon(dyes, im, 'rgb', Imax, ...
% 'cmp')
%
% Input:
% dyes matrix with dye RGB or OD values, a column [R G B]^T for each
% of either two or three dyes (3x2 or 3x3 matrix)
% im RGB image to deconvolve with dyes
% rod (optional) a string to identify whether dyes is in RGB or OD
% rod = 'RGB' dyes contains dye RGB values (default)
% rod = 'OD' dyes contains dye OD (optical density) values
% rgb (optional) a string to identify whether dyes is in RGB or OD
% rgb = 'RGB' dyes contains dye RGB values (default)
% rgb = 'OD' dyes contains dye OD (optical density) values
% Imax (optional) maximum intensity for the RGB values
% (default is 8-bits RGB: Imax = 2^8-1 = 255 for a missing
% argument).
% cmp (optional) a string to identify whether a cpmplementary third
% dye should be calculated with the cross product or by
% normalising absorption across channels.
% cmp = 'cross' add third dye with cross product (default)
% cpm = 'norm' add third dye by normalisation across channels
% (orginal Ruifrok method)
%
% Output:
% amounts 3D matrix of size(im) with dye amounts for dye n in
......@@ -38,38 +45,67 @@ function [amounts, P, Q, R, RGB, A, K, iOD] = cld_decon(dyes, im, varargin)
% set defaults
Imax = 255; rod = 'RGB';
Imax = 255; fromrgb = 1; cmp = 1;
% initialise
amounts = zeros(size(im));
P = amounts; Q = P; R = P; RGB = P;
% parse input
for n = 1:numel(varargin)
if ischar(varargin{n}) % RGB/OD switch
rod = varargin{n};
else % Imax
if isnumeric(varargin{n}) % Imax
Imax = varargin{n};
else % RGB/OD or cross / Ruifrok switch
varg = varargin{n};
if strcmpi(varg(1), 'r') % RGB switch
fromrgb = 1;
elseif strcmpi(varg(1), 'o') % OD switch
fromrgb = 0;
elseif strcmpi(varg(1), 'c') % cross product switch
cmp = 1;
elseif strcmpi(varg(1), 'n') % normalisation switch
cmp = 0;
else
warning('Input argument ''%s'' not recognised, skiping.\n', ...
varargin{n})
end
end
end
% RGB or OD?
if strcmpi(rod(1), 'r')
if fromrgb > 0
% dyes contains RGB values, convert to OD
A = cld_rgb2od(dyes, Imax);
else
A = dyes;
end
% (only) two colours?
if size(A, 2) < 3
% add third `perpendicular' colour
A(:, 3) = cross(A(:, 1), A(:, 2));
end
% normalise: matrix K contains `k-hat'
K = A;
for c = 1:3
for c = 1:size(K, 2)
K(:, c) = K(:, c)/norm(K(:, c));
end
% (only) two colours?
if size(A, 2) < 3
if cmp > 0
% perpendicular absorption with cross product
A(:, 3) = cross(K(:, 1), K(:, 2));
else
% normalise across channels according to Ruifrok
Ks = sum(K(:, 1:2).^2, 2);
Ks(Ks > 1) = 1;
A(:, 3) = sqrt(1 - Ks);
end
% normalise into K
K(:, 3) = A(:, 3)/norm(A(:, 3));
end
% convert image RGB to image OD
iOD = cld_rgb2od(double(im), Imax);
......@@ -79,7 +115,7 @@ for r = 1:size(im, 1)
for c = 1:size(im, 2)
p = p + 1; % update pixel count
if round(100*p/(numel(im)/3)) > done
if round(100*p/(numel(im)/3)) > done
% update feedback
done = round(100*p/(numel(im)/3));
fprintf('\b\b\b\b\b\b\b\b\b%3i%% done', done)
......
......@@ -7,7 +7,7 @@ dyes = [254 209 0; % yellow
0 155 58]'; % green
% do the deconvolve
[amounts, P, Q, R, RGB, A, K] = cld_decon(dyes, im, 'rgb');
[amounts, P, Q, R, RGB, A, K] = cld_decon(dyes, im, 'rgb', 'cross');
dyes(:, 3) = 256*exp(-K(:, 3))-1
K = K
iK = inv(K)
......
......@@ -7,22 +7,7 @@ dyes = [254 209 0; % yellow
0 155 58]'; % green
% do the deconvolve
[amounts, P, Q, R, RGB, A, K] = cld_decon(dyes, im(1, 1, :), 'rgb');
%%%% Ruifrok
As = K.^2;
for c = 1:3
if sum(As(c, 1:2)) < 1
A(c, 3) = sqrt(1 - sum(As(c, 1:2)));
else
A(c, 3) = 0;
end
end
% do the deconvolve
[amounts, P, Q, R, RGB, A, K] = cld_decon(A, im, 'od');
[amounts, P, Q, R, RGB, A, K] = cld_decon(dyes, im, 'rgb', 'norm');
dyes(:, 3) = 256*exp(-K(:, 3))-1
K = K
iK = inv(K)
......
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