Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
threshold.cpp
1 
2 /***************************************************************************
3  * threshold.cpp - Implementation for threshold filter, this filter will
4  * luminance values below a given threshold to the given
5  * min_replace value, values above a given max threshold
6  * will be set to the max_replace value
7  *
8  * Created: Tue Jun 07 14:30:10 2005
9  * Copyright 2005-2012 Tim Niemueller [www.niemueller.de]
10  ****************************************************************************/
11 
12 /* This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License as published by
14  * the Free Software Foundation; either version 2 of the License, or
15  * (at your option) any later version. A runtime exception applies to
16  * this software (see LICENSE.GPL_WRE file mentioned below for details).
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU Library General Public License for more details.
22  *
23  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
24  */
25 
26 #include <fvfilters/threshold.h>
27 
28 #include <core/exception.h>
29 
30 #include <cstddef>
31 
32 #ifdef HAVE_IPP
33 # include <ippi.h>
34 #elif defined(HAVE_OPENCV)
35 # include <cv.h>
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 FilterThreshold <fvfilters/threshold.h>
46  * Threshold filter.
47  * Implementation for threshold filter, this filter will luminance
48  * values below a given threshold to the given min_replace value,
49  * values above a given max threshold will be set to the max_replace
50  * value
51  */
52 
53 /** Constructor.
54  * @param min minimum value
55  * @param min_replace values below min are replaced with this value
56  * @param max maximum value
57  * @param max_replace values above max are replaced with this value
58  */
59 FilterThreshold::FilterThreshold(unsigned char min, unsigned char min_replace,
60  unsigned char max, unsigned char max_replace)
61  : Filter("FilterThreshold")
62 {
63  this->min = min;
64  this->max = max;
65  this->min_replace = min_replace;
66  this->max_replace = max_replace;
67 #if defined(HAVE_OPENCV)
68  if (min_replace != 0) {
69  throw fawkes::Exception("OpenCV-based threshold filter only allows min_replace=0");
70  }
71 #endif
72 
73 }
74 
75 
76 /** Set new thresholds.
77  * @param min minimum value
78  * @param min_replace values below min are replaced with this value
79  * @param max maximum value
80  * @param max_replace values above max are replaced with this value
81  */
82 void
83 FilterThreshold::set_thresholds(unsigned char min, unsigned char min_replace,
84  unsigned char max, unsigned char max_replace)
85 {
86  this->min = min;
87  this->max = max;
88  this->min_replace = min_replace;
89  this->max_replace = max_replace;
90 }
91 
92 
93 void
95 {
96 #if defined(HAVE_IPP)
97  IppiSize size;
98  size.width = src_roi[0]->width;
99  size.height = src_roi[0]->height;
100 
101  IppStatus status;
102 
103  if ((dst == NULL) || (dst == src[0])) {
104  // In-place
105  status = ippiThreshold_GTVal_8u_C1IR( src[0] + (src_roi[0]->start.y * src_roi[0]->line_step) + (src_roi[0]->start.x * src_roi[0]->pixel_step), src_roi[0]->line_step,
106  size, max, max_replace );
107  if ( status == ippStsNoErr ) {
108  status = ippiThreshold_LTVal_8u_C1IR( src[0] + (src_roi[0]->start.y * src_roi[0]->line_step) + (src_roi[0]->start.x * src_roi[0]->pixel_step), src_roi[0]->line_step,
109  size, min, min_replace );
110  }
111  } else {
112  // base + number of bytes to line y + pixel bytes
113  status = ippiThreshold_GTVal_8u_C1R( src[0] + (src_roi[0]->start.y * src_roi[0]->line_step) + (src_roi[0]->start.x * src_roi[0]->pixel_step), src_roi[0]->line_step,
115  size, max, max_replace );
116 
117  if ( status == ippStsNoErr ) {
118  status = ippiThreshold_LTVal_8u_C1R( src[0] + (src_roi[0]->start.y * src_roi[0]->line_step) + (src_roi[0]->start.x * src_roi[0]->pixel_step), src_roi[0]->line_step,
120  size, min, min_replace );
121  }
122  }
123 
124  if ( status != ippStsNoErr ) {
125  throw fawkes::Exception("Threshold filter failed with %i\n", status);
126  }
127 
128 #elif defined(HAVE_OPENCV)
129  if ((dst == NULL) || (dst == src[0])) {
130  throw fawkes::Exception("OpenCV-based threshold filter cannot be in-place");
131  }
132 
133  cv::Mat srcm(src_roi[0]->height, src_roi[0]->width, CV_8UC1,
134  src[0] +
135  (src_roi[0]->start.y * src_roi[0]->line_step) +
136  (src_roi[0]->start.x * src_roi[0]->pixel_step),
137  src_roi[0]->line_step);
138 
139  cv::Mat dstm(dst_roi->height, dst_roi->width, CV_8UC1,
140  dst +
143  dst_roi->line_step);
144 
145  cv::threshold(srcm, dstm, max, max_replace, cv::THRESH_BINARY);
146  cv::threshold(srcm, dstm, min, 0, cv::THRESH_TOZERO);
147 
148 #endif
149 
150 }
151 
152 } // end namespace firevision
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 ** src
Source buffers, dynamically allocated by Filter ctor.
Definition: filter.h:65
Base class for exceptions in Fawkes.
Definition: exception.h:36
Filter interface.
Definition: filter.h:35
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
virtual void apply()
Apply the filter.
Definition: threshold.cpp:94
unsigned int line_step
line step
Definition: roi.h:128
unsigned char * dst
Destination buffer.
Definition: filter.h:67
unsigned int pixel_step
pixel step
Definition: roi.h:130
void set_thresholds(unsigned char min, unsigned char min_replace, unsigned char max, unsigned char max_replace)
Set new thresholds.
Definition: threshold.cpp:83
ROI * dst_roi
Destination ROI.
Definition: filter.h:72