-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscielab.m
204 lines (179 loc) · 6.65 KB
/
scielab.m
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
function result = scielab(sampPerDeg, image1, image2, whitepoint, imageformat,k)
% result = scielab(sampPerDeg, image1, image2, whitepoint, imageformat,k)
%
% Computes the S-CIELAB difference between two images.
%
% sampPerDeg -- input image resolution in samples per degree of visual angle.
% (use the function visualAngle to determine SAMPPERDEG: pass numpixels=-1
% and visualAngle=1);
% image1, image2 -- input images in XYZ or cone coordinate. These should
% be in the format [x-plane y-plane z-plane] if they are
% in XYZ coordinate, or [L-plane M-plane S-plane] if they
% are in cone coordinate. They can also be in a
% three-dimensional matrix of size [m n 3] if
% you are using Matlab version 5 and up.
% whitepoint -- white point for computing CIELAB values. If the images are
% in XYZ format, whitepoint should be in the format
% [x y z]. If the images are in LMS format, whitepoint should
% also be in LMS format [l m s].
% imageformat -- specifies the format of the input images. Options are
% 'xyz': CIE1931 (2 degree) XYZ values (DEFAULT)
% 'xyz10': CIE1964 (10 degree) XYZ values
% 'lms': LMS cone coordinates, use XYZ2 to compute CIELAB.
% 'lms10': LMS cone coordinates, use XYZ10 to compute CIELAB.
% k -- If this value is provided, use the CIE94 color difference
% formula to compute delta E. If k is given as a 3
% vector, use this as the k94 vector in the CIE94
% calculation. Otherwise, use the default.
%
% If all arguements are given, the s-cielab difference image is returned
% with entries representing deltaE values for each pixel location.
% If only sampPerDeg, image1 (and possibly imageformat) are specified,
% then return image1 after the spatial filtering operation, in the
% opponent representation [BW-plane RG-plane BY-plane]. For example:
% result = scielab(sampPerDeg, image1, 'xyz2') will return the
% spatially filtered opponent representation of image1 as result,
% assuming "image1" is provided in xyz1931 format.
%
% Functions called: changeColorSpace, cmatrix,
% getPlanes, pad4conv, resize, deltaLab,
% separableFilters, separableConv.
% (implicit: gauss, gauss2, sumGauss, xyz2lab).
%
% Xuemei Zhang 1/28/96
% Last Modified 4/15/98
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% General Preparation %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%
if (nargin==2 | nargin==4) % if imageformat is not given, use default
imageformat = 'xyz';
elseif (nargin==3) % if only 3 arguments, the 3rd one is imageformat
imageformat = image2;
end
% force imageformat to be length 5 consistently, so that it is
% easy to do comparisons like (imageformat=='...').
imageformat = [imageformat ' '];
imageformat = imageformat(1:5);
% Check if the input images are 1-D or 2-D
imsize = size(image1);
if (imsize(1)>1 & prod(imsize(2:length(imsize)))>3) % 2-D images
dimension = 2;
else
dimension = 1;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Color Transformation %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('Performing color transformations ...');
% Convert XYZ or LMS representation to Poirson&Wandell opponent
% representation.
if (imageformat=='xyz10' | imageformat=='lms10')
xyztype = 10;
else
xyztype = 2;
end
if (imageformat(1:3)=='lms')
opp1 = changeColorSpace(image1, cmatrix('lms2opp'));
if (nargin>3)
opp2 = changeColorSpace(image2, cmatrix('lms2opp'));
oppwhite = changeColorSpace(whitepoint, cmatrix('lms2opp'));
whitepoint = changeColorSpace(oppwhite, cmatrix('opp2xyz', xyztype));
end
else
opp1 = changeColorSpace(image1, cmatrix('xyz2opp', xyztype));
if (nargin>3)
opp2 = changeColorSpace(image2, cmatrix('xyz2opp', xyztype));
end
end
clear image1; clear image2;
%%%%%%%%%%%%%%%%%%%%%%
%% Prepare filters %%
%%%%%%%%%%%%%%%%%%%%%%
disp('Preparing filters ...');
if (dimension == 1)
[k1, k2, k3] = separableFilters(sampPerDeg, 1);
else
[k1, k2, k3] = separableFilters(sampPerDeg, 3);
end
%%%%%%%%%%%%%%%%%%%%%%%%
%% Spatial Filtering %%
%%%%%%%%%%%%%%%%%%%%%%%%
% Apply the filters k1, k2, k3 to the images.
% The edges of the images are reflected for convolution.
if (length(imsize)==3)
w1 = opp1(:,:,1);
w2 = opp1(:,:,2);
w3 = opp1(:,:,3);
else
[w1, w2, w3] = getPlanes(opp1);
end
clear opp1;
wsize = size(w1);
if (dimension == 1)
w1 = pad4conv(w1, length(k1));
w2 = pad4conv(w2, length(k2));
w3 = pad4conv(w3, length(k3));
disp('Filtering BW plane of image1 ...');
p1 = resize(conv(w1, k1), wsize);
disp('Filtering RG plane of image1 ...');
p2 = resize(conv(w2, k2), wsize);
disp('Filtering BY plane of image1 ...');
p3 = resize(conv(w3, k3), wsize);
else
disp('Filtering BW plane of image1 ...');
p1 = separableConv(w1, k1, abs(k1));
disp('Filtering RG plane of image1 ...');
p2 = separableConv(w2, k2, abs(k2));
disp('Filtering BY plane of image1 ...');
p3 = separableConv(w3, k3, abs(k3));
end
new1 = [p1 p2 p3];
% If a second image is given, do the same filtering to the second image
% and then compute the CIELAB difference between them.
if (nargin>3)
if (length(imsize)==3)
w1 = opp2(:,:,1);
w2 = opp2(:,:,2);
w3 = opp2(:,:,3);
else
[w1, w2, w3] = getPlanes(opp2);
end
clear opp2;
if (dimension == 1)
w1 = pad4conv(w1, length(k1));
w2 = pad4conv(w2, length(k2));
w3 = pad4conv(w3, length(k3));
disp('Filtering BW plane of image2 ...');
p1 = resize(conv(w1, k1), wsize);
disp('Filtering RG plane of image2 ...');
p2 = resize(conv(w2, k2), wsize);
disp('Filtering BY plane of image2 ...');
p3 = resize(conv(w3, k3), wsize);
else
disp('Filtering BW plane of image2 ...');
p1 = separableConv(w1, k1, abs(k1));
disp('Filtering RG plane of image2 ...');
p2 = separableConv(w2, k2, abs(k2));
disp('Filtering BY plane of image2 ...');
p3 = separableConv(w3, k3, abs(k3));
end
new2 = [p1 p2 p3];
end
clear p1 p2 p3 w1 w2 w3 k1 k2 k3;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Return Appropriate Results %%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if (nargin<4) % return filtered image1 only
result = reshape(new1, imsize);
else % compute difference image
disp('Computing CIELAB differences ...');
result = changeColorSpace(new1, cmatrix('opp2xyz', xyztype));
result2 = changeColorSpace(new2, cmatrix('opp2xyz', xyztype));
% result = result .* (result>0);
% result2 = result2 .* (result2>0);
if (nargin == 6)
result = deltaLab(result, result2, whitepoint, 1/3, k);
else
result = deltaLab(result, result2, whitepoint);
end
end