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