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