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 <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 # include <opencv2/imgproc.hpp>
37 #else
38 # error "Neither IPP nor OpenCV available"
39 #endif
40 
41 namespace firevision {
42 #if 0 /* just to make Emacs auto-indent happy */
43 }
44 #endif
45 
46 /** @class FilterThreshold <fvfilters/threshold.h>
47  * Threshold filter.
48  * Implementation for threshold filter, this filter will luminance
49  * values below a given threshold to the given min_replace value,
50  * values above a given max threshold will be set to the max_replace
51  * value
52  */
53 
54 /** Constructor.
55  * @param min minimum value
56  * @param min_replace values below min are replaced with this value
57  * @param max maximum value
58  * @param max_replace values above max are replaced with this value
59  */
60 FilterThreshold::FilterThreshold(unsigned char min, unsigned char min_replace,
61  unsigned char max, 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 
76 
77 /** Set new thresholds.
78  * @param min minimum value
79  * @param min_replace values below min are replaced with this value
80  * @param max maximum value
81  * @param max_replace values above max are replaced with this value
82  */
83 void
84 FilterThreshold::set_thresholds(unsigned char min, unsigned char min_replace,
85  unsigned char max, 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 
94 void
96 {
97 #if defined(HAVE_IPP)
98  IppiSize size;
99  size.width = src_roi[0]->width;
100  size.height = src_roi[0]->height;
101 
102  IppStatus status;
103 
104  if ((dst == NULL) || (dst == src[0])) {
105  // In-place
106  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,
107  size, max, max_replace );
108  if ( status == ippStsNoErr ) {
109  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,
110  size, min, min_replace );
111  }
112  } else {
113  // base + number of bytes to line y + pixel bytes
114  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,
116  size, max, max_replace );
117 
118  if ( status == ippStsNoErr ) {
119  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,
121  size, min, min_replace );
122  }
123  }
124 
125  if ( status != ippStsNoErr ) {
126  throw fawkes::Exception("Threshold filter failed with %i\n", status);
127  }
128 
129 #elif defined(HAVE_OPENCV)
130  if ((dst == NULL) || (dst == src[0])) {
131  throw fawkes::Exception("OpenCV-based threshold filter cannot be in-place");
132  }
133 
134  cv::Mat srcm(src_roi[0]->height, src_roi[0]->width, CV_8UC1,
135  src[0] +
136  (src_roi[0]->start.y * src_roi[0]->line_step) +
137  (src_roi[0]->start.x * src_roi[0]->pixel_step),
138  src_roi[0]->line_step);
139 
140  cv::Mat dstm(dst_roi->height, dst_roi->width, CV_8UC1,
141  dst +
144  dst_roi->line_step);
145 
146  cv::threshold(srcm, dstm, max, max_replace, cv::THRESH_BINARY);
147  cv::threshold(srcm, dstm, min, 0, cv::THRESH_TOZERO);
148 
149 #endif
150 
151 }
152 
153 } // 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
FilterThreshold(unsigned char min=128, unsigned char min_replace=0, unsigned char max=127, unsigned char max_replace=255)
Constructor.
Definition: threshold.cpp:60
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:95
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:84
ROI * dst_roi
Destination ROI.
Definition: filter.h:72