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 # include <opencv2/opencv.hpp>
33 #else
34 # error "Neither IPP nor OpenCV available"
35 #endif
36 
37 namespace firevision {
38 
39 /** @class FilterErosion <fvfilters/morphology/erosion.h>
40  * Morphological erosion.
41  *
42  * @author Tim Niemueller
43  */
44 
45 /** Constructor. */
47 {
48 }
49 
50 void
52 {
53 #if defined(HAVE_IPP)
54  IppStatus status;
55 
56  if (se == NULL) {
57  // standard 3x3 erosion
58 
59  IppiSize size;
60  size.width = src_roi[0]->width - 2;
61  size.height = src_roi[0]->height - 2;
62 
63  if ((dst == NULL) || (dst == src[0])) {
64  // In-place
65  status = ippiErode3x3_8u_C1IR(src[0] + ((src_roi[0]->start.y + 1) * src_roi[0]->line_step)
66  + ((src_roi[0]->start.x + 1) * src_roi[0]->pixel_step),
67  src_roi[0]->line_step,
68  size);
69 
70  } else {
71  status = ippiErode3x3_8u_C1R(src[0] + ((src_roi[0]->start.y + 1) * src_roi[0]->line_step)
72  + ((src_roi[0]->start.x + 1) * src_roi[0]->pixel_step),
73  src_roi[0]->line_step,
74  dst + ((dst_roi->start.y + 1) * dst_roi->line_step)
75  + ((dst_roi->start.x + 1) * dst_roi->pixel_step),
77  size);
78 
79  yuv422planar_copy_uv(src[0],
80  dst,
81  src_roi[0]->image_width,
82  src_roi[0]->image_height,
83  src_roi[0]->start.x,
84  src_roi[0]->start.y,
85  src_roi[0]->width,
86  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 =
101  ippiErode_8u_C1IR(src[0] + ((src_roi[0]->start.y + (se_height / 2)) * src_roi[0]->line_step)
102  + ((src_roi[0]->start.x + (se_width / 2)) * src_roi[0]->pixel_step),
103  src_roi[0]->line_step,
104  size,
105  se,
106  mask_size,
107  mask_anchor);
108 
109  //std::cout << "in-place operation ended with status " << status << std::endl;
110 
111  } else {
112  status =
113  ippiErode_8u_C1R(src[0] + ((src_roi[0]->start.y + (se_height / 2)) * src_roi[0]->line_step)
114  + ((src_roi[0]->start.x + (se_width / 2)) * src_roi[0]->pixel_step),
115  src_roi[0]->line_step,
116  dst + ((dst_roi->start.y + (se_height / 2)) * dst_roi->line_step)
117  + ((dst_roi->start.x + (se_width / 2)) * dst_roi->pixel_step),
119  size,
120  se,
121  mask_size,
122  mask_anchor);
123 
124  // std::cout << "NOT in-place operation ended with status " << status << std::endl;
125 
126  yuv422planar_copy_uv(src[0],
127  dst,
128  src_roi[0]->image_width,
129  src_roi[0]->image_height,
130  src_roi[0]->start.x,
131  src_roi[0]->start.y,
132  src_roi[0]->width,
133  src_roi[0]->height);
134  }
135  }
136 
137  if (status != ippStsNoErr) {
138  throw fawkes::Exception("Morphological erosion failed with %i\n", status);
139  }
140 #elif defined(HAVE_OPENCV)
141  cv::Mat srcm(src_roi[0]->height,
142  src_roi[0]->width,
143  CV_8UC1,
144  src[0] + (src_roi[0]->start.y * src_roi[0]->line_step)
145  + (src_roi[0]->start.x * src_roi[0]->pixel_step),
146  src_roi[0]->line_step);
147 
148  if (dst == NULL) {
149  dst = src[0];
150  dst_roi = src_roi[0];
151  }
152 
153  cv::Mat dstm(dst_roi->height,
154  dst_roi->width,
155  CV_8UC1,
157  + (dst_roi->start.x * dst_roi->pixel_step),
158  dst_roi->line_step);
159 
160  if (se == NULL) {
161  cv::erode(srcm, dstm, cv::Mat());
162  } else {
163  cv::Mat sem(se_width, se_height, CV_8UC1);
164  cv::Point sem_anchor(se_anchor_x, se_anchor_y);
165  cv::erode(srcm, dstm, sem, sem_anchor);
166  }
167 #endif
168 }
169 
170 } // end namespace firevision
Base class for exceptions in Fawkes.
Definition: exception.h:36
FilterErosion()
Constructor.
Definition: erosion.cpp:46
virtual void apply()
Apply the filter.
Definition: erosion.cpp:51
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
Morphological filter interface.
unsigned int se_height
Height of structuring element.
unsigned int se_width
Width of structuring element.
unsigned int se_anchor_y
Anchor point y offset of structuring element.
unsigned char * se
Structuring element.
unsigned int se_anchor_x
Anchor point x offset of structuring element.
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