Fawkes API  Fawkes Development Version
median.cpp
1 
2 /***************************************************************************
3  * median.cpp - Implementation of a median filter
4  *
5  * Created: Mon Jun 05 15:02:36 2006
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/median.h>
24 
25 #include <core/exception.h>
26 
27 #ifdef HAVE_IPP
28 # include <ippi.h>
29 #elif defined(HAVE_OPENCV)
30 # include <cv.h>
31 # include <opencv2/imgproc.hpp>
32 #else
33 # error "Neither IPP nor OpenCV available"
34 #endif
35 
36 namespace firevision {
37 #if 0 /* just to make Emacs auto-indent happy */
38 }
39 #endif
40 
41 /** @class FilterMedian <fvfilters/median.h>
42  * Median filter.
43  * @author Tim Niemueller
44  */
45 
46 /** Constructor.
47  * @param mask_size size of median mask
48  */
49 FilterMedian::FilterMedian(unsigned int mask_size)
50  : Filter("FilterMedian")
51 {
52  this->mask_size = mask_size;
53 }
54 
55 
56 void
58 {
59 #if defined(HAVE_IPP)
60  IppiSize size;
61  size.width = src_roi[0]->width - mask_size;
62  size.height = src_roi[0]->height - mask_size;
63 
64  IppiSize mask = { mask_size, mask_size };
65  IppiPoint anchor = { (mask_size + 1) / 2, (mask_size + 1) / 2 };
66 
67  IppStatus status;
68 
69  // base + number of bytes to line y + pixel bytes
70  status = ippiFilterMedian_8u_C1R( src[0] + ((src_roi[0]->start.y + (mask_size + 1) / 2) * src_roi[0]->line_step) + ((src_roi[0]->start.x + ( mask_size + 1) / 2) * src_roi[0]->pixel_step), src_roi[0]->line_step,
71  dst + ((dst_roi->start.y + (mask_size + 1) / 2) * dst_roi->line_step) + ((dst_roi->start.x + ( mask_size + 1) / 2) * dst_roi->pixel_step), dst_roi->line_step,
72  size, mask, anchor );
73 
74  if ( status != ippStsNoErr ) {
75  throw fawkes::Exception("Median filter failed with %i\n", status);
76  }
77 #elif defined(HAVE_OPENCV)
78  cv::Mat srcm(src_roi[0]->height, src_roi[0]->width, CV_8UC1,
79  src[0] +
80  (src_roi[0]->start.y * src_roi[0]->line_step) +
81  (src_roi[0]->start.x * src_roi[0]->pixel_step),
82  src_roi[0]->line_step);
83 
84  if (dst == NULL) { dst = src[0]; dst_roi = src_roi[0]; }
85 
86  cv::Mat dstm(dst_roi->height, dst_roi->width, CV_8UC1,
87  dst +
91 
92  cv::medianBlur(srcm, dstm, mask_size);
93 #endif
94 }
95 
96 } // end namespace firevision
fawkes::point_t start
ROI start.
Definition: roi.h:118
unsigned int x
x coordinate
Definition: types.h:35
unsigned int width
ROI width.
Definition: roi.h:120
virtual void apply()
Apply the filter.
Definition: median.cpp:57
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
unsigned int y
y coordinate
Definition: types.h:36
ROI ** src_roi
Source ROIs, dynamically allocated by Filter ctor.
Definition: filter.h:70
unsigned int height
ROI height.
Definition: roi.h:122
FilterMedian(unsigned int mask_size)
Constructor.
Definition: median.cpp:49
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