Fawkes API  Fawkes Development Version
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 # 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 FilterErosion <fvfilters/morphology/erosion.h>
45  * Morphological erosion.
46  *
47  * @author Tim Niemueller
48  */
49 
50 /** Constructor. */
52  : MorphologicalFilter("Morphological Erosion")
53 {
54 }
55 
56 
57 void
59 {
60 #if defined(HAVE_IPP)
61  IppStatus status;
62 
63  if ( se == NULL ) {
64  // standard 3x3 erosion
65 
66  IppiSize size;
67  size.width = src_roi[0]->width - 2;
68  size.height = src_roi[0]->height - 2;
69 
70 
71  if ( (dst == NULL) || (dst == src[0]) ) {
72  // In-place
73  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),
74  src_roi[0]->line_step,
75  size);
76 
77  } else {
78  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),
79  src_roi[0]->line_step,
80  dst + ((dst_roi->start.y + 1) * dst_roi->line_step) + ((dst_roi->start.x + 1) * dst_roi->pixel_step),
82  size);
83 
84  yuv422planar_copy_uv(src[0], dst,
85  src_roi[0]->image_width, src_roi[0]->image_height,
86  src_roi[0]->start.x, src_roi[0]->start.y,
87  src_roi[0]->width, src_roi[0]->height );
88  }
89  } else {
90  // we have a custom SE
91 
92  IppiSize size;
93  size.width = src_roi[0]->width - se_width;
94  size.height = src_roi[0]->height - se_height;
95 
96  IppiSize mask_size = { se_width, se_height };
97  IppiPoint mask_anchor = { se_anchor_x, se_anchor_y };
98 
99  if ( (dst == NULL) || (dst == src[0]) ) {
100  // In-place
101  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),
102  src_roi[0]->line_step,
103  size,
104  se, mask_size, mask_anchor);
105 
106  //std::cout << "in-place operation ended with status " << status << std::endl;
107 
108  } else {
109  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),
110  src_roi[0]->line_step,
111  dst + ((dst_roi->start.y + (se_height / 2)) * dst_roi->line_step) + ((dst_roi->start.x + (se_width / 2)) * dst_roi->pixel_step),
113  size,
114  se, mask_size, mask_anchor);
115 
116  // std::cout << "NOT in-place operation ended with status " << status << std::endl;
117 
118  yuv422planar_copy_uv(src[0], dst,
119  src_roi[0]->image_width, src_roi[0]->image_height,
120  src_roi[0]->start.x, src_roi[0]->start.y,
121  src_roi[0]->width, src_roi[0]->height );
122  }
123 
124  }
125 
126  if ( status != ippStsNoErr ) {
127  throw fawkes::Exception("Morphological erosion failed with %i\n", status);
128  }
129 #elif defined(HAVE_OPENCV)
130  cv::Mat srcm(src_roi[0]->height, src_roi[0]->width, CV_8UC1,
131  src[0] +
132  (src_roi[0]->start.y * src_roi[0]->line_step) +
133  (src_roi[0]->start.x * src_roi[0]->pixel_step),
134  src_roi[0]->line_step);
135 
136  if (dst == NULL) { dst = src[0]; dst_roi = src_roi[0]; }
137 
138  cv::Mat dstm(dst_roi->height, dst_roi->width, CV_8UC1,
139  dst +
142  dst_roi->line_step);
143 
144  if (se == NULL) {
145  cv::erode(srcm, dstm, cv::Mat());
146  } else {
147  cv::Mat sem(se_width, se_height, CV_8UC1);
148  cv::Point sem_anchor(se_anchor_x, se_anchor_y);
149  cv::erode(srcm, dstm, sem, sem_anchor);
150  }
151 #endif
152 }
153 
154 } // 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:58
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
FilterErosion()
Constructor.
Definition: erosion.cpp:51
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