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 # if CV_MAJOR_VERSION < 2 || (CV_MAJOR_VERSION == 2 && CV_MINOR_VERSION < 4)
34 # include <opencv/cv.h>
35 # endif
36 # include <opencv/cv.hpp>
37 #else
38 # error "Neither IPP nor OpenCV available"
39 #endif
40 
41 namespace firevision {
42 #if 0 /* just to make Emacs auto-indent happy */
43 }
44 #endif
45 
46 /** @class FilterErosion <fvfilters/morphology/erosion.h>
47  * Morphological erosion.
48  *
49  * @author Tim Niemueller
50  */
51 
52 /** Constructor. */
54  : MorphologicalFilter("Morphological Erosion")
55 {
56 }
57 
58 
59 void
61 {
62 #if defined(HAVE_IPP)
63  IppStatus status;
64 
65  if ( se == NULL ) {
66  // standard 3x3 erosion
67 
68  IppiSize size;
69  size.width = src_roi[0]->width - 2;
70  size.height = src_roi[0]->height - 2;
71 
72 
73  if ( (dst == NULL) || (dst == src[0]) ) {
74  // In-place
75  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),
76  src_roi[0]->line_step,
77  size);
78 
79  } else {
80  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),
81  src_roi[0]->line_step,
82  dst + ((dst_roi->start.y + 1) * dst_roi->line_step) + ((dst_roi->start.x + 1) * dst_roi->pixel_step),
84  size);
85 
86  yuv422planar_copy_uv(src[0], dst,
87  src_roi[0]->image_width, src_roi[0]->image_height,
88  src_roi[0]->start.x, src_roi[0]->start.y,
89  src_roi[0]->width, src_roi[0]->height );
90  }
91  } else {
92  // we have a custom SE
93 
94  IppiSize size;
95  size.width = src_roi[0]->width - se_width;
96  size.height = src_roi[0]->height - se_height;
97 
98  IppiSize mask_size = { se_width, se_height };
99  IppiPoint mask_anchor = { se_anchor_x, se_anchor_y };
100 
101  if ( (dst == NULL) || (dst == src[0]) ) {
102  // In-place
103  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),
104  src_roi[0]->line_step,
105  size,
106  se, mask_size, mask_anchor);
107 
108  //std::cout << "in-place operation ended with status " << status << std::endl;
109 
110  } else {
111  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),
112  src_roi[0]->line_step,
113  dst + ((dst_roi->start.y + (se_height / 2)) * dst_roi->line_step) + ((dst_roi->start.x + (se_width / 2)) * dst_roi->pixel_step),
115  size,
116  se, mask_size, mask_anchor);
117 
118  // std::cout << "NOT in-place operation ended with status " << status << std::endl;
119 
120  yuv422planar_copy_uv(src[0], dst,
121  src_roi[0]->image_width, src_roi[0]->image_height,
122  src_roi[0]->start.x, src_roi[0]->start.y,
123  src_roi[0]->width, src_roi[0]->height );
124  }
125 
126  }
127 
128  if ( status != ippStsNoErr ) {
129  throw fawkes::Exception("Morphological erosion failed with %i\n", status);
130  }
131 #elif defined(HAVE_OPENCV)
132  cv::Mat srcm(src_roi[0]->height, src_roi[0]->width, CV_8UC1,
133  src[0] +
134  (src_roi[0]->start.y * src_roi[0]->line_step) +
135  (src_roi[0]->start.x * src_roi[0]->pixel_step),
136  src_roi[0]->line_step);
137 
138  if (dst == NULL) { dst = src[0]; dst_roi = src_roi[0]; }
139 
140  cv::Mat dstm(dst_roi->height, dst_roi->width, CV_8UC1,
141  dst +
144  dst_roi->line_step);
145 
146  if (se == NULL) {
147  cv::erode(srcm, dstm, cv::Mat());
148  } else {
149  cv::Mat sem(se_width, se_height, CV_8UC1);
150  cv::Point sem_anchor(se_anchor_x, se_anchor_y);
151  cv::erode(srcm, dstm, sem, sem_anchor);
152  }
153 #endif
154 }
155 
156 } // 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:60
fawkes::upoint_t start
ROI start.
Definition: roi.h:119
unsigned int y
y coordinate
Definition: types.h:36
unsigned int x
x coordinate
Definition: types.h:35
unsigned int width
ROI width.
Definition: roi.h:121
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
ROI ** src_roi
Source ROIs, dynamically allocated by Filter ctor.
Definition: filter.h:70
FilterErosion()
Constructor.
Definition: erosion.cpp:53
unsigned int height
ROI height.
Definition: roi.h:123
unsigned int se_width
Width of structuring element.
unsigned int line_step
line step
Definition: roi.h:129
unsigned char * dst
Destination buffer.
Definition: filter.h:67
unsigned int pixel_step
pixel step
Definition: roi.h:131
ROI * dst_roi
Destination ROI.
Definition: filter.h:72