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 # include <opencv2/imgproc.hpp>
36 #else
37 # error "Neither IPP nor OpenCV available"
38 #endif
39 
40 namespace firevision {
41 #if 0 /* just to make Emacs auto-indent happy */
42 }
43 #endif
44 
45 /** @class FilterDilation <fvfilters/morphology/dilation.h>
46  * Morphological dilation.
47  *
48  * @author Tim Niemueller
49  */
50 
51 /** Constructor. */
53  : MorphologicalFilter("Morphological Dilation")
54 {
55 }
56 
57 
58 /** Constructor with parameters.
59  * @param se structuring element buffer. This is just a line-wise concatenated array
60  * of values. A value of zero means ignore, any other value means to consider this
61  * value.
62  * @param se_width width of structuring element
63  * @param se_height height of structuring element
64  * @param se_anchor_x x coordinate of anchor in structuring element
65  * @param se_anchor_y y coordinate of anchor in structuring element
66  */
68  unsigned int se_width, unsigned int se_height,
69  unsigned int se_anchor_x, unsigned int se_anchor_y)
70  : MorphologicalFilter("Morphological Dilation")
71 {
72  this->se = se;
73  this->se_width = se_width;
74  this->se_height = se_height;
75  this->se_anchor_x = se_anchor_x;
76  this->se_anchor_y = se_anchor_y;
77 }
78 
79 
80 void
82 {
83 #if defined(HAVE_IPP)
84  IppStatus status;
85 
86  if ( se == NULL ) {
87  // standard 3x3 dilation
88 
89  IppiSize size;
90  size.width = src_roi[0]->width - 2;
91  size.height = src_roi[0]->height - 2;
92 
93 
94  if ( (dst == NULL) || (dst == src[0]) ) {
95  // In-place
96 
97  // std::cout << "Running in-place with standard SE" << std::endl;
98 
99  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),
100  src_roi[0]->line_step,
101  size);
102 
103  } else {
104  // std::cout << "Running not in-place dilation with standard SE" << std::endl;
105 
106  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),
107  src_roi[0]->line_step,
108  dst + ((dst_roi->start.y + 1) * dst_roi->line_step) + ((dst_roi->start.x + 1) * dst_roi->pixel_step),
110  size);
111 
112  yuv422planar_copy_uv(src[0], dst,
113  src_roi[0]->image_width, src_roi[0]->image_height,
114  src_roi[0]->start.x, src_roi[0]->start.y,
115  src_roi[0]->width, 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 = 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),
142  src_roi[0]->line_step,
143  size,
144  se, mask_size, mask_anchor);
145 
146  } else {
147  //std::cout << "Running NOT in-place" << std::endl;
148 
149  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),
150  src_roi[0]->line_step,
151  dst + ((dst_roi->start.y + (se_height / 2)) * dst_roi->line_step) + ((dst_roi->start.x + (se_width / 2)) * dst_roi->pixel_step),
153  size,
154  se, mask_size, mask_anchor);
155 
156  yuv422planar_copy_uv(src[0], dst,
157  src_roi[0]->image_width, src_roi[0]->image_height,
158  src_roi[0]->start.x, src_roi[0]->start.y,
159  src_roi[0]->width, src_roi[0]->height );
160 
161  }
162  }
163 
164  if ( status != ippStsNoErr ) {
165  throw fawkes::Exception("Morphological dilation failed with %i\n", status);
166  }
167 #elif defined(HAVE_OPENCV)
168  cv::Mat srcm(src_roi[0]->height, src_roi[0]->width, CV_8UC1,
169  src[0] +
170  (src_roi[0]->start.y * src_roi[0]->line_step) +
171  (src_roi[0]->start.x * src_roi[0]->pixel_step),
172  src_roi[0]->line_step);
173 
174  if (dst == NULL) { dst = src[0]; dst_roi = src_roi[0]; }
175 
176  cv::Mat dstm(dst_roi->height, dst_roi->width, CV_8UC1,
177  dst +
180  dst_roi->line_step);
181 
182  if (se == NULL) {
183  cv::dilate(srcm, dstm, cv::Mat());
184  } else {
185  cv::Mat sem(se_width, se_height, CV_8UC1);
186  cv::Point sem_anchor(se_anchor_x, se_anchor_y);
187  cv::dilate(srcm, dstm, sem, sem_anchor);
188  }
189 #endif
190 
191 }
192 
193 } // 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:52
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:81
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