Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
erosion.cpp
1 
2 /***************************************************************************
3  * erosion.cpp - implementation of morphological erosion filter
4  *
5  * Created: Fri May 26 12:13:22 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/morphology/erosion.h>
24 
25 #include <fvutils/color/yuv.h>
26 #include <core/exception.h>
27 
28 #include <cstddef>
29 
30 #ifdef HAVE_IPP
31 # include <ippi.h>
32 #elif defined(HAVE_OPENCV)
33 # include <cv.h>
34 #else
35 # error "Neither IPP nor OpenCV available"
36 #endif
37 
38 namespace firevision {
39 #if 0 /* just to make Emacs auto-indent happy */
40 }
41 #endif
42 
43 /** @class FilterErosion <fvfilters/morphology/erosion.h>
44  * Morphological erosion.
45  *
46  * @author Tim Niemueller
47  */
48 
49 /** Constructor. */
50 FilterErosion::FilterErosion()
51  : MorphologicalFilter("Morphological Erosion")
52 {
53 }
54 
55 
56 void
58 {
59 #if defined(HAVE_IPP)
60  IppStatus status;
61 
62  if ( se == NULL ) {
63  // standard 3x3 erosion
64 
65  IppiSize size;
66  size.width = src_roi[0]->width - 2;
67  size.height = src_roi[0]->height - 2;
68 
69 
70  if ( (dst == NULL) || (dst == src[0]) ) {
71  // In-place
72  status = ippiErode3x3_8u_C1IR(src[0] + ((src_roi[0]->start.y + 1) * src_roi[0]->line_step) + ((src_roi[0]->start.x + 1) * src_roi[0]->pixel_step),
73  src_roi[0]->line_step,
74  size);
75 
76  } else {
77  status = ippiErode3x3_8u_C1R(src[0] + ((src_roi[0]->start.y + 1) * src_roi[0]->line_step) + ((src_roi[0]->start.x + 1) * src_roi[0]->pixel_step),
78  src_roi[0]->line_step,
79  dst + ((dst_roi->start.y + 1) * dst_roi->line_step) + ((dst_roi->start.x + 1) * dst_roi->pixel_step),
81  size);
82 
83  yuv422planar_copy_uv(src[0], dst,
84  src_roi[0]->image_width, src_roi[0]->image_height,
85  src_roi[0]->start.x, src_roi[0]->start.y,
86  src_roi[0]->width, src_roi[0]->height );
87  }
88  } else {
89  // we have a custom SE
90 
91  IppiSize size;
92  size.width = src_roi[0]->width - se_width;
93  size.height = src_roi[0]->height - se_height;
94 
95  IppiSize mask_size = { se_width, se_height };
96  IppiPoint mask_anchor = { se_anchor_x, se_anchor_y };
97 
98  if ( (dst == NULL) || (dst == src[0]) ) {
99  // In-place
100  status = ippiErode_8u_C1IR(src[0] + ((src_roi[0]->start.y + (se_height / 2)) * src_roi[0]->line_step) + ((src_roi[0]->start.x + (se_width / 2)) * src_roi[0]->pixel_step),
101  src_roi[0]->line_step,
102  size,
103  se, mask_size, mask_anchor);
104 
105  //std::cout << "in-place operation ended with status " << status << std::endl;
106 
107  } else {
108  status = ippiErode_8u_C1R(src[0] + ((src_roi[0]->start.y + (se_height / 2)) * src_roi[0]->line_step) + ((src_roi[0]->start.x + (se_width / 2)) * src_roi[0]->pixel_step),
109  src_roi[0]->line_step,
110  dst + ((dst_roi->start.y + (se_height / 2)) * dst_roi->line_step) + ((dst_roi->start.x + (se_width / 2)) * dst_roi->pixel_step),
112  size,
113  se, mask_size, mask_anchor);
114 
115  // std::cout << "NOT in-place operation ended with status " << status << std::endl;
116 
117  yuv422planar_copy_uv(src[0], dst,
118  src_roi[0]->image_width, src_roi[0]->image_height,
119  src_roi[0]->start.x, src_roi[0]->start.y,
120  src_roi[0]->width, src_roi[0]->height );
121  }
122 
123  }
124 
125  if ( status != ippStsNoErr ) {
126  throw fawkes::Exception("Morphological erosion failed with %i\n", status);
127  }
128 #elif defined(HAVE_OPENCV)
129  cv::Mat srcm(src_roi[0]->height, src_roi[0]->width, CV_8UC1,
130  src[0] +
131  (src_roi[0]->start.y * src_roi[0]->line_step) +
132  (src_roi[0]->start.x * src_roi[0]->pixel_step),
133  src_roi[0]->line_step);
134 
135  if (dst == NULL) { dst = src[0]; dst_roi = src_roi[0]; }
136 
137  cv::Mat dstm(dst_roi->height, dst_roi->width, CV_8UC1,
138  dst +
141  dst_roi->line_step);
142 
143  if (se == NULL) {
144  cv::erode(srcm, dstm, cv::Mat());
145  } else {
146  cv::Mat sem(se_width, se_height, CV_8UC1);
147  cv::Point sem_anchor(se_anchor_x, se_anchor_y);
148  cv::erode(srcm, dstm, sem, sem_anchor);
149  }
150 #endif
151 }
152 
153 } // end namespace firevision
unsigned int se_anchor_y
Anchor point y offset of structuring element.
unsigned int se_anchor_x
Anchor point x offset of structuring element.
virtual void apply()
Apply the filter.
Definition: erosion.cpp:57
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
unsigned char * se
Structuring element.
unsigned int se_height
Height of structuring element.
Morphological filter interface.
unsigned char ** src
Source buffers, dynamically allocated by Filter ctor.
Definition: filter.h:65
Base class for exceptions in Fawkes.
Definition: exception.h:36
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
unsigned int se_width
Width of structuring element.
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