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 <core/exception.h>
24 #include <fvfilters/median.h>
25 
26 #ifdef HAVE_IPP
27 # include <ippi.h>
28 #elif defined(HAVE_OPENCV)
29 # if CV_MAJOR_VERSION < 2 || (CV_MAJOR_VERSION == 2 && CV_MINOR_VERSION < 4)
30 # include <opencv/cv.h>
31 # endif
32 # include <opencv/cv.hpp>
33 #else
34 # error "Neither IPP nor OpenCV available"
35 #endif
36 
37 namespace firevision {
38 
39 /** @class FilterMedian <fvfilters/median.h>
40  * Median filter.
41  * @author Tim Niemueller
42  */
43 
44 /** Constructor.
45  * @param mask_size size of median mask
46  */
47 FilterMedian::FilterMedian(unsigned int mask_size) : Filter("FilterMedian")
48 {
49  this->mask_size = mask_size;
50 }
51 
52 void
54 {
55 #if defined(HAVE_IPP)
56  IppiSize size;
57  size.width = src_roi[0]->width - mask_size;
58  size.height = src_roi[0]->height - mask_size;
59 
60  IppiSize mask = {mask_size, mask_size};
61  IppiPoint anchor = {(mask_size + 1) / 2, (mask_size + 1) / 2};
62 
63  IppStatus status;
64 
65  // base + number of bytes to line y + pixel bytes
66  status = ippiFilterMedian_8u_C1R(
67  src[0] + ((src_roi[0]->start.y + (mask_size + 1) / 2) * src_roi[0]->line_step)
68  + ((src_roi[0]->start.x + (mask_size + 1) / 2) * src_roi[0]->pixel_step),
69  src_roi[0]->line_step,
70  dst + ((dst_roi->start.y + (mask_size + 1) / 2) * dst_roi->line_step)
71  + ((dst_roi->start.x + (mask_size + 1) / 2) * dst_roi->pixel_step),
73  size,
74  mask,
75  anchor);
76 
77  if (status != ippStsNoErr) {
78  throw fawkes::Exception("Median filter failed with %i\n", status);
79  }
80 #elif defined(HAVE_OPENCV)
81  cv::Mat srcm(src_roi[0]->height,
82  src_roi[0]->width,
83  CV_8UC1,
84  src[0] + (src_roi[0]->start.y * src_roi[0]->line_step)
85  + (src_roi[0]->start.x * src_roi[0]->pixel_step),
86  src_roi[0]->line_step);
87 
88  if (dst == NULL) {
89  dst = src[0];
90  dst_roi = src_roi[0];
91  }
92 
93  cv::Mat dstm(dst_roi->height,
94  dst_roi->width,
95  CV_8UC1,
99 
100  cv::medianBlur(srcm, dstm, mask_size);
101 #endif
102 }
103 
104 } // end namespace firevision
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual void apply()
Apply the filter.
Definition: median.cpp:53
FilterMedian(unsigned int mask_size)
Constructor.
Definition: median.cpp:47
Filter interface.
Definition: filter.h:33
ROI ** src_roi
Source ROIs, dynamically allocated by Filter ctor.
Definition: filter.h:66
unsigned char ** src
Source buffers, dynamically allocated by Filter ctor.
Definition: filter.h:61
unsigned char * dst
Destination buffer.
Definition: filter.h:63
ROI * dst_roi
Destination ROI.
Definition: filter.h:68
unsigned int height
ROI height.
Definition: roi.h:119
fawkes::upoint_t start
ROI start.
Definition: roi.h:115
unsigned int line_step
line step
Definition: roi.h:125
unsigned int width
ROI width.
Definition: roi.h:117
unsigned int pixel_step
pixel step
Definition: roi.h:127
unsigned int x
x coordinate
Definition: types.h:36
unsigned int y
y coordinate
Definition: types.h:37