-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColorMechanism.m
More file actions
executable file
·281 lines (231 loc) · 10 KB
/
ColorMechanism.m
File metadata and controls
executable file
·281 lines (231 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
% for details on the terminology see the following publications:
% "An object-color space", A.D. Logvinenko, Journal of Vision
% "Metamer Mismatch Volumes", Logvinenko, Godau and Funt, Proc. CGIV 2012
classdef ColorMechanism < handle
properties
illuminant
cmf
wavelengths
cum_cmf_pp
white
n_sensors
invomegaspline
omegaspline
end
methods
% constructor
% each color mechanism consists of illuminant and cmf, which need
% to be provided on creation, along with the corresponding
% wavelengths
% illuminant normalized to make Y (second column of cmf) = 100
function obj = ColorMechanism(illuminant, cmf, wavelengths,no_norm_ill)
obj.illuminant=illuminant;
obj.cmf=cmf;
obj.wavelengths=wavelengths;
if nargin<4
obj.normalize_illuminant();
end
obj.n_sensors=size(cmf,2);
obj.generate_interpolator();
end
function [ ill_norm ] = normalize_illuminant(obj, normvalue, normchannel)
% normalizes the cmf to that maximum of each channel is 100
n_channels=size(obj.cmf,2);
if nargin<2
normvalue=100;
end
if nargin<3
normchannel=ceil(n_channels/2);
end
max_rgb=obj.illuminant(:)'*repmat(obj.cmf(:,normchannel),[1 n_channels]);
obj.cmf=obj.cmf./repmat(max_rgb,size(obj.cmf,1),1)*normvalue;
obj.white=obj.illuminant'*obj.cmf;
end
% generates the inperpolation functions needed to quickly calculate
% responses
function generate_interpolator(obj)
% create interpolation functions for sensor responses
illcones=obj.cmf.*repmat(obj.illuminant,1,obj.n_sensors);
cumcones=cumtrapz(illcones); % cumulative cones
obj.white=cumcones(end,:); % maximal response (white)
obj.white=obj.white(:)';
cumcones=repmat(cumcones(end,:),size(cumcones,1),1)-cumcones; % invert the response (integrate from wl to end instead of integrating from start to wl)
obj.cum_cmf_pp=spline(obj.wavelengths',cumcones'); % interpolation polygon
% create the de/reeparametrization function interpolators
sigma=cumtrapz(sqrt(sum(obj.cmf.^2,2)));
sigma=sigma/max(sigma);
[~,idx]=unique(sigma(end:-1:1,:));
idx=length(sigma)-idx+1;
obj.invomegaspline=spline(sigma(idx), obj.wavelengths(idx));
obj.omegaspline=spline(obj.wavelengths(idx), sigma(idx));
end
% deparamatrization: from [0,1] to wavelengths
function out=deparametrize(obj, wl)
out=ppval(obj.invomegaspline,wl);
end
% reparametrization: from wavelengths to [0,1]
function out=reparametrize(obj, wl)
out=ppval(obj.omegaspline,wl);
end
% this will evaluate the interpolation polygon at the given
% wavelengths
% corresponds to integration from lambda to lambda_max
function out=getcum(obj, wavelengths)
out=ppval(obj.cum_cmf_pp, wavelengths);
end
% convert an n-transition optimal spectrum to sensor responses
% each line in ntrs is a spectrum with l1<l2<l3... for type 1 or
% l1>l2>l3... for type 2
function out=ntr2lms(obj,ntrs, types)
% for more then one transition wavelength, the order determines
% the type, otherwise the types argument needs to be given
% set to 1 for type 2 reflectances
if ~exist('types','var')
types=(ntrs(:,1)>ntrs(:,end))+1;
end
out=zeros(size(ntrs,1),obj.n_sensors);
for i=1:size(ntrs,1)
current_ntr=ntrs(i,:);
v=obj.getcum(sort(current_ntr));
lms=sum([v(:,1:2:end) -v(:,2:2:end)],2)';
if types(i)==2
% type2 response is white-type1
lms=obj.white-lms;
end
out(i,:)=lms;
end
end
% wrapper function for reparametrized optimal wl to lms
% conversion
% a _r suffic indicates reparametrization
function out=ntr2lms_r(obj,ntrs_r, types)
% if nargin < 2
% % set to 1 for type 2 reflectances
% types=ntrs_r(:,1)>ntrs_r(:,end);
% end
if ~exist('types')
types=ntrs(:,1)>ntrs(:,end)+1;
end
ntrs=obj.deparametrize(ntrs_r);
out=obj.ntr2lms(ntrs, types);
end
function [ LMS ] = rect2lms( obj, rect_wl )
% convert a rectangular metamer given by transition wavelengths
% and purity to lms
% rect_wl contains columns for purity, l_1, l_2
% calculate LMS for optimal reflectance function given by
% transition wavelengths
rect_lms=obj.ntr2lms(rect_wl(:,2:3));
% calculate final LMS using given alpha as a convex combination
% of oLMS and grey
nSamples=size(rect_wl,1);
grey=repmat(obj.white/2,nSamples,1);
opt_lms_nogrey=rect_lms-grey; % subtract grey from optimal lms
LMS=grey+repmat(rect_wl(:,1),1,3).*opt_lms_nogrey; % add optimal lms weighted by purity
end
function [ rect ] = adl2opt( obj, adl )
% converts the provided adl to a rectangular metamer
% rect has 3 columns: purity + 2 transition wavelengths
% purity is not changed by this function
% type is determined by the order of the transition wavelengths
rect=adl;
rect(:,2:3)=0;
wlrange=obj.wavelengths(end)-obj.wavelengths(1);
l1=adl(:,3)-adl(:,2)/2;
l2=adl(:,3)+adl(:,2)/2;
% correct for type 2
l1(l1<obj.wavelengths(1))=l1(l1<obj.wavelengths(1))+wlrange;
l2(obj.wavelengths(end)<l2)=l2(obj.wavelengths(end)<l2)-wlrange;
rect(:,2:3)=[l1 l2];
end
function out=adl2lms(obj, adl)
rect=obj.adl2opt(adl);
out=obj.rect2lms(rect);
end
function [vol XYZ]=draw_objcolorsolid(obj, alpha)
% draws a colored 3d model of the object color solid in XYZ
% space into the current figure
% this obviously only works for 3 dimensions, and assumes the
% values are something like XYZ to draw colors
if nargin<2
alpha=0.5;
end
% generate samples on ocs surface using reparametrized values for a more even
% distribution of points
range=linspace(0,1,100);
N=numel(range)^2;
rOPT=zeros(N,2);
i=0;
for L1=range
for L2=range
i=i+1;
rOPT(i,:)=[L1,L2];
end
end
% convert to normal wl in nm
OPT=obj.deparametrize(rOPT);
% convert to XYZ
XYZ=obj.ntr2lms(OPT);
% draw the convex hull
vol=draw_conv_hull_color(XYZ,alpha)
end
function [ adl ] = opt2adl( obj, rect, wl )
% converts the provided rectangular metamers to adl
% rect has 3 columns: purity + 2 transition wavelengths
% purity is not changed by this function
% type is determined by the order of the transition wavelengths
adl=rect;
adl(:,2:3)=0;
%calculate lambda central
lc=mean(rect(:,2:3),2);
% correction for type 2
t2=rect(:,3)<rect(:,2);
wlrange=obj.wavelengths(end)-obj.wavelengths(1);
lc(t2)=lc(t2)-wlrange/2;
lc(lc<wl(1))=lc(lc<wl(1))+wlrange;
adl(:,3)=lc;
%calculate delta
d=abs(rect(:,2)-rect(:,3));
d(t2)=wlrange-d(t2);
adl(:,2)=d;
end
function [hull_x, hull_y, hull_l]=draw_ocs_2d(obj)
% draw the objects color solid in 2d xy chromaticity space
n_points=2000; % how many points on the boundary?
wls=linspace(obj.wavelengths(1)+0.5, obj.wavelengths(end)-0.5, n_points);
step_size=obj.wavelengths(2)-obj.wavelengths(1);
% make a list of "pure" colors
ntrs=[wls(:)-step_size/2 wls(:)+step_size/2];
lms=obj.ntr2lms(ntrs);
x= lms(:,1)./sum(lms,2);
y= lms(:,2)./sum(lms,2);
l= lms(:,2);
k = convhull(x,y);
plot(x(k),y(k),'-');
hull_x=x(k);
hull_y=y(k);
hull_l=l(k);
end
function [vol]=get_ocs_vol(obj)
% get the approximate volume of the object color solid
% generate samples on ocs surface using reparametrized values for a more even
% distribution of points
range=linspace(0,1,100);
N=numel(range)^2;
rOPT=zeros(N,2);
i=0;
for L1=range
for L2=range
i=i+1;
rOPT(i,:)=[L1,L2];
end
end
% convert to normal wl in nm
OPT=obj.deparametrize(rOPT);
% convert to XYZ
XYZ=obj.ntr2lms(OPT);
% draw the convex hull
[~,vol]=convhull(XYZ);
end
end
end