Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 #else
32 # error "Neither IPP nor OpenCV available"
33 #endif
34 
35 namespace firevision {
36 #if 0 /* just to make Emacs auto-indent happy */
37 }
38 #endif
39 
40 /** @class FilterMedian <fvfilters/median.h>
41  * Median filter.
42  * @author Tim Niemueller
43  */
44 
45 /** Constructor.
46  * @param mask_size size of median mask
47  */
48 FilterMedian::FilterMedian(unsigned int mask_size)
49  : Filter("FilterMedian")
50 {
51  this->mask_size = mask_size;
52 }
53 
54 
55 void
57 {
58 #if defined(HAVE_IPP)
59  IppiSize size;
60  size.width = src_roi[0]->width - mask_size;
61  size.height = src_roi[0]->height - mask_size;
62 
63  IppiSize mask = { mask_size, mask_size };
64  IppiPoint anchor = { (mask_size + 1) / 2, (mask_size + 1) / 2 };
65 
66  IppStatus status;
67 
68  // base + number of bytes to line y + pixel bytes
69  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,
70  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,
71  size, mask, anchor );
72 
73  if ( status != ippStsNoErr ) {
74  throw fawkes::Exception("Median filter failed with %i\n", status);
75  }
76 #elif defined(HAVE_OPENCV)
77  cv::Mat srcm(src_roi[0]->height, src_roi[0]->width, CV_8UC1,
78  src[0] +
79  (src_roi[0]->start.y * src_roi[0]->line_step) +
80  (src_roi[0]->start.x * src_roi[0]->pixel_step),
81  src_roi[0]->line_step);
82 
83  if (dst == NULL) { dst = src[0]; dst_roi = src_roi[0]; }
84 
85  cv::Mat dstm(dst_roi->height, dst_roi->width, CV_8UC1,
86  dst +
90 
91  cv::medianBlur(srcm, dstm, mask_size);
92 #endif
93 }
94 
95 } // end namespace firevision