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 <core/exception.h>
24 #include <fvfilters/morphology/erosion.h>
25 #include <fvutils/color/yuv.h>
26 
27 #include <cstddef>
28 
29 #ifdef HAVE_IPP
30 # include <ippi.h>
31 #elif defined(HAVE_OPENCV)
32 # if CV_MAJOR_VERSION < 2 || (CV_MAJOR_VERSION == 2 && CV_MINOR_VERSION < 4)
33 # include <opencv/cv.h>
34 # endif
35 # include <opencv/cv.hpp>
36 #else
37 # error "Neither IPP nor OpenCV available"
38 #endif
39 
40 namespace firevision {
41 
42 /** @class FilterErosion <fvfilters/morphology/erosion.h>
43  * Morphological erosion.
44  *
45  * @author Tim Niemueller
46  */
47 
48 /** Constructor. */
50 {
51 }
52 
53 void
55 {
56 #if defined(HAVE_IPP)
57  IppStatus status;
58 
59  if (se == NULL) {
60  // standard 3x3 erosion
61 
62  IppiSize size;
63  size.width = src_roi[0]->width - 2;
64  size.height = src_roi[0]->height - 2;
65 
66  if ((dst == NULL) || (dst == src[0])) {
67  // In-place
68  status = ippiErode3x3_8u_C1IR(src[0] + ((src_roi[0]->start.y + 1) * src_roi[0]->line_step)
69  + ((src_roi[0]->start.x + 1) * src_roi[0]->pixel_step),
70  src_roi[0]->line_step,
71  size);
72 
73  } else {
74  status = ippiErode3x3_8u_C1R(src[0] + ((src_roi[0]->start.y + 1) * src_roi[0]->line_step)
75  + ((src_roi[0]->start.x + 1) * src_roi[0]->pixel_step),
76  src_roi[0]->line_step,
77  dst + ((dst_roi->start.y + 1) * dst_roi->line_step)
78  + ((dst_roi->start.x + 1) * dst_roi->pixel_step),
80  size);
81 
82  yuv422planar_copy_uv(src[0],
83  dst,
84  src_roi[0]->image_width,
85  src_roi[0]->image_height,
86  src_roi[0]->start.x,
87  src_roi[0]->start.y,
88  src_roi[0]->width,
89  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 =
104  ippiErode_8u_C1IR(src[0] + ((src_roi[0]->start.y + (se_height / 2)) * src_roi[0]->line_step)
105  + ((src_roi[0]->start.x + (se_width / 2)) * src_roi[0]->pixel_step),
106  src_roi[0]->line_step,
107  size,
108  se,
109  mask_size,
110  mask_anchor);
111 
112  //std::cout << "in-place operation ended with status " << status << std::endl;
113 
114  } else {
115  status =
116  ippiErode_8u_C1R(src[0] + ((src_roi[0]->start.y + (se_height / 2)) * src_roi[0]->line_step)
117  + ((src_roi[0]->start.x + (se_width / 2)) * src_roi[0]->pixel_step),
118  src_roi[0]->line_step,
119  dst + ((dst_roi->start.y + (se_height / 2)) * dst_roi->line_step)
120  + ((dst_roi->start.x + (se_width / 2)) * dst_roi->pixel_step),
122  size,
123  se,
124  mask_size,
125  mask_anchor);
126 
127  // std::cout << "NOT in-place operation ended with status " << status << std::endl;
128 
129  yuv422planar_copy_uv(src[0],
130  dst,
131  src_roi[0]->image_width,
132  src_roi[0]->image_height,
133  src_roi[0]->start.x,
134  src_roi[0]->start.y,
135  src_roi[0]->width,
136  src_roi[0]->height);
137  }
138  }
139 
140  if (status != ippStsNoErr) {
141  throw fawkes::Exception("Morphological erosion failed with %i\n", status);
142  }
143 #elif defined(HAVE_OPENCV)
144  cv::Mat srcm(src_roi[0]->height,
145  src_roi[0]->width,
146  CV_8UC1,
147  src[0] + (src_roi[0]->start.y * src_roi[0]->line_step)
148  + (src_roi[0]->start.x * src_roi[0]->pixel_step),
149  src_roi[0]->line_step);
150 
151  if (dst == NULL) {
152  dst = src[0];
153  dst_roi = src_roi[0];
154  }
155 
156  cv::Mat dstm(dst_roi->height,
157  dst_roi->width,
158  CV_8UC1,
160  + (dst_roi->start.x * dst_roi->pixel_step),
161  dst_roi->line_step);
162 
163  if (se == NULL) {
164  cv::erode(srcm, dstm, cv::Mat());
165  } else {
166  cv::Mat sem(se_width, se_height, CV_8UC1);
167  cv::Point sem_anchor(se_anchor_x, se_anchor_y);
168  cv::erode(srcm, dstm, sem, sem_anchor);
169  }
170 #endif
171 }
172 
173 } // 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:54
fawkes::upoint_t start
ROI start.
Definition: roi.h:115
unsigned int y
y coordinate
Definition: types.h:37
unsigned int x
x coordinate
Definition: types.h:36
unsigned int width
ROI width.
Definition: roi.h:117
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:61
Base class for exceptions in Fawkes.
Definition: exception.h:35
ROI ** src_roi
Source ROIs, dynamically allocated by Filter ctor.
Definition: filter.h:66
FilterErosion()
Constructor.
Definition: erosion.cpp:49
unsigned int height
ROI height.
Definition: roi.h:119
unsigned int se_width
Width of structuring element.
unsigned int line_step
line step
Definition: roi.h:125
unsigned char * dst
Destination buffer.
Definition: filter.h:63
unsigned int pixel_step
pixel step
Definition: roi.h:127
ROI * dst_roi
Destination ROI.
Definition: filter.h:68