forked from InsightSoftwareConsortium/ITKWikiExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathitkOilPaintingImageFilter.txx
92 lines (77 loc) · 2.54 KB
/
itkOilPaintingImageFilter.txx
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
#ifndef __itkOilPaintingImageFilter_txx
#define __itkOilPaintingImageFilter_txx
#include "itkOilPaintingImageFilter.h"
#include "itkObjectFactory.h"
#include "itkImageRegionIterator.h"
#include "itkImageRegionConstIterator.h"
#include "itkNeighborhoodIterator.h"
#include "itkMinimumMaximumImageCalculator.h"
namespace itk
{
template<class TImage>
OilPaintingImageFilter<TImage>::OilPaintingImageFilter()
{
this->m_NumberOfBins=20;
this->SetRadius(5);
}
template<class TImage>
void OilPaintingImageFilter<TImage>::SetRadius(unsigned int radius)
{
for (unsigned int i = 0; i < TImage::ImageDimension; ++i)
{
m_Radius[i] = radius;
}
}
template<class TImage>
void OilPaintingImageFilter<TImage>::BeforeThreadedGenerateData()
{
using CalculatorType = itk::MinimumMaximumImageCalculator< TImage >;
typename CalculatorType::Pointer calculatorI = CalculatorType::New();
calculatorI->SetImage( this->GetInput() );
calculatorI->Compute();
m_Maximum = calculatorI->GetMaximum();
m_Minimum = calculatorI->GetMinimum();
}
template<class TImage>
void OilPaintingImageFilter<TImage>
::ThreadedGenerateData(const typename Superclass::OutputImageRegionType& outputRegionForThread, ThreadIdType threadId)
{
typename TImage::ConstPointer input = this->GetInput();
typename TImage::Pointer output = this->GetOutput();
itk::ImageRegionIterator<TImage> out(output, outputRegionForThread);
itk::ConstNeighborhoodIterator<TImage> it(m_Radius, input, outputRegionForThread);
unsigned long long *bins=new unsigned long long[m_NumberOfBins];
while(!out.IsAtEnd())
{
for (unsigned int i=0; i<m_NumberOfBins; i++)
{
bins[i]=0; //reset histogram
}
//create histogram of values within the radius
for (unsigned int i = 0; i < it.Size(); ++i)
{
typename TImage::ValueType val=it.GetPixel(i);
bins[int((m_NumberOfBins-0.001)*(val-m_Minimum)/(m_Maximum-m_Minimum))]++;
//casting to int rounds towards zero
//without -0.001, when val=max then we would go over the upper bound (index m_NumberOfBins)
}
//find the peak
unsigned maxIndex=0;
unsigned long long maxBin=bins[0];
for (unsigned int i=1; i<m_NumberOfBins; i++)
{
if (bins[i]>maxBin)
{
maxBin=bins[i];
maxIndex=i;
}
}
//set middle value of a bin's range as intensity
out.Set((maxIndex+0.5)*(m_Maximum-m_Minimum)/m_NumberOfBins);
++it;
++out;
}
delete bins; //dealloc array
}
}// end namespace
#endif //__itkOilPaintingImageFilter_txx