Fawkes API  Fawkes Development Version
dilation.cpp
1 
2 /***************************************************************************
3  * dilation.cpp - implementation of morphological dilation filter
4  *
5  * Created: Thu May 25 15:47:01 2006
6  * Copyright 2005-2007 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <core/exception.h>
25 #include <fvfilters/morphology/dilation.h>
26 #include <fvutils/color/yuv.h>
27 
28 #include <cstddef>
29 
30 #ifdef HAVE_IPP
31 # include <ippi.h>
32 #elif defined(HAVE_OPENCV)
33 # include <opencv2/opencv.hpp>
34 #else
35 # error "Neither IPP nor OpenCV available"
36 #endif
37 
38 namespace firevision {
39 
40 /** @class FilterDilation <fvfilters/morphology/dilation.h>
41  * Morphological dilation.
42  *
43  * @author Tim Niemueller
44  */
45 
46 /** Constructor. */
48 {
49 }
50 
51 /** Constructor with parameters.
52  * @param se structuring element buffer. This is just a line-wise concatenated array
53  * of values. A value of zero means ignore, any other value means to consider this
54  * value.
55  * @param se_width width of structuring element
56  * @param se_height height of structuring element
57  * @param se_anchor_x x coordinate of anchor in structuring element
58  * @param se_anchor_y y coordinate of anchor in structuring element
59  */
61  unsigned int se_width,
62  unsigned int se_height,
63  unsigned int se_anchor_x,
64  unsigned int se_anchor_y)
65 : MorphologicalFilter("Morphological Dilation")
66 {
67  this->se = se;
68  this->se_width = se_width;
69  this->se_height = se_height;
70  this->se_anchor_x = se_anchor_x;
71  this->se_anchor_y = se_anchor_y;
72 }
73 
74 void
76 {
77 #if defined(HAVE_IPP)
78  IppStatus status;
79 
80  if (se == NULL) {
81  // standard 3x3 dilation
82 
83  IppiSize size;
84  size.width = src_roi[0]->width - 2;
85  size.height = src_roi[0]->height - 2;
86 
87  if ((dst == NULL) || (dst == src[0])) {
88  // In-place
89 
90  // std::cout << "Running in-place with standard SE" << std::endl;
91 
92  status = ippiDilate3x3_8u_C1IR(src[0] + ((src_roi[0]->start.y + 1) * src_roi[0]->line_step)
93  + ((src_roi[0]->start.x + 1) * src_roi[0]->pixel_step),
94  src_roi[0]->line_step,
95  size);
96 
97  } else {
98  // std::cout << "Running not in-place dilation with standard SE" << std::endl;
99 
100  status = ippiDilate3x3_8u_C1R(src[0] + ((src_roi[0]->start.y + 1) * src_roi[0]->line_step)
101  + ((src_roi[0]->start.x + 1) * src_roi[0]->pixel_step),
102  src_roi[0]->line_step,
103  dst + ((dst_roi->start.y + 1) * dst_roi->line_step)
104  + ((dst_roi->start.x + 1) * dst_roi->pixel_step),
106  size);
107 
108  yuv422planar_copy_uv(src[0],
109  dst,
110  src_roi[0]->image_width,
111  src_roi[0]->image_height,
112  src_roi[0]->start.x,
113  src_roi[0]->start.y,
114  src_roi[0]->width,
115  src_roi[0]->height);
116  }
117  } else {
118  // we have a custom SE
119 
120  IppiSize size;
121  size.width = src_roi[0]->width - se_width;
122  size.height = src_roi[0]->height - se_width;
123 
124  IppiSize mask_size = {se_width, se_height};
125  IppiPoint mask_anchor = {se_anchor_x, se_anchor_y};
126 
127  /*
128  std::cout << "Dilation filter is running with the following parameters:" << std::endl
129  << " ROI size: " << size.width << " x " << size.height << std::endl
130  << " mask size: " << mask_size.width << " x " << mask_size.height << std::endl
131  << " mask anchor: (" << mask_anchor.x << "," << mask_anchor.y << ")" << std::endl
132  << std::endl;
133 
134  printf(" src buf: 0x%x\n", (unsigned int)src );
135  printf(" dst buf: 0x%x\n", (unsigned int)dst );
136  */
137 
138  if ((dst == NULL) || (dst == src[0])) {
139  // In-place
140 
141  status =
142  ippiDilate_8u_C1IR(src[0]
143  + ((src_roi[0]->start.y + (se_height / 2)) * src_roi[0]->line_step)
144  + ((src_roi[0]->start.x + (se_width / 2)) * src_roi[0]->pixel_step),
145  src_roi[0]->line_step,
146  size,
147  se,
148  mask_size,
149  mask_anchor);
150 
151  } else {
152  //std::cout << "Running NOT in-place" << std::endl;
153 
154  status =
155  ippiDilate_8u_C1R(src[0] + ((src_roi[0]->start.y + (se_height / 2)) * src_roi[0]->line_step)
156  + ((src_roi[0]->start.x + (se_width / 2)) * src_roi[0]->pixel_step),
157  src_roi[0]->line_step,
158  dst + ((dst_roi->start.y + (se_height / 2)) * dst_roi->line_step)
159  + ((dst_roi->start.x + (se_width / 2)) * dst_roi->pixel_step),
161  size,
162  se,
163  mask_size,
164  mask_anchor);
165 
166  yuv422planar_copy_uv(src[0],
167  dst,
168  src_roi[0]->image_width,
169  src_roi[0]->image_height,
170  src_roi[0]->start.x,
171  src_roi[0]->start.y,
172  src_roi[0]->width,
173  src_roi[0]->height);
174  }
175  }
176 
177  if (status != ippStsNoErr) {
178  throw fawkes::Exception("Morphological dilation failed with %i\n", status);
179  }
180 #elif defined(HAVE_OPENCV)
181  cv::Mat srcm(src_roi[0]->height,
182  src_roi[0]->width,
183  CV_8UC1,
184  src[0] + (src_roi[0]->start.y * src_roi[0]->line_step)
185  + (src_roi[0]->start.x * src_roi[0]->pixel_step),
186  src_roi[0]->line_step);
187 
188  if (dst == NULL) {
189  dst = src[0];
190  dst_roi = src_roi[0];
191  }
192 
193  cv::Mat dstm(dst_roi->height,
194  dst_roi->width,
195  CV_8UC1,
197  + (dst_roi->start.x * dst_roi->pixel_step),
198  dst_roi->line_step);
199 
200  if (se == NULL) {
201  cv::dilate(srcm, dstm, cv::Mat());
202  } else {
203  cv::Mat sem(se_width, se_height, CV_8UC1);
204  cv::Point sem_anchor(se_anchor_x, se_anchor_y);
205  cv::dilate(srcm, dstm, sem, sem_anchor);
206  }
207 #endif
208 }
209 
210 } // end namespace firevision
Base class for exceptions in Fawkes.
Definition: exception.h:36
virtual void apply()
Apply the filter.
Definition: dilation.cpp:75
FilterDilation()
Constructor.
Definition: dilation.cpp:47
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