23 #include "bias_config.h"
24 #include "Bilateral.hh"
26 #include <Base/Image/ImageIO.hh>
27 #include <Base/Image/ImageConvert.hh>
29 #include <Base/Math/Vector2.hh>
35 template <
class InputStorageType,
class OutputStorageType>
37 :
FilterNToN<InputStorageType,OutputStorageType>()
49 template <
class InputStorageType,
class OutputStorageType>
52 :
FilterNToN<InputStorageType, OutputStorageType>(other)
65 template <
class InputStorageType,
class OutputStorageType>
70 template <
class InputStorageType,
class OutputStorageType>
74 if(newsize != _lastBilateralSize || secondsize!=_lastSecondSize){
75 _BilateralSize = newsize;
76 _lastBilateralSize =_BilateralSize;
78 _secondSize = secondsize;
79 _lastSecondSize = _secondSize;
82 _secondSize = _BilateralSize;
83 _lastSecondSize = _secondSize;
86 _CalculateKernels(_GaussSigma);
89 template <
class InputStorageType,
class OutputStorageType>
93 _GaussSigma = gaussSigma;
96 register double g=0.0;
97 _gaussFilterMask.Init(_BilateralSize*2+1,_secondSize*2+1,1);
98 float **fmida = _gaussFilterMask.GetImageDataArray();
101 for(
int xh=-_BilateralSize;xh<=_BilateralSize;xh++){
102 for(
int yh=-_secondSize;yh<=_secondSize;yh++){
104 double dist = zero.Dist(p)*zero.Dist(p);
105 g = exp(
double(-0.5*(
double)(dist/gaussSigma*gaussSigma)));
106 fmida[yh+_secondSize][xh+_BilateralSize] = float(g);
113 template <
class InputStorageType,
class OutputStorageType>
118 return FilterColorImg(src,dst);
124 bool identimg =
false;
139 register double g=0.0,gb=0.0;
141 float ** fmida = _gaussFilterMask.GetImageDataArray();
142 int y=0,x=0,xh=0,yh=0;
145 double sumGB=0.0,sumGBMulI=0.0;
147 double bilSigSq = _BilateralSigma*_BilateralSigma;
152 #ifdef BIAS_HAVE_OPENMP
153 #pragma omp parallel for private(y,x,xh,yh,g,gb,scrSq,sumGB,sumGBMulI)
155 for ( y=0; y < height; y++) {
156 for ( x=0; x < width; x++) {
157 if(srcida[y][x] != 0.0){
158 sumGB =sumGBMulI = 0.0;
160 for(yh = -_secondSize; yh <= _secondSize; yh++){
161 for(xh = -_BilateralSize; xh <= _BilateralSize; xh++){
162 if((y+yh >= 0) && (y+yh < height) &&
163 (x+xh >= 0) && (x+xh < width) &&
164 srcida[y][x] != _ignoreValue && srcida[y+yh][x+xh] != _ignoreValue)
167 g = (double)fmida[_secondSize+yh][_BilateralSize+xh];
170 scrSq = (double)(srcida[y][x]-srcida[y+yh][x+xh]);
172 gb = exp(
double(-0.5*(scrSq/bilSigSq)) );
177 sumGBMulI += g*gb*(double)srcida[y+yh][x+xh];
183 if(sumGB != 0.0 && count != 0 && !BIAS_ISNAN(sumGB) && !BIAS_ISINF(sumGB)){
186 dstida[y][x] = (OutputStorageType)(sumGBMulI / sumGB);
190 dstida[y][x] = (OutputStorageType) srcida[y][x];
201 template <
class InputStorageType,
class OutputStorageType>
208 BIASERR(
"Source and support image do not have same pixel and channel count!");
213 return FilterColorImg(src,support,dst);
220 bool identimg =
false;
235 register double g=0.0,gb=0.0;
236 float ** fmida = _gaussFilterMask.GetImageDataArray();
237 int y=0,x=0,xh=0,yh=0;
241 double sumGB=0.0,sumGBMulI=0.0;
243 double bilSigSq = _BilateralSigma*_BilateralSigma;
245 #ifdef BIAS_HAVE_OPENMP
246 #pragma omp parallel for private(y,x,xh,yh,g,gb,scrSq,sumGB,sumGBMulI)
248 for ( y=0; y < height; y++) {
249 for ( x=0; x < width; x++) {
250 if(srcida[y][x] != 0.0){
251 sumGB =sumGBMulI = 0.0;
253 for(xh=-_BilateralSize;xh<=_BilateralSize;xh++){
254 for(yh=-_secondSize;yh<=_secondSize;yh++){
255 if(y+yh >= 0 && y+yh < height &&
256 x+xh >= 0 && x+xh < width &&
257 srcida[y][x] != _ignoreValue && srcida[y+yh][x+xh] != _ignoreValue)
260 g = (double)fmida[_secondSize+yh][_BilateralSize+xh];
263 scrSq = (double)(suppida[y][x]-suppida[y+yh][x+xh]);
265 gb = exp(
double(-0.5*(scrSq/bilSigSq)) );
269 sumGBMulI += g*gb*srcida[y+yh][x+xh];
274 if(sumGB != 0.0 && !BIAS_ISNAN(sumGB) && !BIAS_ISINF(sumGB)){
275 dstida[y][x] = (OutputStorageType)(sumGBMulI / sumGB);
278 dstida[y][x] = (OutputStorageType) srcida[y][x];
288 template <
class InputStorageType,
class OutputStorageType>
292 BIASERR(
"FilterInt makes no sense here, using general Filter interface.");
293 return Filter(src, dst);
298 template <
class InputStorageType,
class OutputStorageType>
302 BIASERR(
"FilterFloat makes no sense here, using general Filter interface.");
303 return Filter(src, dst);
307 template <
class InputStorageType,
class OutputStorageType>
311 if (_secondSize > _BilateralSize) {
312 border_x = border_y = _secondSize;
314 border_x = border_y = _BilateralSize;
318 template <
class InputStorageType,
class OutputStorageType>
350 Filter(green, greenG);
361 template <
class InputStorageType,
class OutputStorageType>
408 Filter(red, redSupp,redG);
409 Filter(green, greenSupp, greenG);
410 Filter(blue, blueSupp, blueG);
424 #define FILTER_INSTANTIATION_CLASS Bilateral
425 #include "Filterinst.hh"
void Release()
reimplemented from ImageBase
virtual void GetBordersValid_(int &border_x, int &border_y) const
static void SetChannel(const ImageBase &im, const unsigned int channelId, const inputType *channelIn)
Copy channel, determines the internal ImageBase type and casts the input type to the type foreseen in...
void SetSize(int newsize, int secondsize=-1)
bool IsEmpty() const
check if ImageData_ points to allocated image buffer or not
double _GaussSigma
sigma of gaussian kernel
int _secondSize
Use this variable for non-quadratic filter sizes e.g. 3x1.
virtual int FilterFloat(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
unsigned int GetWidth() const
virtual int Filter(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
Bilateral filtering with given filter size (5x5 as standard)
int FilterColorImg(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
Filter a color image by calling Filter(...) for every channel.
InputStorageType _ignoreValue
int GetChannel(const ImageBase &source, const unsigned int channel)
copies one specific channel from source to Image can only be called from an planar image...
int _BilateralSize
half win size of filter, 1 means 3x3
int StealImage(ImageBase &source)
steals the image data array from source, after releasing the actual image data and sets source image ...
unsigned int GetChannelCount() const
returns the number of Color channels, e.g.
virtual int FilterInt(const Image< InputStorageType > &src, Image< OutputStorageType > &dst)
color values, 3 channels, order: red,green,blue
base class for simple n->n filter implementations
unsigned int GetHeight() const
bool SamePixelAndChannelCount(const ImageBase &Image) const
checks if data area has same "size" as Image of other type
void Init(unsigned int Width, unsigned int Height, unsigned int channels=1, enum EStorageType storageType=ST_unsignedchar, const bool interleaved=true)
calls Init from ImageBase storageType is ignored, just dummy argument
const StorageType * GetImageData() const
overloaded GetImageData() from ImageBase
enum EColorModel GetColorModel() const
static int Convert(BIAS::ImageBase &source, BIAS::ImageBase &dest, enum BIAS::ImageBase::EColorModel targetColorModel, bool bPlanar=false)
main general conversion function, calls desired specialized functions, always initializes the destIma...
void _CalculateKernels(double gaussSigma)
const StorageType ** GetImageDataArray() const
overloaded GetImageDataArray() from ImageBase