Fawkes API  Fawkes Development Version
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 <core/exception.h>
27 #include <fvfilters/threshold.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 
44 /** @class FilterThreshold <fvfilters/threshold.h>
45  * Threshold filter.
46  * Implementation for threshold filter, this filter will luminance
47  * values below a given threshold to the given min_replace value,
48  * values above a given max threshold will be set to the max_replace
49  * value
50  */
51 
52 /** Constructor.
53  * @param min minimum value
54  * @param min_replace values below min are replaced with this value
55  * @param max maximum value
56  * @param max_replace values above max are replaced with this value
57  */
59  unsigned char min_replace,
60  unsigned char max,
61  unsigned char max_replace)
62 : Filter("FilterThreshold")
63 {
64  this->min = min;
65  this->max = max;
66  this->min_replace = min_replace;
67  this->max_replace = max_replace;
68 #if defined(HAVE_OPENCV)
69  if (min_replace != 0) {
70  throw fawkes::Exception("OpenCV-based threshold filter only allows min_replace=0");
71  }
72 #endif
73 }
74 
75 /** Set new thresholds.
76  * @param min minimum value
77  * @param min_replace values below min are replaced with this value
78  * @param max maximum value
79  * @param max_replace values above max are replaced with this value
80  */
81 void
83  unsigned char min_replace,
84  unsigned char max,
85  unsigned char max_replace)
86 {
87  this->min = min;
88  this->max = max;
89  this->min_replace = min_replace;
90  this->max_replace = max_replace;
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)
106  + (src_roi[0]->start.x * src_roi[0]->pixel_step),
107  src_roi[0]->line_step,
108  size,
109  max,
110  max_replace);
111  if (status == ippStsNoErr) {
112  status = ippiThreshold_LTVal_8u_C1IR(src[0] + (src_roi[0]->start.y * src_roi[0]->line_step)
113  + (src_roi[0]->start.x * src_roi[0]->pixel_step),
114  src_roi[0]->line_step,
115  size,
116  min,
117  min_replace);
118  }
119  } else {
120  // base + number of bytes to line y + pixel bytes
121  status = ippiThreshold_GTVal_8u_C1R(src[0] + (src_roi[0]->start.y * src_roi[0]->line_step)
122  + (src_roi[0]->start.x * src_roi[0]->pixel_step),
123  src_roi[0]->line_step,
125  + (dst_roi->start.x * dst_roi->pixel_step),
127  size,
128  max,
129  max_replace);
130 
131  if (status == ippStsNoErr) {
132  status = ippiThreshold_LTVal_8u_C1R(src[0] + (src_roi[0]->start.y * src_roi[0]->line_step)
133  + (src_roi[0]->start.x * src_roi[0]->pixel_step),
134  src_roi[0]->line_step,
136  + (dst_roi->start.x * dst_roi->pixel_step),
138  size,
139  min,
140  min_replace);
141  }
142  }
143 
144  if (status != ippStsNoErr) {
145  throw fawkes::Exception("Threshold filter failed with %i\n", status);
146  }
147 
148 #elif defined(HAVE_OPENCV)
149  if ((dst == NULL) || (dst == src[0])) {
150  throw fawkes::Exception("OpenCV-based threshold filter cannot be in-place");
151  }
152 
153  cv::Mat srcm(src_roi[0]->height,
154  src_roi[0]->width,
155  CV_8UC1,
156  src[0] + (src_roi[0]->start.y * src_roi[0]->line_step)
157  + (src_roi[0]->start.x * src_roi[0]->pixel_step),
158  src_roi[0]->line_step);
159 
160  cv::Mat dstm(dst_roi->height,
161  dst_roi->width,
162  CV_8UC1,
164  + (dst_roi->start.x * dst_roi->pixel_step),
165  dst_roi->line_step);
166 
167  cv::threshold(srcm, dstm, max, max_replace, cv::THRESH_BINARY);
168  cv::threshold(srcm, dstm, min, 0, cv::THRESH_TOZERO);
169 
170 #endif
171 }
172 
173 } // end namespace firevision
Base class for exceptions in Fawkes.
Definition: exception.h:36
void set_thresholds(unsigned char min, unsigned char min_replace, unsigned char max, unsigned char max_replace)
Set new thresholds.
Definition: threshold.cpp:82
virtual void apply()
Apply the filter.
Definition: threshold.cpp:94
FilterThreshold(unsigned char min=128, unsigned char min_replace=0, unsigned char max=127, unsigned char max_replace=255)
Constructor.
Definition: threshold.cpp:58
Filter interface.
Definition: filter.h:33
ROI ** src_roi
Source ROIs, dynamically allocated by Filter ctor.
Definition: filter.h:66
unsigned char ** src
Source buffers, dynamically allocated by Filter ctor.
Definition: filter.h:61
unsigned char * dst
Destination buffer.
Definition: filter.h:63
ROI * dst_roi
Destination ROI.
Definition: filter.h:68
unsigned int height
ROI height.
Definition: roi.h:119
fawkes::upoint_t start
ROI start.
Definition: roi.h:115
unsigned int line_step
line step
Definition: roi.h:125
unsigned int width
ROI width.
Definition: roi.h:117
unsigned int pixel_step
pixel step
Definition: roi.h:127
unsigned int x
x coordinate
Definition: types.h:36
unsigned int y
y coordinate
Definition: types.h:37