1 /*
2  * Medical Image Registration ToolKit (MIRTK)
3  *
4  * Copyright 2016-2017 Imperial College London
5  * Copyright 2016-2017 Andreas Schuh
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 
20 #include "mirtk/LabelConsistency.h"
21 
22 #include "mirtk/ObjectFactory.h"
23 
24 
25 namespace mirtk {
26 
27 
28 // Register energy term with object factory during static initialization
29 mirtkAutoRegisterEnergyTermMacro(LabelConsistency);
30 
31 
32 // =============================================================================
33 // Construction/Destruction
34 // =============================================================================
35 
36 // -----------------------------------------------------------------------------
LabelConsistency(const char * name)37 LabelConsistency::LabelConsistency(const char *name)
38 :
39   HistogramImageSimilarity(name)
40 {
41 }
42 
43 // -----------------------------------------------------------------------------
LabelConsistency(const LabelConsistency & other)44 LabelConsistency::LabelConsistency(const LabelConsistency &other)
45 :
46   HistogramImageSimilarity(other)
47 {
48 }
49 
50 // -----------------------------------------------------------------------------
~LabelConsistency()51 LabelConsistency::~LabelConsistency()
52 {
53 }
54 
55 // =============================================================================
56 // Initialization
57 // =============================================================================
58 
59 // -----------------------------------------------------------------------------
Initialize()60 void LabelConsistency::Initialize()
61 {
62   // Number of target and source bins must be equal
63   if (_Samples && !_SamplesOwner) {
64     if (_Samples->NumberOfBinsX() != _Samples->NumberOfBinsY()) {
65       cerr << this->NameOfType() << "::Initialize: External joint histogram must"
66           " have equal number of bins in both dimensions" << endl;
67       exit(1);
68     }
69   } else {
70     if (_NumberOfTargetBins <= 0) {
71       double tmin, tmax;
72       Target()->InputImage()->GetMinMaxAsDouble(&tmin, &tmax);
73       _NumberOfTargetBins = DefaultNumberOfBins(Target()->InputImage(), tmin, tmax);
74     }
75     if (_NumberOfSourceBins <= 0) {
76       double smin, smax;
77       Source()->InputImage()->GetMinMaxAsDouble(&smin, &smax);
78       _NumberOfSourceBins = DefaultNumberOfBins(Source()->InputImage(), smin, smax);
79     }
80     _NumberOfTargetBins = _NumberOfSourceBins = (_NumberOfTargetBins + _NumberOfSourceBins) / 2;
81   }
82 
83   // Initialize base class
84   HistogramImageSimilarity::Initialize();
85 }
86 
87 // =============================================================================
88 // Evaluation
89 // =============================================================================
90 
91 // -----------------------------------------------------------------------------
Evaluate()92 double LabelConsistency::Evaluate()
93 {
94   return _Histogram.LabelConsistency();
95 }
96 
97 
98 } // namespace mirtk
99