1 /*
2  * Copyright (C) 2011 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*$Id: db_feature_detection.h,v 1.3 2011/06/17 14:03:30 mbansal Exp $*/
18 
19 #ifndef DB_FEATURE_DETECTION_H
20 #define DB_FEATURE_DETECTION_H
21 
22 /*****************************************************************
23 *    Lean and mean begins here                                   *
24 *****************************************************************/
25 /*!
26  * \defgroup FeatureDetection Feature Detection
27  */
28 #include "db_utilities.h"
29 #include "db_utilities_constants.h"
30 #include <stdlib.h> //for NULL
31 
32 /*!
33  * \class db_CornerDetector_f
34  * \ingroup FeatureDetection
35  * \brief Harris corner detector for float images.
36  *
37  *  This class performs Harris corner extraction on *float* images managed
38  * with functions in \ref LMImageBasicUtilities.
39  */
40 class DB_API db_CornerDetector_f
41 {
42 public:
43     db_CornerDetector_f();
44     ~db_CornerDetector_f();
45 
46     /*!
47      * Set parameters and pre-allocate memory. Return an upper bound
48      * on the number of corners detected in one frame.
49      * \param im_width      width
50      * \param im_height     height
51      * \param target_nr_corners
52      * \param nr_horizontal_blocks
53      * \param nr_vertical_blocks
54      * \param absolute_threshold
55      * \param relative_threshold
56      */
57     unsigned long Init(int im_width,int im_height,
58                             int target_nr_corners=DB_DEFAULT_TARGET_NR_CORNERS,
59                             int nr_horizontal_blocks=DB_DEFAULT_NR_FEATURE_BLOCKS,
60                             int nr_vertical_blocks=DB_DEFAULT_NR_FEATURE_BLOCKS,
61                             double absolute_threshold=DB_DEFAULT_ABS_CORNER_THRESHOLD,
62                             double relative_threshold=DB_DEFAULT_REL_CORNER_THRESHOLD);
63 
64     /*!
65      * Detect the corners.
66      * x_coord and y_coord should be pre-allocated arrays of length returned by Init().
67      * \param img   row array pointer
68      * \param x_coord   corner locations
69      * \param y_coord   corner locations
70      * \param nr_corners    actual number of corners computed
71      */
72     void DetectCorners(const float * const *img,double *x_coord,double *y_coord,int *nr_corners) const;
SetAbsoluteThreshold(double a_thresh)73     void SetAbsoluteThreshold(double a_thresh) { m_a_thresh = a_thresh; };
SetRelativeThreshold(double r_thresh)74     void SetRelativeThreshold(double r_thresh) { m_r_thresh = r_thresh; };
75 protected:
76     void Clean();
77     unsigned long Start(int im_width,int im_height,
78             int block_width,int block_height,unsigned long area_factor,
79             double absolute_threshold,double relative_threshold,int chunkwidth);
80 
81     int m_w,m_h,m_cw,m_bw,m_bh;
82     /*Area factor holds the maximum number of corners to detect
83     per 10000 pixels*/
84     unsigned long m_area_factor,m_max_nr;
85     double m_a_thresh,m_r_thresh;
86     float *m_temp_f;
87     double *m_temp_d;
88     float **m_strength,*m_strength_mem;
89 };
90 /*!
91  * \class db_CornerDetector_u
92  * \ingroup FeatureDetection
93  * \brief Harris corner detector for byte images.
94  *
95  *  This class performs Harris corner extraction on *byte* images managed
96  * with functions in \ref LMImageBasicUtilities.
97  */
98 class DB_API db_CornerDetector_u
99 {
100 public:
101     db_CornerDetector_u();
102     virtual ~db_CornerDetector_u();
103 
104     /*!
105      Copy ctor duplicates settings.
106      Memory is not copied.
107      */
108     db_CornerDetector_u(const db_CornerDetector_u& cd);
109     /*!
110      Assignment optor duplicates settings.
111      Memory not copied.
112      */
113     db_CornerDetector_u& operator=(const db_CornerDetector_u& cd);
114 
115     /*!
116      * Set parameters and pre-allocate memory. Return an upper bound
117      * on the number of corners detected in one frame
118      */
119     virtual unsigned long Init(int im_width,int im_height,
120                             int target_nr_corners=DB_DEFAULT_TARGET_NR_CORNERS,
121                             int nr_horizontal_blocks=DB_DEFAULT_NR_FEATURE_BLOCKS,
122                             int nr_vertical_blocks=DB_DEFAULT_NR_FEATURE_BLOCKS,
123                             double absolute_threshold=DB_DEFAULT_ABS_CORNER_THRESHOLD,
124                             double relative_threshold=DB_DEFAULT_REL_CORNER_THRESHOLD);
125 
126     /*!
127      * Detect the corners.
128      * Observe that the image should be overallocated by at least 256 bytes
129      * at the end.
130      * x_coord and y_coord should be pre-allocated arrays of length returned by Init().
131      * Specifying image mask will restrict corner output to foreground regions.
132      * Foreground value can be specified using fgnd. By default any >0 mask value
133      * is considered to be foreground
134      * \param img   row array pointer
135      * \param x_coord   corner locations
136      * \param y_coord   corner locations
137      * \param nr_corners    actual number of corners computed
138      * \param msk       row array pointer to mask image
139      * \param fgnd      foreground value in the mask
140      */
141     virtual void DetectCorners(const unsigned char * const *img,double *x_coord,double *y_coord,int *nr_corners,
142         const unsigned char * const * msk=NULL, unsigned char fgnd=255) const;
143 
144     /*!
145      Set absolute feature threshold
146      */
SetAbsoluteThreshold(double a_thresh)147     virtual void SetAbsoluteThreshold(double a_thresh) { m_a_thresh = a_thresh; };
148     /*!
149      Set relative feature threshold
150      */
SetRelativeThreshold(double r_thresh)151     virtual void SetRelativeThreshold(double r_thresh) { m_r_thresh = r_thresh; };
152 
153     /*!
154      Extract corners from a pre-computed strength image.
155      \param strength    Harris strength image
156      \param x_coord corner locations
157      \param y_coord corner locations
158      \param nr_corners  actual number of corners computed
159      */
160     virtual void ExtractCorners(float ** strength, double *x_coord, double *y_coord, int *nr_corners);
161 protected:
162     virtual void Clean();
163     /*The absolute threshold to this function should be 16.0 times
164     normal*/
165     unsigned long Start(int im_width,int im_height,
166             int block_width,int block_height,unsigned long area_factor,
167             double absolute_threshold,double relative_threshold);
168 
169     int m_w,m_h,m_bw,m_bh;
170     /*Area factor holds the maximum number of corners to detect
171     per 10000 pixels*/
172     unsigned long m_area_factor,m_max_nr;
173     double m_a_thresh,m_r_thresh;
174     int *m_temp_i;
175     double *m_temp_d;
176     float **m_strength,*m_strength_mem;
177 };
178 
179 #endif /*DB_FEATURE_DETECTION_H*/
180