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# include <opencv2/opencv.hpp>
35#else
36# error "Neither IPP nor OpenCV available"
37#endif
38
39namespace firevision {
40
41/** @class FilterThreshold <fvfilters/threshold.h>
42 * Threshold filter.
43 * Implementation for threshold filter, this filter will luminance
44 * values below a given threshold to the given min_replace value,
45 * values above a given max threshold will be set to the max_replace
46 * value
47 */
48
49/** Constructor.
50 * @param min minimum value
51 * @param min_replace values below min are replaced with this value
52 * @param max maximum value
53 * @param max_replace values above max are replaced with this value
54 */
56 unsigned char min_replace,
57 unsigned char max,
58 unsigned char max_replace)
59: Filter("FilterThreshold")
60{
61 this->min = min;
62 this->max = max;
63 this->min_replace = min_replace;
64 this->max_replace = max_replace;
65#if defined(HAVE_OPENCV)
66 if (min_replace != 0) {
67 throw fawkes::Exception("OpenCV-based threshold filter only allows min_replace=0");
68 }
69#endif
70}
71
72/** Set new thresholds.
73 * @param min minimum value
74 * @param min_replace values below min are replaced with this value
75 * @param max maximum value
76 * @param max_replace values above max are replaced with this value
77 */
78void
80 unsigned char min_replace,
81 unsigned char max,
82 unsigned char max_replace)
83{
84 this->min = min;
85 this->max = max;
86 this->min_replace = min_replace;
87 this->max_replace = max_replace;
88}
89
90void
92{
93#if defined(HAVE_IPP)
94 IppiSize size;
95 size.width = src_roi[0]->width;
96 size.height = src_roi[0]->height;
97
98 IppStatus status;
99
100 if ((dst == NULL) || (dst == src[0])) {
101 // In-place
102 status = ippiThreshold_GTVal_8u_C1IR(src[0] + (src_roi[0]->start.y * src_roi[0]->line_step)
103 + (src_roi[0]->start.x * src_roi[0]->pixel_step),
104 src_roi[0]->line_step,
105 size,
106 max,
107 max_replace);
108 if (status == ippStsNoErr) {
109 status = ippiThreshold_LTVal_8u_C1IR(src[0] + (src_roi[0]->start.y * src_roi[0]->line_step)
110 + (src_roi[0]->start.x * src_roi[0]->pixel_step),
111 src_roi[0]->line_step,
112 size,
113 min,
114 min_replace);
115 }
116 } else {
117 // base + number of bytes to line y + pixel bytes
118 status = ippiThreshold_GTVal_8u_C1R(src[0] + (src_roi[0]->start.y * src_roi[0]->line_step)
119 + (src_roi[0]->start.x * src_roi[0]->pixel_step),
120 src_roi[0]->line_step,
124 size,
125 max,
126 max_replace);
127
128 if (status == ippStsNoErr) {
129 status = ippiThreshold_LTVal_8u_C1R(src[0] + (src_roi[0]->start.y * src_roi[0]->line_step)
130 + (src_roi[0]->start.x * src_roi[0]->pixel_step),
131 src_roi[0]->line_step,
135 size,
136 min,
137 min_replace);
138 }
139 }
140
141 if (status != ippStsNoErr) {
142 throw fawkes::Exception("Threshold filter failed with %i\n", status);
143 }
144
145#elif defined(HAVE_OPENCV)
146 if ((dst == NULL) || (dst == src[0])) {
147 throw fawkes::Exception("OpenCV-based threshold filter cannot be in-place");
148 }
149
150 cv::Mat srcm(src_roi[0]->height,
151 src_roi[0]->width,
152 CV_8UC1,
153 src[0] + (src_roi[0]->start.y * src_roi[0]->line_step)
154 + (src_roi[0]->start.x * src_roi[0]->pixel_step),
155 src_roi[0]->line_step);
156
157 cv::Mat dstm(dst_roi->height,
158 dst_roi->width,
159 CV_8UC1,
163
164 cv::threshold(srcm, dstm, max, max_replace, cv::THRESH_BINARY);
165 cv::threshold(srcm, dstm, min, 0, cv::THRESH_TOZERO);
166
167#endif
168}
169
170} // 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:79
virtual void apply()
Apply the filter.
Definition: threshold.cpp:91
FilterThreshold(unsigned char min=128, unsigned char min_replace=0, unsigned char max=127, unsigned char max_replace=255)
Constructor.
Definition: threshold.cpp:55
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