forked from Andre-lab/ZEAL_commandLine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZC.m
1627 lines (1130 loc) · 65.6 KB
/
ZC.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
classdef ZC < handle
% ZC Class with methods to compute Zernike-Canterakis moments and
% associated shape descripors. An object is created, but the class
% contains static methods that can be used without having an instance of
% the object. See the ZC (constructor method) for input details.
%
% Many methods are based on the C++ library by Novotni & Klein
% described in:
% Novotni, M., & Klein, R. (2003).
% 3D Zernike Descriptors for Content Based Shape Retrieval.
% Proceedings of the Eighth ACM Symposium on Solid Modeling and Applications,
% 216–225. https://doi.org/10.1145/781636.781639
%
%
% Filip (Persson) Ljung
%
%
% Last modified 2021-02-08
%
% IMPORTANT: The bug in the original C++ library that caused the shape
% descriptors to be cumulative has been fixed in this library.
%
% TODO: Unit test
%
% -------------------------------------------------------------
properties
Order % integer: max ZC expansion order
Settings % struct :
Moments % struct :
% Moments.Values = complex-valued vector with ZC moments
% Moments.CellValues = 3d cell for complex-valued ZC moments arranged based on their labels,
% i.e. ZCmoments3Dcell{n,l,m} = ZCmom_nlm
% Moments.IndicesList = Nx5 col 1-3 are the n,l,m
% indices and col 4-5 are the real and imaginary
% components of the moments respectively.
Descriptors % vector: the real-valued ZC shape descriptor
ShapeFunction % 3D matrix: the cubic grid with voxel-values defining the shape function
ChiCoeffs % : object (ChiCoeffs class)
%
% Values : 3d cell array
% Indices : 3d cell arrayx
% Order : integer
GridRes % integer: side length (in grid units) of the cubic grid containing the shape function
end
methods
function obj = ZC(shapeFunction, order, chiCoeffs, varargin)
% ZC Construct an instance of this class
%
% INPUT
% -------------------------------------------------------------
% <shapeFunction> : NxNxN double/logical
% (cubic) grid with voxel-values defining the shape function
% <order> : integer
% The max ZC expansion order
%
% <ChiCoeffs> : struct with fields (from ZC.computeChiCoeffs in this class)
%
% <ChiCoeffs.Values> : 3d cell array
% <ChiCoeffs.Indices> : 3d cell arrayx
% <ChiCoeffs.Order> : integer
%
% OPTIONAL
% 'name'-value pairs (default)
%
% 'scaleOption' : 1<= integer <= 3 (1)
% Option for object scaling during normalization
%
% 'showLog' : true/false (false)
%
% -------------------------------------------------------------
defaultScaleOption = 1;
defaultShowLogOp = false; % for debugging
% ----- Option parsing and parameter setup -----
p = inputParser;
% set required
addRequired(p, 'shapeFunction', @(x)validateattributes(x,{'numeric', 'logical'}, {'nonempty'}, 'shapeFunction'));
addRequired(p, 'order', @(x)validateattributes(x,{'numeric'}, {'nonempty','integer','positive'}, 'order'));
addRequired(p, 'chiCoeffs');
% set optional
addOptional(p, 'scaleOption', defaultScaleOption, @(x)validateattributes(x,{'numeric'}, {'nonempty','integer','>=1','<=3'}, 'scaleOption'));
addOptional(p, 'ShowLog', defaultShowLogOp);
parse(p, shapeFunction, order, chiCoeffs, varargin{:});
% ----- Store parameters to object -----
obj.Order = p.Results.order;
obj.Settings.scaleOption = p.Results.scaleOption;
obj.Settings.ShowLog = p.Results.ShowLog;
obj.ShapeFunction = p.Results.shapeFunction;
obj.ChiCoeffs.Values = p.Results.chiCoeffs.Values;
obj.ChiCoeffs.Indices = p.Results.chiCoeffs.Indices;
obj.ChiCoeffs.Order = p.Results.chiCoeffs.Order;
obj.GridRes = size(obj.ShapeFunction, 1);
% Check that we have the right chi coeffcients to compute the
% ZC moments (at least to the degree that their size/assigned order
% matches the order).
if ~isequal(obj.ChiCoeffs.Order, obj.Order)
error('The order of the Chi coefficients is %d, but needs to be equal to the desired order=%d.', obj.ChiCoeffs.Order, obj.Order);
end
end
function computeMoments(obj)
if obj.Settings.ShowLog
fprintf('\n ZC.computeMoments(obj): Perform ZC moment computation');
startTime = tic;
end
% flatten 3d grid to 1d grid
if obj.Settings.ShowLog
fprintf('\n\t Flattening cubic grid to linear grid');
end
linearGrid = ZC.cubic2LinearGrid(obj.ShapeFunction, obj.GridRes);
% Compute first order geometric moments
if obj.Settings.ShowLog
fprintf('\n\t Computing geometric moments up to first order (the center of gravity)');
end
[~, geoMom_COM] = ZC.computeGeoMoments(linearGrid, obj.GridRes , 0, 0, 0, 1, 1);
% 0'th order moments -> normalization
null_moment = geoMom_COM(1,1,1);
% 1'st order moments -> center of gravity
xCOM = geoMom_COM(2,1,1) / null_moment;
yCOM = geoMom_COM(1,2,1) / null_moment;
zCOM = geoMom_COM(1,1,2) / null_moment;
% Scale to fit inside unit ball
if obj.Settings.ShowLog
fprintf('\n\t Computing scale factor to fit inside unit sphere using option=%d', obj.Settings.scaleOption);
end
[scale, ~] = ZC.computeScaling(obj.Settings.scaleOption, linearGrid, obj.GridRes, xCOM, yCOM, zCOM);
% Normalize: cut off object function for values outide unit
if obj.Settings.ShowLog
fprintf('\n\t Normalizing shape function');
end
linearGridNorm = ZC.normalizeGrid(linearGrid, obj.GridRes , xCOM, yCOM, zCOM, scale);
% Compute geometric moments
if obj.Settings.ShowLog
fprintf('\n\t Computing geometric moments up to order %d', obj.Order);
end
[~, geoMoments] = ZC.computeGeoMoments(linearGridNorm, obj.GridRes , xCOM, yCOM, zCOM, scale, obj.Order);
% Compute ZC moments
if obj.Settings.ShowLog
fprintf('\n\t Computing ZC moments up to order %d\n', obj.Order);
end
[obj.Moments.IndicesList, obj.Moments.CellValues] = ZC.computeZCmoments(obj.Order, obj.ChiCoeffs.Values, obj.ChiCoeffs.Indices, geoMoments);
obj.Moments.Values = complex(obj.Moments.IndicesList(:,4), obj.Moments.IndicesList(:,5));
if obj.Settings.ShowLog
stopTime = toc(startTime);
fprintf('\n Done. Execution time %2.2e s', stopTime);
end
end
function computeDescriptors(obj)
% COMPUTE_ZCINVARIANTS
% Computes the Zernike-Canterakis shape descriptors, which are the norms of vectors with
% components of Z_nl^m with m being the running index.
% The algorithm is based on the C++ code by Novotni & Klein described in
%
% % Novotni, M., & Klein, R. (2003).
% 3D Zernike Descriptors for Content Based Shape Retrieval.
% Proceedings of the Eighth ACM Symposium on Solid Modeling and Applications,
% 216–225. https://doi.org/10.1145/781636.781639
%
% The bug in the original C++ library that caused the shape
% descriptors to be cumulative has been fixed here.
%
% -------------------------------------------------------------
if isempty(obj.Moments)
computeMoments(obj);
end
if obj.Settings.ShowLog
fprintf('\n\t Computing ZC shape descriptors from moments up to order %d\n', obj.Order);
end
nInvariants = ZC.numberOfInvariants(obj.Order);
obj.Descriptors = zeros(nInvariants,1);
inv_count = 0;
for n = 0:obj.Order
% sum_tmp = 0; % NB This is the bug in the original N&K
% code that caused the invariants to be cumulatative. Keep
% this information for legacy reasons.
for l = mod(n,2):2:n
sum_tmp = 0;
for m=-l:l
absM = abs(m);
% The ZC_nlm moment
mom = obj.Moments.CellValues(n+1, l+1, absM+1);
%conjugate if m negative
if m<0
mom = conj(mom);
% take care of sign for odd m
if mod(absM,2)
mom = -1*mom;
end
end
sum_tmp = sum_tmp + norm(mom)^2;
% the C++ std:norm function gives the square of the L2
%(euclidian norm), which is the so called field norm
end
inv_count = inv_count + 1;
obj.Descriptors(inv_count,1) = sqrt(sum_tmp);
end
end
end
function computeDescriptors_NKbugVersion(obj)
% COMPUTE_ZCINVARIANTS
% Computes the Zernike-Canterakis shape descriptors, which are the norms of vectors with
% components of Z_nl^m with m being the running index.
% The algorithm is based on the C++ code by Novotni & Klein described in
%
% % Novotni, M., & Klein, R. (2003).
% 3D Zernike Descriptors for Content Based Shape Retrieval.
% Proceedings of the Eighth ACM Symposium on Solid Modeling and Applications,
% 216–225. https://doi.org/10.1145/781636.781639
%
%
% NB: There is a known bug in this code which
%
% -------------------------------------------------------------
if isempty(obj.Moments)
computeMoments(obj);
end
if obj.Settings.ShowLog
fprintf('\n\t Computing ZC shape descriptors from moments up to order %d\n', obj.Order);
end
nInvariants = ZC.numberOfInvariants(obj.Order);
obj.Descriptors = zeros(nInvariants,1);
inv_count = 0;
for n = 0:obj.Order
sum_tmp = 0; % NB This is a bug that causes the invariants to be cumulatative
for l = mod(n,2):2:n
for m=-l:l
absM = abs(m);
% The ZC_nlm moment
mom = obj.Moments.CellValues(n+1, l+1, absM+1);
%conjugate if m negative
if m<0
mom = conj(mom);
% take care of sign for odd m
if mod(absM,2)
mom = -1*mom;
end
end
sum_tmp = sum_tmp + norm(mom)^2;
% the C++ std:norm function gives the square of the L2
%(euclidian norm), which is the so called field norm
end
inv_count = inv_count + 1;
obj.Descriptors(inv_count,1) = sqrt(sum_tmp);
end
end
end
function setShapeFunction(obj, cubicGrid)
obj.cubicGrid = cubicGrid;
end
end
methods (Static)
function [linearGrid] = cubic2LinearGrid(cubicGrid, gridRes)
% ZC.cubic2LinearGrid
% Flatten 3D (cubic) voxel-grid to 1D vector.
% xyz coordinates are mapped to the vector as
% element_id = dim*((z-1)*dim+(x-1)) + (y-1)+1;
% where dim is the side length in grid intervals of the cubic grid.
% INPUT
% -------------------------------------------------------------
% meshgrid_3D : NxNxN matrix containing the voxelized object
% N = grid_res
% OUTPUT
% -------------------------------------------------------------
% linearGrid : 1D vector
% -------------------------------------------------------------
linearGrid = zeros(gridRes^3,1);
for x = 1:gridRes
for y = 1:gridRes
for z = 1:gridRes
if cubicGrid(x,y,z)>0
% id = grid_res*((z-1)*grid_res+(y-1)) + (x-1)+1;
id = gridRes*((z-1)*gridRes+(x-1)) + (y-1)+1;
linearGrid(id) = cubicGrid(x,y,z);
end
end
end
end
end
function [geo_mom_list, geo_moments] = computeGeoMoments(LinearGrid, gridRes , xCOM, yCOM, zCOM, scale, order)
% ZC.computeGeoMoments
% Computes geometric moments M_rst up to order n for a voxelized object
% within a grid. The object, with center of mass at (x|y|z)COG, is scaled to fit within
% the unit ball before M_rst is computed for each combination of indices,
% such that r,s,t>0 and r+s+t<n. The algorithm is based on the C++ code by
% Novotni & Klein described in
%
% % Novotni, M., & Klein, R. (2003).
% 3D Zernike Descriptors for Content Based Shape Retrieval.
% Proceedings of the Eighth ACM Symposium on Solid Modeling and Applications,
% 216–225. https://doi.org/10.1145/781636.781639
% INPUT
% -------------------------------------------------------------
% <LinearGrid> : 1D vector
% The flatted voxelized object, where x, y and z coordinates are mapped to
% the array elements as ((z-1) * dim + y-1) * dim + x-1+1
% <dim> : integer
% The side-length resolution of the cubic grid containing the voxelized
% object.
% <xCOG>, <yCOG>, <zCOG> : float
% The center of gravity of the object in the x, y and z dimension
% respectively
% <scale> : float
% The scaling factor to fit object into unit ball
% <order> : integer
% The maximum order for geometric moments.
% OUTPUT
% -------------------------------------------------------------
% <geo_mom_list> : [n x 4] matrix, float
% The geometric moments for each combination of indices r, s, t.
% Column 1 = index r
% Column 2 = index s
% Column 3 = index t
% Column 4 = geo metric moment
% <geo_moments> : [(order+1) x (order+1) x (order +1)] matrix
% Each element contains the geometric moments for a given combinatino of
% indices r,s,t, i.g. M_{r=2,s=2,t=2} = geo_moments(r=2,s=2,t=2)
% -------------------------------------------------------------
% Scale object to fit into unit ball
xDim = gridRes;
yDim = gridRes;
zDim = gridRes;
dimVec = [xDim yDim zDim];
min_v = zeros(3,1);
min_v(1) = (-xCOM+1)*scale;
min_v(2) = (-yCOM+1)*scale;
min_v(3) = (-zCOM+1)*scale;
samples = zeros(max(dimVec)+1,3);
for r=1:3
for s=0:dimVec(r)
samples(s+1,r)=min_v(r) + s*scale;
end
end
% Compute geometric moments
arrayDim = zDim;
layerDim = yDim*zDim;
diffArrayDim = zDim+1;
diffLayerDim = (yDim+1)*zDim;
diffGridDim = (xDim+1)*layerDim;
diffGrid = zeros(diffGridDim,1);
diffLayer = zeros(diffLayerDim,1);
diffArray = zeros(diffArrayDim,1);
layer = zeros(layerDim,1);
array = zeros(arrayDim,1);
geo_moments = zeros(order+1, order+1, order+1);
geo_mom_list = zeros(ZC.numberOfMoments(order),4);
% generate the diff version of the voxel grid in x direction
iter = 1;
diffIter = 1;
for x = 0:layerDim-1
% for x = 0:0
diffGrid = ZC.ComputeDiffFunction(LinearGrid, diffGrid, iter, diffIter, xDim);
iter = iter + xDim;
diffIter = diffIter + xDim + 1;
end
count = 0;
for r=0:order
% diffGrid
diffIter = 1;
for p=0:layerDim-1
sampleIter = 1;
[layer(p+1), diffGrid] = ZC.Multiply(diffGrid, samples(:,1), diffIter, sampleIter, xDim+1);
diffIter = diffIter + xDim + 1;
end
% layer
iter = 1;
% diffLayer
diffIter = 1;
for y=0:arrayDim-1
diffLayer = ZC.ComputeDiffFunction(layer, diffLayer, iter, diffIter, yDim);
iter = iter + yDim;
diffIter = diffIter + yDim + 1;
end
for s=0:order-r
% diffLayer
diffIter = 1;
for p=0:arrayDim-1
sampleIter = 1;
[array(p+1), diffLayer] = ZC.Multiply(diffLayer, samples(:,2), diffIter, sampleIter, yDim+1);
diffIter = diffIter + yDim + 1;
end
% array
iter = 1;
% diffarray
diffIter = 1;
diffArray = ZC.ComputeDiffFunction(array, diffArray, iter, diffIter, zDim);
for t=0:order-r-s
count = count+1;
sampleIter = 1;
[moment, diffArray] = ZC.Multiply(diffArray, samples(:,3), diffIter, sampleIter, zDim+1);
geo_moments(r+1,s+1,t+1) = moment / ( (1+r)*(1+s)*(1+t) );
geo_mom_list(count,:) = [r s t geo_moments(r+1,s+1,t+1)];
end
end % j
end %i
geo_mom_list(count+1:end,:) = [];
end
function [B] = ComputeDiffFunction(A, B, Aiter, Biter, dim)
B(Biter) = -1*A(Aiter);
for i = 1:dim-1
B(Biter+i) = A(Aiter+i-1)-A(Aiter+i);
end
B(Biter+dim) = A(Aiter+(dim-1));
end
function [sum_val, diffGrid] = Multiply(diffGrid, sample, diffIter, sampleIter, dim)
sum_val = 0;
for i=0:dim-1
diffGrid(diffIter+i) = diffGrid(diffIter+i) * sample(sampleIter+i);
sum_val = sum_val + diffGrid(diffIter+i);
end
end
function [scaling_factor, max_extent] = computeScaling(scale_option, voxels, dim, xCOG, yCOG, zCOG)
% ZC.computeScaling
% To compute Zernike-Canterakis moments for a 3d object, the object has to
% be scaled so that it can fit inside the unit-ball (where the ZC functions live).
% This function gives that scaling factor by two different methods. Reproduction accuracy
% falls off close to the ball surface (due to discretization effects?) and both methods make sure
% the scaling avoids this. Novotni and Klein used method 2 as per their code (not described in paper)
% without any justification. Method 1 is used by Grandison, Roberts and Morris as per the paper,
% but no justification for why the factor of 0.6 was chosen.
% Novotni, M., & Klein, R. (2003).
% 3D Zernike Descriptors for Content Based Shape Retrieval.
% Proceedings of the Eighth ACM Symposium on Solid Modeling and Applications,
% 216–225. https://doi.org/10.1145/781636.781639
% Grandison, S., Roberts, C., & Morris, R. J. (2009).
% The Application of 3D Zernike Moments for the Description of
% “Model-Free” Molecular Structure, Functional Motion, and Structural Reliability.
% Journal of Computational Biology, 16(3), 487–500. https://doi.org/10.1089/cmb.2008.0083
% INPUT
% -------------------------------------------------------------
% <scale_option> : 1,2,3
% 1 = scaling so that the maximum distance between a filled voxel
% and the grid centroid is 70 % of the unit ball radius.
% 2 = (Used by Novotni & Klein) = scaling by 2 x Radius of gyration
% (average distance between filled voxels and the grid
% centroid).
% 3 = scaling so that the maximum distance between a
% filled voxel and the grid centroid is 100 % of the unit ball radius.
% <voxels> : 1D vector
% The flatted voxelized object, where x, y and z coordinates are mapped to
% the array elements as ((z-1) * dim + y-1) * dim + x-1+1
% <dim> : integer
% The side-length resolution of the cubic grid containing the voxelized
% object.
% <xCOG>, <yCOG>, <zCOG> : float
% The center of gravity of the object in the x, y and z dimension
% respectively
% OUTPUT
% -------------------------------------------------------------
% <scaling_factor> : float
% The scaling factor for fitting object voxels into the unit ball.
% <Rmax> : float
% Maximum distance from grid centroid
% <max_extent> : float
% For scaling option 1: The fraction of the unit ball radius where the largest distance from the
% centroid is
% <voxel_resolution> : float
% The resolution (in Ångström) captured by a voxel
% -------------------------------------------------------------
switch scale_option
case 1
Rmax = 0;
for x = 1:dim
for y = 1:dim
for z = 1:dim
id = ((z-1) * dim + y-1) * dim + x-1+1;
if voxels(id) > 0
mx = x - xCOG;
my = y - yCOG;
mz = z - zCOG;
temp = mx^2+my^2+mz^2;
if temp > Rmax
Rmax = temp;
end
end
end
end
end
max_extent = 0.7;
scaling_factor = max_extent*(1 / sqrt(Rmax));
% Rmax = sqrt(Rmax);
case 2
temp=0;
nvox = 0;
for x = 1:dim
for y = 1:dim
for z = 1:dim
id = ((z-1)* dim + y-1) * dim + x-1+1;
if voxels(id) > 0
mx = x - xCOG;
my = y - yCOG;
mz = z - zCOG;
temp = temp + mx^2+my^2+mz^2;
nvox=nvox+1;
end
end
end
end
Rmean = sqrt(temp/nvox);
max_extent = 0.5;
scaling_factor = 1/(2* Rmean);
case 3
Rmax = 0;
for x = 1:dim
for y = 1:dim
for z = 1:dim
id = ((z-1) * dim + y-1) * dim + x-1+1;
if voxels(id) > 0
mx = x - xCOG;
my = y - yCOG;
mz = z - zCOG;
temp = mx^2+my^2+mz^2;
if temp > Rmax
Rmax = temp;
end
end
end
end
end
max_extent = 1;
scaling_factor = max_extent*(1 / sqrt(max(Rmax)));
end
% voxel_resolution = Rmax / ( (dim/2));
end
function [zernikeMoments_list, zernikeMoments] = computeZCmoments(order, chi_coeff_cell, chi_nlm_rst_cell, geo_moments)
% ZC.computeZCmom
% Computes the Zernike moments of order n from geometric moments
% and the object-independent chi-coefficients of the same order.
% This computation is data dependent and has to be performed for
% each new object and/or transformation.
% The algorithm is based on the C++ code by Novotni % Klein,
% described in
% % Novotni, M., & Klein, R. (2003).
% 3D Zernike Descriptors for Content Based Shape Retrieval.
% Proceedings of the Eighth ACM Symposium on Solid Modeling and Applications,
% 216–225. https://doi.org/10.1145/781636.781639
% INPUT
% -------------------------------------------------------------
% <order> : integer
% The maximum expansion order
% <chi_coeff_cell> : cell array
% The chi coefficients from ZC.computeChiCoeffs (precomputed and
% loaded from mat files typically (can be expensive to compute))
% <chi_nlm_rst_cell> : cell array
% The indices to label the chi coefficients from ZC.computeChiCoeffs
% OUTPUT
% -------------------------------------------------------------
% <zernikeMoments_list> : NxM matrix
% <zernikeMoments> :
% -------------------------------------------------------------
zernikeMoments = zeros(order+1, order+1, order+1);
zernikeMoments_list = zeros(ZC.numberOfMoments(order),5);
momcount = 0;
pi_factor = (3/(4*pi));
for n=0:order
l0 = mod(n,2);
for l = l0:2:n
for m=-l:l
M=abs(m);
zm = complex(0);
% get chi coeffs
chi_nlm_rst = chi_nlm_rst_cell{n+1,l+1,M+1};
chi_values = chi_coeff_cell{n+1,l+1,M+1};
nCoeffs = size(chi_nlm_rst,1);
for i = 1:nCoeffs
r = chi_nlm_rst(i,4)+1;
s = chi_nlm_rst(i,5)+1;
t = chi_nlm_rst(i,6)+1;
% geoMoms_test(count2,:) = [n l m r-1 s-1 t-1 real(chi_values(i)) imag(chi_values(i)) geo_moments(r,s,t)];
zm = zm + conj(chi_values(i)) * geo_moments(r,s,t);
end
zm = zm * pi_factor;
if n == 0 && l == 0 && m == 0
nullMoment = real(zm);
end
if m<0
zernikeMoments(n+1,l+1,m+l+1) = (-1)^M * conj(zm);
else
zernikeMoments(n+1,l+1,m+l+1) = zm;
end
momcount = momcount + 1;
zernikeMoments_list(momcount,1) = n;
zernikeMoments_list(momcount,2) = l;
zernikeMoments_list(momcount,3) = m;
zernikeMoments_list(momcount,4) = real(zernikeMoments(n+1,l+1,m+l+1));
zernikeMoments_list(momcount,5) = imag(zernikeMoments(n+1,l+1,m+l+1));
end % m
end % l
end % n
zernikeMoments_list(momcount+1:end,:) = [];
end
function [ChiCoeff, chiCoeffList] = computeChiCoeffs(order)
% ZC.computeChiCoeffs
% Computes the coefficients associated with geometric moments used in the computation
% of Zernike-Canterakis (ZC) moments. These coefficients do not depend on the object
% and should be pre-computed and stored for computational efficiency
% The algorithm is based on the C++ code by Novotni % Klein described in
%
% % Novotni, M., & Klein, R. (2003).
% 3D Zernike Descriptors for Content Based Shape Retrieval.
% Proceedings of the Eighth ACM Symposium on Solid Modeling and Applications,
% 216–225. https://doi.org/10.1145/781636.781639
% INPUT
% -------------------------------------------------------------
% <order> : integer
% order for the ZC moments
% OUTPUT
% -------------------------------------------------------------
% ChiCoeff struct with field
% Values : 3d cell array with dim (order+1)x(order+1)x(order+1)
% Each cell contains coefficients for all [r,s,t] (indices) triplets associated with an [n,l,m] triplet
% These are obtained using the syntax ChiCoeff.Values{n,l,m}
% Indices : 3d cell array with dim (order+1)x(order+1)x(order+1)
% Each cell contains the [n,l,m] indices (col 1-3)
% and the combinations of [r,s,t] indices associated with that [n,l,m] triplet.
% These are obtained using the syntax ChiCoeff.Indices{n,l,m}
% chi_coeff : Nx8 matrix for the N combinations of [n,l,m] (col 1-3)
% and [r,s,t] (col 4-6) triplets.
% col 7 and 8 contains the real and imaginary values
% associated with the triplet combinations.
%
% -------------------------------------------------------------
count_coeff = 0;
setStart = 1;
set_count = 0;
chi_coeff = zeros((order+1)^4,8);
chi_coeff_cell = cell(order+1, order+1, order+1);
chi_nlm_rst_cell = cell(order+1, order+1, order+1);
chi_ind_map = zeros((order+1)^3,6);
fprintf('\n Computing chi coefficients for order = %d', order);
n_count_vec = zeros(order+1,1);
for n = 0:order
fprintf('\n Doing order %d', n);
li=0;
l0 = mod(n,2);
n_count = 0;
for l = l0:2:n % only even values of l since Zernike functions require that (n-l) is even
li=li+1;
for m = 0:l
c_set_count=0;
cs = c_lm(l,m) ;
w = cs/ 2^(m);
k=round( (n-l)/2 );
for nu = 0:k
qs = q_klnu(k,l,nu);
w_Nu = w * qs;
for alpha = 0:nu
w_NuA = w_Nu * nchoosek(nu,alpha);
for beta = 0:(nu-alpha)
w_NuAB = w_NuA * nchoosek(nu-alpha, beta);
for p = 0:m
w_NuABP = w_NuAB * nchoosek(m,p);
for mu = 0:floor((l-m)/2)
w_NuABPMu = w_NuABP ...
* nchoosek(l, mu) ...
* nchoosek(l-mu, m+mu) ...
/ 2^(2 * mu);
for q = 0:mu
w_NuABPMuQ = w_NuABPMu * nchoosek(mu, q);
% the sign
if mod((m-p+mu),2)
w_NuABPMuQ = -1 * w_NuABPMuQ;
end
rest = mod(p,4);
switch rest
case 0
c = complex(w_NuABPMuQ, 0);
case 1
c = complex(0, w_NuABPMuQ);
case 2
c = complex(-1 * w_NuABPMuQ, 0);
case 3
c = complex(0, -1 * w_NuABPMuQ);
end
t_i = l - m + 2 * (nu - alpha - beta - mu);
s_i = 2 * (mu - q + beta) + m - p;
r_i = 2 * q + p + 2 * alpha;
c_set_count = c_set_count +1;
count_coeff = count_coeff + 1;
n_count = n_count + 1;
chi_coeff(count_coeff,1) = n;
chi_coeff(count_coeff,2) = l;
chi_coeff(count_coeff,3) = m;
chi_coeff(count_coeff,4) = r_i;
chi_coeff(count_coeff,5) = s_i;
chi_coeff(count_coeff,6) = t_i;
chi_coeff(count_coeff,7) = real(c);
chi_coeff(count_coeff,8) = imag(c);
end %q
end % mu
end % p
end % beta
end % alpha
end % nu
if c_set_count > 0
set_count = set_count + 1;
chi_ind_map(set_count,1) = n;
chi_ind_map(set_count,2) = l;
chi_ind_map(set_count,3) = m;
chi_ind_map(set_count, 4) = setStart;
chi_ind_map(set_count, 5) = setStart + c_set_count - 1;
setStart = chi_ind_map(set_count, 5) + 1;
end
sel_int = chi_ind_map(set_count, 4): chi_ind_map(set_count, 5);
chi_coeff_cell{n+1,l+1,m+1} = complex( chi_coeff(sel_int, 7), chi_coeff(sel_int, 8) );
chi_nlm_rst_cell{n+1,l+1,m+1} = chi_coeff(sel_int, 1:6);