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