-
Notifications
You must be signed in to change notification settings - Fork 2
/
thresholdobjects.h
167 lines (136 loc) · 5.5 KB
/
thresholdobjects.h
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
#pragma once
#include "cthresholdobject.h"
#include "dataobjects.h"
/** ITK Includes **/
#include <itkOtsuMultipleThresholdsImageFilter.h>
#include <itkTriangleThresholdImageFilter.h>
#include <itkRescaleIntensityImageFilter.h>
/** Otsu Threshold **/
class COtsuThresholdObject final : public CThresholdObject
{
public:
COtsuThresholdObject() = default;
COtsuThresholdObject(const COtsuThresholdObject& rhs) = delete;
COtsuThresholdObject& operator =(const COtsuThresholdObject& rhs) = delete;
static COtsuThresholdObject* CreateNew()
{
return new COtsuThresholdObject();
}
const QString& GetDescription() override
{
static const QString desc{"_Otsu"};
return desc;
}
int Process() override
{
/** Initializations **/
const auto cImage = GetInput();
const auto levels = GetLevels();
/** Sanity Chech - Input Image is Present **/
if (cImage.IsNotNull())
{
/** Initialize the Otsu Thresholder **/
using OtsuFilterType = itk::OtsuMultipleThresholdsImageFilter<ThresholdImageType, ThresholdImageType>;
using ThresholdVectorType = OtsuFilterType::ThresholdVectorType;
using RescaleFilterType = itk::RescaleIntensityImageFilter<InputImageType, ThresholdImageType> ;
/** Rescale the Image **/
auto rescaler = RescaleFilterType::New();
rescaler->SetOutputMinimum( 0 );
rescaler->SetOutputMaximum( 255 );
rescaler->SetInput(cImage);
/** Setup the Otsu Filter Input **/
auto otsuFilter = OtsuFilterType::New();
otsuFilter->SetInput(rescaler->GetOutput());
otsuFilter->SetNumberOfThresholds(levels);
otsuFilter->SetNumberOfHistogramBins(255);
/** Threshold THe Image **/
try
{
std::cout << "Performing the Threshold " << std::endl;
otsuFilter->Update();
}
catch (itk::ExceptionObject &excp)
{
std::cerr << "Error--> (" << __FILE__ << ":" << __LINE__ << ")" << std::endl;
std::cerr << "Otsu Threshold Operation Failed: " << excp << std::endl;
return -1;
}
/** Print The Thresholds on the Console - Will add a Status bar later **/
const auto& thresholdVector = otsuFilter->GetThresholds();
auto itNum = thresholdVector.begin();
for (; itNum < thresholdVector.end() ; itNum++)
{
std::cout << "OtsuThreshold[" << (int)(itNum - thresholdVector.begin()) << "] = " << (*itNum) << std::endl ;
}
/** Final Output Image **/
SetOutput(otsuFilter->GetOutput());
/** Final THresholded Color Label Map **/
//SetRGBOutput(otsuFilter->GetOutput());
return 1;
}
/** No Image **/
return -1;
} // End of Process()
};
/** Triangle Threshold **/
class CTriangleThresholdObject final : public CThresholdObject
{
public:
CTriangleThresholdObject() = default;
CTriangleThresholdObject(const CTriangleThresholdObject& rhs) = delete;
CTriangleThresholdObject& operator =(const CTriangleThresholdObject& rhs) = delete;
static CTriangleThresholdObject* CreateNew()
{
return new CTriangleThresholdObject();
}
const QString& GetDescription() override
{
static const QString desc{"_Triangle"};
return desc;
}
int Process() override
{
/** Initializations **/
const auto cImage = GetInput();
const auto levels = GetLevels();
/** Sanity Chech - Input Image is Present **/
if (cImage.IsNotNull())
{
/** Initialize the Otsu Thresholder **/
using TriangleFilterType = itk::TriangleThresholdImageFilter<ThresholdImageType, ThresholdImageType>;
using RescaleFilterType = itk::RescaleIntensityImageFilter<InputImageType, ThresholdImageType>;
/** Rescale the Image **/
auto rescaler = RescaleFilterType::New();
rescaler->SetOutputMinimum( 0 );
rescaler->SetOutputMaximum( 255 );
rescaler->SetInput(cImage);
/** Setup the Otsu Filter Input **/
auto triangleFilter = TriangleFilterType::New();
triangleFilter->SetInput(rescaler->GetOutput());
triangleFilter->SetNumberOfHistogramBins(255);
triangleFilter->SetInsideValue(0);
triangleFilter->SetOutsideValue(255);
/** Threshold THe Image **/
try
{
std::cout << "Performing the Threshold " << std::endl;
triangleFilter->Update();
}
catch (itk::ExceptionObject &excp)
{
std::cerr << "Error--> (" << __FILE__ << ":" << __LINE__ << ")" << std::endl;
std::cerr << "Triangle Threshold Operation Failed: " << excp << std::endl;
return -1;
}
/** Print The Thresholds on the Console - Will add a Status bar later **/
std::cout << "TriangleThreshold: " << (int)triangleFilter->GetThreshold() << std::endl ;
/** Final Output Image **/
SetOutput(triangleFilter->GetOutput());
/** Final THresholded Color Label Map **/
//SetRGBOutput(triangleFilter->GetOutput());
return 1;
}
/** No Image **/
return -1;
} // End of Process()
};