Fawkes API  Fawkes Development Version
laplace.cpp
1 
2 /***************************************************************************
3  * laplace.cpp - Implementation of a laplace filter
4  *
5  * Created: Thu Jun 16 16:30:23 2005
6  * Copyright 2005-2012 Tim Niemueller [www.niemueller.de]
7  ****************************************************************************/
8 
9 /* This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version. A runtime exception applies to
13  * this software (see LICENSE.GPL_WRE file mentioned below for details).
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU Library General Public License for more details.
19  *
20  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
21  */
22 
23 #include <fvfilters/laplace.h>
24 
25 #include <core/exception.h>
26 
27 #include <cmath>
28 #include <cstdlib>
29 
30 #ifdef HAVE_IPP
31 # include <ippi.h>
32 #elif defined(HAVE_OPENCV)
33 # include <cv.h>
34 # include <opencv2/imgproc.hpp>
35 #else
36 # error "Neither IPP nor OpenCV available"
37 #endif
38 
39 namespace firevision {
40 #if 0 /* just to make Emacs auto-indent happy */
41 }
42 #endif
43 
44 /** @class FilterLaplace <fvfilters/laplace.h>
45  * Laplacian filter.
46  * Laplacian of Gaussian filter.
47  * @author Tim Niemueller
48  */
49 
50 /** Constructor. */
52  : Filter("FilterLaplace")
53 {
54  kernel = NULL;
55  kernel_float = NULL;
56 }
57 
58 
59 /** Constructor.
60  * @param sigma sigma for Laplacian
61  * @param size size of kernel
62  * @param scale scale factor
63  */
64 FilterLaplace::FilterLaplace(float sigma, unsigned int size, float scale)
65  : Filter("FilterLaplace")
66 {
67  kernel_size = size;
68  kernel = (int *)malloc( size * size * sizeof(int) );
69  calculate_kernel( kernel, sigma, size, scale );
70 #ifdef HAVE_OPENCV
71  kernel_float = (float *)malloc(size * size * sizeof(float));
72  for (unsigned int i = 0; i < size * size; ++i) {
73  kernel_float[i] = kernel[i];
74  }
75 #endif
76 }
77 
78 
79 /** Destructor. */
81 {
82  if ( kernel != NULL ) {
83  free( kernel );
84  }
85  if ( kernel_float != NULL ) {
86  free( kernel_float );
87  }
88 }
89 
90 
91 void
93 {
94 #if defined(HAVE_IPP)
95  IppiSize size;
96  size.width = src_roi[0]->width - kernel_size;
97  size.height = src_roi[0]->height - kernel_size;
98 
99  IppStatus status;
100 
101  if ( kernel == NULL ) {
102  // base + number of bytes to line y + pixel bytes
103  status = ippiFilterLaplace_8u_C1R( src[0] + (src_roi[0]->start.y * src_roi[0]->line_step) + (src_roi[0]->start.x * src_roi[0]->pixel_step), src_roi[0]->line_step,
105  size, ippMskSize5x5 );
106  } else {
107  IppiSize ksize = { kernel_size, kernel_size };
108  IppiPoint kanchor = { (kernel_size + 1) / 2, (kernel_size + 1) / 2 };
109 
110  /*
111  std::cout << "steps: " << src_roi[0]->line_step << " " << dst_roi->line_step << std::endl
112  << "ksize: " << ksize.width << " x " << ksize.height << std::endl
113  << "kanchor: " << kanchor.x << "," << kanchor.y << std::endl;
114  */
115 
116  status = ippiFilter_8u_C1R( src[0] + ((src_roi[0]->start.y + kernel_size / 2) * src_roi[0]->line_step) + ((src_roi[0]->start.x + kernel_size / 2) * src_roi[0]->pixel_step), src_roi[0]->line_step,
117  dst + ((dst_roi->start.y + kernel_size / 2) * dst_roi->line_step) + ((dst_roi->start.x + kernel_size / 2) * dst_roi->pixel_step), dst_roi->line_step,
118  size, kernel, ksize, kanchor, 1 );
119 
120  }
121 
122  if ( status != ippStsNoErr ) {
123  throw fawkes::Exception("Laplace filter failed with %i\n", status);
124  }
125 
126  /*
127  std::cout << "FilterLaplace: ippiFilterLaplace exit code: " << std::flush;
128  switch (status) {
129  case ippStsNoErr:
130  std::cout << "ippStsNoErr";
131  break;
132  case ippStsNullPtrErr:
133  std::cout << "ippStsNullPtrErr";
134  break;
135  case ippStsSizeErr:
136  std::cout << "ippStsSizeErr";
137  break;
138  case ippStsStepErr:
139  std::cout << "ippStsStepErr";
140  break;
141  case ippStsMaskSizeErr:
142  std::cout << "ippStsMaskSizeErr";
143  break;
144  default:
145  std::cout << "Unknown status " << status;
146  }
147  std::cout << std::endl;
148  */
149 #elif defined(HAVE_OPENCV)
150  if ((dst == NULL) || (dst == src[0])) {
151  throw fawkes::Exception("OpenCV-based Sobel filter cannot be in-place");
152  }
153 
154  cv::Mat srcm(src_roi[0]->height, src_roi[0]->width, CV_8UC1,
155  src[0] +
156  (src_roi[0]->start.y * src_roi[0]->line_step) +
157  (src_roi[0]->start.x * src_roi[0]->pixel_step),
158  src_roi[0]->line_step);
159 
160  cv::Mat dstm(dst_roi->height, dst_roi->width, CV_8UC1,
161  dst +
164  dst_roi->line_step);
165 
166  if ( kernel_float == NULL ) {
167  cv::Laplacian(srcm, dstm, /* ddepth */ CV_8UC1, /* ksize */ 5);
168  } else {
169  cv::Mat kernel(kernel_size, kernel_size, CV_32F, kernel_float);
170  cv::Point kanchor((kernel_size + 1) / 2, (kernel_size + 1) / 2);
171  cv::filter2D(srcm, dstm, /* ddepth */ -1, kernel, kanchor);
172  }
173 #endif
174 }
175 
176 
177 /** Calculate a Laplacian of Gaussian kernel.
178  * The kernel is calculated with the formula
179  * \f[
180  * roundf( \frac{-1}{\pi * \sigma^4} *
181  * ( 1 - \frac{w^2 + h^2}{2 * \sigma^2} )
182  * * e^{-\frac{w^2 + h^2}{2 * \sigma^2}} * \mathtt{scale} )
183  * \f]
184  *
185  * @param kernel buffer contains kernel upon return
186  * @param sigma sigma for formula
187  * @param size kernel is of quadratic size \f$\mathtt{size} \times \mathtt{size}\f$
188  * @param scale scale parameter in formula
189  */
190 void
191 FilterLaplace::calculate_kernel(int *kernel, float sigma, unsigned int size, float scale)
192 {
193  // title "LoGFUNC__________________________________________"
194 
195  /*
196  std::cout.precision( 5 );
197  std::cout.width( 10 );
198 
199  std::cout << "Discrete Laplacian kernel for sigma=" << sigma
200  << " quadratic size of " << size
201  << " scaled by " << scale << std::endl;
202  */
203  for (int h = (-(int)(size / 2)); h <= (int)((size - 1) / 2); ++h) {
204  for (int w = (-(int)(size / 2)); w <= (int)((size - 1) / 2); ++w) {
205  //float v = ( (w*w + h*h - 2 * sigma * sigma) / sigma * sigma * sigma * sigma )
206  //* exp( -( (w*w + h*h) / (2 * sigma * sigma) ));
207  int v = (int)roundf( - 1/( M_PI * sigma * sigma * sigma * sigma ) *
208  ( 1 - ( (w*w + h*h) / (2 * sigma * sigma) ) )
209  * exp( -( (w*w + h*h) / (2 * sigma * sigma) )) * scale );
210  // std::cout << " " << v << std::flush;
211  kernel[ (h + (size / 2)) * size + (w + (size / 2)) ] = v;
212  }
213  //std::cout << std::endl;
214  }
215 
216  /*
217  for (int h = 0; h < size; ++h) {
218  for (int w = 0; w < size; ++w) {
219  std::cout << " " << kernel[ h * size + w ] << std::flush;
220  }
221  std::cout << std::endl;
222  }
223  */
224 
225 }
226 
227 } // end namespace firevision
fawkes::point_t start
ROI start.
Definition: roi.h:118
unsigned int x
x coordinate
Definition: types.h:35
FilterLaplace()
Constructor.
Definition: laplace.cpp:51
unsigned int width
ROI width.
Definition: roi.h:120
~FilterLaplace()
Destructor.
Definition: laplace.cpp:80
unsigned char ** src
Source buffers, dynamically allocated by Filter ctor.
Definition: filter.h:65
Base class for exceptions in Fawkes.
Definition: exception.h:36
Filter interface.
Definition: filter.h:35
virtual void apply()
Apply the filter.
Definition: laplace.cpp:92
unsigned int y
y coordinate
Definition: types.h:36
ROI ** src_roi
Source ROIs, dynamically allocated by Filter ctor.
Definition: filter.h:70
static void calculate_kernel(int *kernel_buffer, float sigma, unsigned int size, float scale)
Calculate a Laplacian of Gaussian kernel.
Definition: laplace.cpp:191
unsigned int height
ROI height.
Definition: roi.h:122
unsigned int line_step
line step
Definition: roi.h:128
unsigned char * dst
Destination buffer.
Definition: filter.h:67
unsigned int pixel_step
pixel step
Definition: roi.h:130
ROI * dst_roi
Destination ROI.
Definition: filter.h:72