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 <fvfilters/morphology/dilation.h>
25 
26 #include <fvutils/color/yuv.h>
27 #include <core/exception.h>
28 
29 #include <cstddef>
30 
31 #ifdef HAVE_IPP
32 # include <ippi.h>
33 #elif defined(HAVE_OPENCV)
34 # if CV_MAJOR_VERSION < 2 || (CV_MAJOR_VERSION == 2 && CV_MINOR_VERSION < 4)
35 # include <opencv/cv.h>
36 # endif
37 # include <opencv/cv.hpp>
38 #else
39 # error "Neither IPP nor OpenCV available"
40 #endif
41 
42 namespace firevision {
43 #if 0 /* just to make Emacs auto-indent happy */
44 }
45 #endif
46 
47 /** @class FilterDilation <fvfilters/morphology/dilation.h>
48  * Morphological dilation.
49  *
50  * @author Tim Niemueller
51  */
52 
53 /** Constructor. */
55  : MorphologicalFilter("Morphological Dilation")
56 {
57 }
58 
59 
60 /** Constructor with parameters.
61  * @param se structuring element buffer. This is just a line-wise concatenated array
62  * of values. A value of zero means ignore, any other value means to consider this
63  * value.
64  * @param se_width width of structuring element
65  * @param se_height height of structuring element
66  * @param se_anchor_x x coordinate of anchor in structuring element
67  * @param se_anchor_y y coordinate of anchor in structuring element
68  */
70  unsigned int se_width, unsigned int se_height,
71  unsigned int se_anchor_x, unsigned int se_anchor_y)
72  : MorphologicalFilter("Morphological Dilation")
73 {
74  this->se = se;
75  this->se_width = se_width;
76  this->se_height = se_height;
77  this->se_anchor_x = se_anchor_x;
78  this->se_anchor_y = se_anchor_y;
79 }
80 
81 
82 void
84 {
85 #if defined(HAVE_IPP)
86  IppStatus status;
87 
88  if ( se == NULL ) {
89  // standard 3x3 dilation
90 
91  IppiSize size;
92  size.width = src_roi[0]->width - 2;
93  size.height = src_roi[0]->height - 2;
94 
95 
96  if ( (dst == NULL) || (dst == src[0]) ) {
97  // In-place
98 
99  // std::cout << "Running in-place with standard SE" << std::endl;
100 
101  status = ippiDilate3x3_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),
102  src_roi[0]->line_step,
103  size);
104 
105  } else {
106  // std::cout << "Running not in-place dilation with standard SE" << std::endl;
107 
108  status = ippiDilate3x3_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),
109  src_roi[0]->line_step,
110  dst + ((dst_roi->start.y + 1) * dst_roi->line_step) + ((dst_roi->start.x + 1) * dst_roi->pixel_step),
112  size);
113 
114  yuv422planar_copy_uv(src[0], dst,
115  src_roi[0]->image_width, src_roi[0]->image_height,
116  src_roi[0]->start.x, src_roi[0]->start.y,
117  src_roi[0]->width, src_roi[0]->height );
118  }
119  } else {
120  // we have a custom SE
121 
122  IppiSize size;
123  size.width = src_roi[0]->width - se_width;
124  size.height = src_roi[0]->height - se_width;
125 
126  IppiSize mask_size = { se_width, se_height };
127  IppiPoint mask_anchor = { se_anchor_x, se_anchor_y };
128 
129  /*
130  std::cout << "Dilation filter is running with the following parameters:" << std::endl
131  << " ROI size: " << size.width << " x " << size.height << std::endl
132  << " mask size: " << mask_size.width << " x " << mask_size.height << std::endl
133  << " mask anchor: (" << mask_anchor.x << "," << mask_anchor.y << ")" << std::endl
134  << std::endl;
135 
136  printf(" src buf: 0x%x\n", (unsigned int)src );
137  printf(" dst buf: 0x%x\n", (unsigned int)dst );
138  */
139 
140  if ( (dst == NULL) || (dst == src[0]) ) {
141  // In-place
142 
143  status = ippiDilate_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),
144  src_roi[0]->line_step,
145  size,
146  se, mask_size, mask_anchor);
147 
148  } else {
149  //std::cout << "Running NOT in-place" << std::endl;
150 
151  status = ippiDilate_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),
152  src_roi[0]->line_step,
153  dst + ((dst_roi->start.y + (se_height / 2)) * dst_roi->line_step) + ((dst_roi->start.x + (se_width / 2)) * dst_roi->pixel_step),
155  size,
156  se, mask_size, mask_anchor);
157 
158  yuv422planar_copy_uv(src[0], dst,
159  src_roi[0]->image_width, src_roi[0]->image_height,
160  src_roi[0]->start.x, src_roi[0]->start.y,
161  src_roi[0]->width, src_roi[0]->height );
162 
163  }
164  }
165 
166  if ( status != ippStsNoErr ) {
167  throw fawkes::Exception("Morphological dilation failed with %i\n", status);
168  }
169 #elif defined(HAVE_OPENCV)
170  cv::Mat srcm(src_roi[0]->height, src_roi[0]->width, CV_8UC1,
171  src[0] +
172  (src_roi[0]->start.y * src_roi[0]->line_step) +
173  (src_roi[0]->start.x * src_roi[0]->pixel_step),
174  src_roi[0]->line_step);
175 
176  if (dst == NULL) { dst = src[0]; dst_roi = src_roi[0]; }
177 
178  cv::Mat dstm(dst_roi->height, dst_roi->width, CV_8UC1,
179  dst +
182  dst_roi->line_step);
183 
184  if (se == NULL) {
185  cv::dilate(srcm, dstm, cv::Mat());
186  } else {
187  cv::Mat sem(se_width, se_height, CV_8UC1);
188  cv::Point sem_anchor(se_anchor_x, se_anchor_y);
189  cv::dilate(srcm, dstm, sem, sem_anchor);
190  }
191 #endif
192 
193 }
194 
195 } // 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.
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.
FilterDilation()
Constructor.
Definition: dilation.cpp:54
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
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
virtual void apply()
Apply the filter.
Definition: dilation.cpp:83
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