1826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker/*M///////////////////////////////////////////////////////////////////////////////////////
2826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker//
3d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz//
5d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz//  By downloading, copying, installing or using the software you agree to this license.
6d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz//  If you do not agree to this license, do not download, install,
7d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz//  copy or use the software.
8bb2f85d397fa65f20c6932186d9156ced9beb184Michael Portuesi//
9826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker//
10826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker//                        Intel License Agreement
11826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker//                For Open Source Computer Vision Library
12826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker//
13826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker// Copyright (C) 2000, Intel Corporation, all rights reserved.
14d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz// Third party copyrights are property of their respective owners.
15d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz//
16d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz// Redistribution and use in source and binary forms, with or without modification,
17d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz// are permitted provided that the following conditions are met:
18d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz//
19d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz//   * Redistribution's of source code must retain the above copyright notice,
20d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz//     this list of conditions and the following disclaimer.
21d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz//
22826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker//   * Redistribution's in binary form must reproduce the above copyright notice,
23826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker//     this list of conditions and the following disclaimer in the documentation
24826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker//     and/or other materials provided with the distribution.
25ba7a1270d36fb28e5f90be135431700c4ce81504Phil Goodwin//
26826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker//   * The name of Intel Corporation may not be used to endorse or promote products
27ba7a1270d36fb28e5f90be135431700c4ce81504Phil Goodwin//     derived from this software without specific prior written permission.
28826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker//
29ba7a1270d36fb28e5f90be135431700c4ce81504Phil Goodwin// This software is provided by the copyright holders and contributors "as is" and
30826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker// any express or implied warranties, including, but not limited to, the implied
31826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker// warranties of merchantability and fitness for a particular purpose are disclaimed.
32826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker// In no event shall the Intel Corporation or contributors be liable for any direct,
3329610b43047065c5dc66452d33f0113ac7e65fe8Phil Goodwin// indirect, incidental, special, exemplary, or consequential damages
34bb2f85d397fa65f20c6932186d9156ced9beb184Michael Portuesi// (including, but not limited to, procurement of substitute goods or services;
3540c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams// loss of use, data, or profits; or business interruption) however caused
3640c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams// and on any theory of liability, whether in contract, strict liability,
3740c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams// or tort (including negligence or otherwise) arising in any way out of
3840c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams// the use of this software, even if advised of the possibility of such damage.
3940c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams//
4040c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams//M*/
41041a15421126d0667517def59cef9b284359d729Tom Parker
42041a15421126d0667517def59cef9b284359d729Tom Parker#include "_cv.h"
43d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz#include <stdio.h>
44826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker
45826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker
46826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parkerstatic void
47826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom ParkericvCalcMinEigenVal( const float* cov, int cov_step, float* dst,
48826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker                    int dst_step, CvSize size, CvMat* buffer )
49826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker{
50826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker    int j;
51826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker    float* buf = buffer->data.fl;
52826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker    cov_step /= sizeof(cov[0]);
53826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker    dst_step /= sizeof(dst[0]);
54826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker    buffer->rows = 1;
55826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker
56826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker    for( ; size.height--; cov += cov_step, dst += dst_step )
57826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker    {
58826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker        for( j = 0; j < size.width; j++ )
59826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker        {
60826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker            double a = cov[j*3]*0.5;
61826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker            double b = cov[j*3+1];
62826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker            double c = cov[j*3+2]*0.5;
63826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker
64826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker            buf[j + size.width] = (float)(a + c);
65826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker            buf[j] = (float)((a - c)*(a - c) + b*b);
66bb2f85d397fa65f20c6932186d9156ced9beb184Michael Portuesi        }
6740c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams
68d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz        cvPow( buffer, buffer, 0.5 );
69d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz
70d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz        for( j = 0; j < size.width ; j++ )
71d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz            dst[j] = (float)(buf[j + size.width] - buf[j]);
72d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz    }
73d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz}
74d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz
75d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz
76d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultzstatic void
77d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler SchultzicvCalcHarris( const float* cov, int cov_step, float* dst,
78d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz               int dst_step, CvSize size, CvMat* /*buffer*/, double k )
79d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz{
80d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz    int j;
81d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz    cov_step /= sizeof(cov[0]);
82d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz    dst_step /= sizeof(dst[0]);
8340c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams
84826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker    for( ; size.height--; cov += cov_step, dst += dst_step )
85826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker    {
86d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz        for( j = 0; j < size.width; j++ )
87d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz        {
88d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz            double a = cov[j*3];
89d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz            double b = cov[j*3+1];
90d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz            double c = cov[j*3+2];
91d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz            dst[j] = (float)(a*c - b*b - k*(a + c)*(a + c));
92d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz        }
93d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz    }
94d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz}
95d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz
96d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz
97041a15421126d0667517def59cef9b284359d729Tom Parkerstatic void
98041a15421126d0667517def59cef9b284359d729Tom ParkericvCalcEigenValsVecs( const float* cov, int cov_step, float* dst,
99826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker                      int dst_step, CvSize size, CvMat* buffer )
100826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker{
101041a15421126d0667517def59cef9b284359d729Tom Parker    static int y0 = 0;
102d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz
103d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz    int j;
104826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker    float* buf = buffer->data.fl;
105826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker    cov_step /= sizeof(cov[0]);
106d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz    dst_step /= sizeof(dst[0]);
107d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz
108826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker    for( ; size.height--; cov += cov_step, dst += dst_step )
109826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker    {
110826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker        for( j = 0; j < size.width; j++ )
111826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker        {
112826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker            double a = cov[j*3]*0.5;
113826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker            double b = cov[j*3+1];
114826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker            double c = cov[j*3+2]*0.5;
115826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker
116d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz            buf[j + size.width] = (float)(a + c);
117d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz            buf[j] = (float)((a - c)*(a - c) + b*b);
118826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker        }
119826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker
120826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker        buffer->rows = 1;
121041a15421126d0667517def59cef9b284359d729Tom Parker        cvPow( buffer, buffer, 0.5 );
122bb2f85d397fa65f20c6932186d9156ced9beb184Michael Portuesi
123bb2f85d397fa65f20c6932186d9156ced9beb184Michael Portuesi        for( j = 0; j < size.width; j++ )
124826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker        {
125826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker            double a = cov[j*3];
126826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker            double b = cov[j*3+1];
127826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker            double c = cov[j*3+2];
128826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker
129826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker            double l1 = buf[j + size.width] + buf[j];
130826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker            double l2 = buf[j + size.width] - buf[j];
131ba7a1270d36fb28e5f90be135431700c4ce81504Phil Goodwin
132826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker            double x = b;
133826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker            double y = l1 - a;
134826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker            double e = fabs(x);
135bb2f85d397fa65f20c6932186d9156ced9beb184Michael Portuesi
136826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker            if( e + fabs(y) < 1e-4 )
137d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz            {
138d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz                y = b;
139826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker                x = l1 - c;
140826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker                e = fabs(x);
141826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker                if( e + fabs(y) < 1e-4 )
142826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker                {
143826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker                    e = 1./(e + fabs(y) + FLT_EPSILON);
144826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker                    x *= e, y *= e;
145826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker                }
146826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker            }
147826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker
148826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker            buf[j] = (float)(x*x + y*y + DBL_EPSILON);
149d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz            dst[6*j] = (float)l1;
150d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz            dst[6*j + 2] = (float)x;
151d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz            dst[6*j + 3] = (float)y;
152d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz
153d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz            x = b;
154d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz            y = l2 - a;
155d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz            e = fabs(x);
156d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz
157d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz            if( e + fabs(y) < 1e-4 )
158d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz            {
159d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz                y = b;
160d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz                x = l2 - c;
161d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz                e = fabs(x);
162d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz                if( e + fabs(y) < 1e-4 )
163d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz                {
164d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz                    e = 1./(e + fabs(y) + FLT_EPSILON);
165d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz                    x *= e, y *= e;
166d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz                }
167d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz            }
168d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz
169d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz            buf[j + size.width] = (float)(x*x + y*y + DBL_EPSILON);
170d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz            dst[6*j + 1] = (float)l2;
171d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz            dst[6*j + 4] = (float)x;
172d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz            dst[6*j + 5] = (float)y;
173d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz        }
174d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz
175d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz        buffer->rows = 2;
176bb2f85d397fa65f20c6932186d9156ced9beb184Michael Portuesi        cvPow( buffer, buffer, -0.5 );
177826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker
178826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker        for( j = 0; j < size.width; j++ )
179d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz        {
180d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz            double t0 = buf[j]*dst[6*j + 2];
181d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz            double t1 = buf[j]*dst[6*j + 3];
182d6cd04adb85895b340ce9b6df33b749b22099c43Joe Moore and Tyler Schultz
18340c6251719cccc0a84ae99c976d2836b14374ce6Christian Williams            dst[6*j + 2] = (float)t0;
184826bfc77015bfd1044cfb0f98b0fafe65f1e465eTom Parker            dst[6*j + 3] = (float)t1;
185
186            t0 = buf[j + size.width]*dst[6*j + 4];
187            t1 = buf[j + size.width]*dst[6*j + 5];
188
189            dst[6*j + 4] = (float)t0;
190            dst[6*j + 5] = (float)t1;
191        }
192
193        y0++;
194    }
195}
196
197
198#define ICV_MINEIGENVAL     0
199#define ICV_HARRIS          1
200#define ICV_EIGENVALSVECS   2
201
202static void
203icvCornerEigenValsVecs( const CvMat* src, CvMat* eigenv, int block_size,
204                        int aperture_size, int op_type, double k=0. )
205{
206    CvSepFilter dx_filter, dy_filter;
207    CvBoxFilter blur_filter;
208    CvMat *tempsrc = 0;
209    CvMat *Dx = 0, *Dy = 0, *cov = 0;
210    CvMat *sqrt_buf = 0;
211
212    int buf_size = 1 << 12;
213
214    CV_FUNCNAME( "icvCornerEigenValsVecs" );
215
216    __BEGIN__;
217
218    int i, j, y, dst_y = 0, max_dy, delta = 0;
219    int aperture_size0 = aperture_size;
220    int temp_step = 0, d_step;
221    uchar* shifted_ptr = 0;
222    int depth, d_depth;
223    int stage = CV_START;
224    CvSobelFixedIPPFunc ipp_sobel_vert = 0, ipp_sobel_horiz = 0;
225    CvFilterFixedIPPFunc ipp_scharr_vert = 0, ipp_scharr_horiz = 0;
226    CvSize el_size, size, stripe_size;
227    int aligned_width;
228    CvPoint el_anchor;
229    double factorx, factory;
230    bool use_ipp = false;
231
232    if( block_size < 3 || !(block_size & 1) )
233        CV_ERROR( CV_StsOutOfRange, "averaging window size must be an odd number >= 3" );
234
235    if( (aperture_size < 3 && aperture_size != CV_SCHARR) || !(aperture_size & 1) )
236        CV_ERROR( CV_StsOutOfRange,
237        "Derivative filter aperture size must be a positive odd number >=3 or CV_SCHARR" );
238
239    depth = CV_MAT_DEPTH(src->type);
240    d_depth = depth == CV_8U ? CV_16S : CV_32F;
241
242    size = cvGetMatSize(src);
243    aligned_width = cvAlign(size.width, 4);
244
245    aperture_size = aperture_size == CV_SCHARR ? 3 : aperture_size;
246    el_size = cvSize( aperture_size, aperture_size );
247    el_anchor = cvPoint( aperture_size/2, aperture_size/2 );
248
249    if( aperture_size <= 5 && icvFilterSobelVert_8u16s_C1R_p )
250    {
251        if( depth == CV_8U && aperture_size0 == CV_SCHARR )
252        {
253            ipp_scharr_vert = icvFilterScharrVert_8u16s_C1R_p;
254            ipp_scharr_horiz = icvFilterScharrHoriz_8u16s_C1R_p;
255        }
256        else if( depth == CV_32F && aperture_size0 == CV_SCHARR )
257        {
258            ipp_scharr_vert = icvFilterScharrVert_32f_C1R_p;
259            ipp_scharr_horiz = icvFilterScharrHoriz_32f_C1R_p;
260        }
261        else if( depth == CV_8U )
262        {
263            ipp_sobel_vert = icvFilterSobelVert_8u16s_C1R_p;
264            ipp_sobel_horiz = icvFilterSobelHoriz_8u16s_C1R_p;
265        }
266        else if( depth == CV_32F )
267        {
268            ipp_sobel_vert = icvFilterSobelVert_32f_C1R_p;
269            ipp_sobel_horiz = icvFilterSobelHoriz_32f_C1R_p;
270        }
271    }
272
273    if( (ipp_sobel_vert && ipp_sobel_horiz) ||
274        (ipp_scharr_vert && ipp_scharr_horiz) )
275    {
276        CV_CALL( tempsrc = icvIPPFilterInit( src, buf_size,
277            cvSize(el_size.width,el_size.height + block_size)));
278        shifted_ptr = tempsrc->data.ptr + el_anchor.y*tempsrc->step +
279                      el_anchor.x*CV_ELEM_SIZE(depth);
280        temp_step = tempsrc->step ? tempsrc->step : CV_STUB_STEP;
281        max_dy = tempsrc->rows - aperture_size + 1;
282        use_ipp = true;
283    }
284    else
285    {
286        ipp_sobel_vert = ipp_sobel_horiz = 0;
287        ipp_scharr_vert = ipp_scharr_horiz = 0;
288
289        CV_CALL( dx_filter.init_deriv( size.width, depth, d_depth, 1, 0, aperture_size0 ));
290        CV_CALL( dy_filter.init_deriv( size.width, depth, d_depth, 0, 1, aperture_size0 ));
291        max_dy = buf_size / src->cols;
292        max_dy = MAX( max_dy, aperture_size + block_size );
293    }
294
295    CV_CALL( Dx = cvCreateMat( max_dy, aligned_width, d_depth ));
296    CV_CALL( Dy = cvCreateMat( max_dy, aligned_width, d_depth ));
297    CV_CALL( cov = cvCreateMat( max_dy + block_size + 1, size.width, CV_32FC3 ));
298    CV_CALL( sqrt_buf = cvCreateMat( 2, size.width, CV_32F ));
299    Dx->cols = Dy->cols = size.width;
300
301    if( !use_ipp )
302        max_dy -= aperture_size - 1;
303    d_step = Dx->step ? Dx->step : CV_STUB_STEP;
304
305    CV_CALL(blur_filter.init(size.width, CV_32FC3, CV_32FC3, 0, cvSize(block_size,block_size)));
306    stripe_size = size;
307
308    factorx = (double)(1 << (aperture_size - 1)) * block_size;
309    if( aperture_size0 == CV_SCHARR )
310        factorx *= 2;
311    if( depth == CV_8U )
312        factorx *= 255.;
313    factory = factorx = 1./factorx;
314    if( ipp_sobel_vert )
315        factory = -factory;
316
317    for( y = 0; y < size.height; y += delta )
318    {
319        if( !use_ipp )
320        {
321            delta = MIN( size.height - y, max_dy );
322            if( y + delta == size.height )
323                stage = stage & CV_START ? CV_START + CV_END : CV_END;
324            dx_filter.process( src, Dx, cvRect(0,y,-1,delta), cvPoint(0,0), stage );
325            stripe_size.height = dy_filter.process( src, Dy, cvRect(0,y,-1,delta),
326                                                    cvPoint(0,0), stage );
327        }
328        else
329        {
330            delta = icvIPPFilterNextStripe( src, tempsrc, y, el_size, el_anchor );
331            stripe_size.height = delta;
332
333            if( ipp_sobel_vert )
334            {
335                IPPI_CALL( ipp_sobel_vert( shifted_ptr, temp_step,
336                        Dx->data.ptr, d_step, stripe_size,
337                        aperture_size*10 + aperture_size ));
338                IPPI_CALL( ipp_sobel_horiz( shifted_ptr, temp_step,
339                        Dy->data.ptr, d_step, stripe_size,
340                        aperture_size*10 + aperture_size ));
341            }
342            else /*if( ipp_scharr_vert )*/
343            {
344                IPPI_CALL( ipp_scharr_vert( shifted_ptr, temp_step,
345                        Dx->data.ptr, d_step, stripe_size ));
346                IPPI_CALL( ipp_scharr_horiz( shifted_ptr, temp_step,
347                        Dy->data.ptr, d_step, stripe_size ));
348            }
349        }
350
351        for( i = 0; i < stripe_size.height; i++ )
352        {
353            float* cov_data = (float*)(cov->data.ptr + i*cov->step);
354            if( d_depth == CV_16S )
355            {
356                const short* dxdata = (const short*)(Dx->data.ptr + i*Dx->step);
357                const short* dydata = (const short*)(Dy->data.ptr + i*Dy->step);
358
359                for( j = 0; j < size.width; j++ )
360                {
361                    double dx = dxdata[j]*factorx;
362                    double dy = dydata[j]*factory;
363
364                    cov_data[j*3] = (float)(dx*dx);
365                    cov_data[j*3+1] = (float)(dx*dy);
366                    cov_data[j*3+2] = (float)(dy*dy);
367                }
368            }
369            else
370            {
371                const float* dxdata = (const float*)(Dx->data.ptr + i*Dx->step);
372                const float* dydata = (const float*)(Dy->data.ptr + i*Dy->step);
373
374                for( j = 0; j < size.width; j++ )
375                {
376                    double dx = dxdata[j]*factorx;
377                    double dy = dydata[j]*factory;
378
379                    cov_data[j*3] = (float)(dx*dx);
380                    cov_data[j*3+1] = (float)(dx*dy);
381                    cov_data[j*3+2] = (float)(dy*dy);
382                }
383            }
384        }
385
386        if( y + stripe_size.height >= size.height )
387            stage = stage & CV_START ? CV_START + CV_END : CV_END;
388
389        stripe_size.height = blur_filter.process(cov,cov,
390            cvRect(0,0,-1,stripe_size.height),cvPoint(0,0),stage+CV_ISOLATED_ROI);
391
392        if( op_type == ICV_MINEIGENVAL )
393            icvCalcMinEigenVal( cov->data.fl, cov->step,
394                (float*)(eigenv->data.ptr + dst_y*eigenv->step), eigenv->step,
395                stripe_size, sqrt_buf );
396        else if( op_type == ICV_HARRIS )
397            icvCalcHarris( cov->data.fl, cov->step,
398                (float*)(eigenv->data.ptr + dst_y*eigenv->step), eigenv->step,
399                stripe_size, sqrt_buf, k );
400        else if( op_type == ICV_EIGENVALSVECS )
401            icvCalcEigenValsVecs( cov->data.fl, cov->step,
402                (float*)(eigenv->data.ptr + dst_y*eigenv->step), eigenv->step,
403                stripe_size, sqrt_buf );
404
405        dst_y += stripe_size.height;
406        stage = CV_MIDDLE;
407    }
408
409    __END__;
410
411    cvReleaseMat( &Dx );
412    cvReleaseMat( &Dy );
413    cvReleaseMat( &cov );
414    cvReleaseMat( &sqrt_buf );
415    cvReleaseMat( &tempsrc );
416}
417
418
419CV_IMPL void
420cvCornerMinEigenVal( const void* srcarr, void* eigenvarr,
421                     int block_size, int aperture_size )
422{
423    CV_FUNCNAME( "cvCornerMinEigenVal" );
424
425    __BEGIN__;
426
427    CvMat stub, *src = (CvMat*)srcarr;
428    CvMat eigstub, *eigenv = (CvMat*)eigenvarr;
429
430    CV_CALL( src = cvGetMat( srcarr, &stub ));
431    CV_CALL( eigenv = cvGetMat( eigenv, &eigstub ));
432
433    if( (CV_MAT_TYPE(src->type) != CV_8UC1 && CV_MAT_TYPE(src->type) != CV_32FC1) ||
434        CV_MAT_TYPE(eigenv->type) != CV_32FC1 )
435        CV_ERROR( CV_StsUnsupportedFormat, "Input must be 8uC1 or 32fC1, output must be 32fC1" );
436
437    if( !CV_ARE_SIZES_EQ( src, eigenv ))
438        CV_ERROR( CV_StsUnmatchedSizes, "" );
439
440    CV_CALL( icvCornerEigenValsVecs( src, eigenv, block_size, aperture_size, ICV_MINEIGENVAL ));
441
442    __END__;
443}
444
445
446CV_IMPL void
447cvCornerHarris( const CvArr* srcarr, CvArr* harris_responce,
448                int block_size, int aperture_size, double k )
449{
450    CV_FUNCNAME( "cvCornerHarris" );
451
452    __BEGIN__;
453
454    CvMat stub, *src = (CvMat*)srcarr;
455    CvMat eigstub, *eigenv = (CvMat*)harris_responce;
456
457    CV_CALL( src = cvGetMat( srcarr, &stub ));
458    CV_CALL( eigenv = cvGetMat( eigenv, &eigstub ));
459
460    if( (CV_MAT_TYPE(src->type) != CV_8UC1 && CV_MAT_TYPE(src->type) != CV_32FC1) ||
461        CV_MAT_TYPE(eigenv->type) != CV_32FC1 )
462        CV_ERROR( CV_StsUnsupportedFormat, "Input must be 8uC1 or 32fC1, output must be 32fC1" );
463
464    if( !CV_ARE_SIZES_EQ( src, eigenv ))
465        CV_ERROR( CV_StsUnmatchedSizes, "" );
466
467    CV_CALL( icvCornerEigenValsVecs( src, eigenv, block_size, aperture_size, ICV_HARRIS, k ));
468
469    __END__;
470}
471
472
473CV_IMPL void
474cvCornerEigenValsAndVecs( const void* srcarr, void* eigenvarr,
475                          int block_size, int aperture_size )
476{
477    CV_FUNCNAME( "cvCornerEigenValsAndVecs" );
478
479    __BEGIN__;
480
481    CvMat stub, *src = (CvMat*)srcarr;
482    CvMat eigstub, *eigenv = (CvMat*)eigenvarr;
483
484    CV_CALL( src = cvGetMat( srcarr, &stub ));
485    CV_CALL( eigenv = cvGetMat( eigenv, &eigstub ));
486
487    if( CV_MAT_CN(eigenv->type)*eigenv->cols != src->cols*6 ||
488        eigenv->rows != src->rows )
489        CV_ERROR( CV_StsUnmatchedSizes, "Output array should be 6 times "
490            "wider than the input array and they should have the same height");
491
492    if( (CV_MAT_TYPE(src->type) != CV_8UC1 && CV_MAT_TYPE(src->type) != CV_32FC1) ||
493        CV_MAT_TYPE(eigenv->type) != CV_32FC1 )
494        CV_ERROR( CV_StsUnsupportedFormat, "Input must be 8uC1 or 32fC1, output must be 32fC1" );
495
496    CV_CALL( icvCornerEigenValsVecs( src, eigenv, block_size, aperture_size, ICV_EIGENVALSVECS ));
497
498    __END__;
499}
500
501
502CV_IMPL void
503cvPreCornerDetect( const void* srcarr, void* dstarr, int aperture_size )
504{
505    CvSepFilter dx_filter, dy_filter, d2x_filter, d2y_filter, dxy_filter;
506    CvMat *Dx = 0, *Dy = 0, *D2x = 0, *D2y = 0, *Dxy = 0;
507    CvMat *tempsrc = 0;
508
509    int buf_size = 1 << 12;
510
511    CV_FUNCNAME( "cvPreCornerDetect" );
512
513    __BEGIN__;
514
515    int i, j, y, dst_y = 0, max_dy, delta = 0;
516    int temp_step = 0, d_step;
517    uchar* shifted_ptr = 0;
518    int depth, d_depth;
519    int stage = CV_START;
520    CvSobelFixedIPPFunc ipp_sobel_vert = 0, ipp_sobel_horiz = 0,
521                        ipp_sobel_vert_second = 0, ipp_sobel_horiz_second = 0,
522                        ipp_sobel_cross = 0;
523    CvSize el_size, size, stripe_size;
524    int aligned_width;
525    CvPoint el_anchor;
526    double factor;
527    CvMat stub, *src = (CvMat*)srcarr;
528    CvMat dststub, *dst = (CvMat*)dstarr;
529    bool use_ipp = false;
530
531    CV_CALL( src = cvGetMat( srcarr, &stub ));
532    CV_CALL( dst = cvGetMat( dst, &dststub ));
533
534    if( (CV_MAT_TYPE(src->type) != CV_8UC1 && CV_MAT_TYPE(src->type) != CV_32FC1) ||
535        CV_MAT_TYPE(dst->type) != CV_32FC1 )
536        CV_ERROR( CV_StsUnsupportedFormat, "Input must be 8uC1 or 32fC1, output must be 32fC1" );
537
538    if( !CV_ARE_SIZES_EQ( src, dst ))
539        CV_ERROR( CV_StsUnmatchedSizes, "" );
540
541    if( aperture_size == CV_SCHARR )
542        CV_ERROR( CV_StsOutOfRange, "CV_SCHARR is not supported by this function" );
543
544    if( aperture_size < 3 || aperture_size > 7 || !(aperture_size & 1) )
545        CV_ERROR( CV_StsOutOfRange,
546        "Derivative filter aperture size must be 3, 5 or 7" );
547
548    depth = CV_MAT_DEPTH(src->type);
549    d_depth = depth == CV_8U ? CV_16S : CV_32F;
550
551    size = cvGetMatSize(src);
552    aligned_width = cvAlign(size.width, 4);
553
554    el_size = cvSize( aperture_size, aperture_size );
555    el_anchor = cvPoint( aperture_size/2, aperture_size/2 );
556
557    if( aperture_size <= 5 && icvFilterSobelVert_8u16s_C1R_p )
558    {
559        if( depth == CV_8U )
560        {
561            ipp_sobel_vert = icvFilterSobelVert_8u16s_C1R_p;
562            ipp_sobel_horiz = icvFilterSobelHoriz_8u16s_C1R_p;
563            ipp_sobel_vert_second = icvFilterSobelVertSecond_8u16s_C1R_p;
564            ipp_sobel_horiz_second = icvFilterSobelHorizSecond_8u16s_C1R_p;
565            ipp_sobel_cross = icvFilterSobelCross_8u16s_C1R_p;
566        }
567        else if( depth == CV_32F )
568        {
569            ipp_sobel_vert = icvFilterSobelVert_32f_C1R_p;
570            ipp_sobel_horiz = icvFilterSobelHoriz_32f_C1R_p;
571            ipp_sobel_vert_second = icvFilterSobelVertSecond_32f_C1R_p;
572            ipp_sobel_horiz_second = icvFilterSobelHorizSecond_32f_C1R_p;
573            ipp_sobel_cross = icvFilterSobelCross_32f_C1R_p;
574        }
575    }
576
577    if( ipp_sobel_vert && ipp_sobel_horiz && ipp_sobel_vert_second &&
578        ipp_sobel_horiz_second && ipp_sobel_cross )
579    {
580        CV_CALL( tempsrc = icvIPPFilterInit( src, buf_size, el_size ));
581        shifted_ptr = tempsrc->data.ptr + el_anchor.y*tempsrc->step +
582                      el_anchor.x*CV_ELEM_SIZE(depth);
583        temp_step = tempsrc->step ? tempsrc->step : CV_STUB_STEP;
584        max_dy = tempsrc->rows - aperture_size + 1;
585        use_ipp = true;
586    }
587    else
588    {
589        ipp_sobel_vert = ipp_sobel_horiz = 0;
590        ipp_sobel_vert_second = ipp_sobel_horiz_second = ipp_sobel_cross = 0;
591        dx_filter.init_deriv( size.width, depth, d_depth, 1, 0, aperture_size );
592        dy_filter.init_deriv( size.width, depth, d_depth, 0, 1, aperture_size );
593        d2x_filter.init_deriv( size.width, depth, d_depth, 2, 0, aperture_size );
594        d2y_filter.init_deriv( size.width, depth, d_depth, 0, 2, aperture_size );
595        dxy_filter.init_deriv( size.width, depth, d_depth, 1, 1, aperture_size );
596        max_dy = buf_size / src->cols;
597        max_dy = MAX( max_dy, aperture_size );
598    }
599
600    CV_CALL( Dx = cvCreateMat( max_dy, aligned_width, d_depth ));
601    CV_CALL( Dy = cvCreateMat( max_dy, aligned_width, d_depth ));
602    CV_CALL( D2x = cvCreateMat( max_dy, aligned_width, d_depth ));
603    CV_CALL( D2y = cvCreateMat( max_dy, aligned_width, d_depth ));
604    CV_CALL( Dxy = cvCreateMat( max_dy, aligned_width, d_depth ));
605    Dx->cols = Dy->cols = D2x->cols = D2y->cols = Dxy->cols = size.width;
606
607    if( !use_ipp )
608        max_dy -= aperture_size - 1;
609    d_step = Dx->step ? Dx->step : CV_STUB_STEP;
610
611    stripe_size = size;
612
613    factor = 1 << (aperture_size - 1);
614    if( depth == CV_8U )
615        factor *= 255;
616    factor = 1./(factor * factor * factor);
617
618    aperture_size = aperture_size * 10 + aperture_size;
619
620    for( y = 0; y < size.height; y += delta )
621    {
622        if( !use_ipp )
623        {
624            delta = MIN( size.height - y, max_dy );
625            CvRect roi = cvRect(0,y,size.width,delta);
626            CvPoint origin=cvPoint(0,0);
627
628            if( y + delta == size.height )
629                stage = stage & CV_START ? CV_START + CV_END : CV_END;
630
631            dx_filter.process(src,Dx,roi,origin,stage);
632            dy_filter.process(src,Dy,roi,origin,stage);
633            d2x_filter.process(src,D2x,roi,origin,stage);
634            d2y_filter.process(src,D2y,roi,origin,stage);
635            stripe_size.height = dxy_filter.process(src,Dxy,roi,origin,stage);
636        }
637        else
638        {
639            delta = icvIPPFilterNextStripe( src, tempsrc, y, el_size, el_anchor );
640            stripe_size.height = delta;
641
642            IPPI_CALL( ipp_sobel_vert( shifted_ptr, temp_step,
643                Dx->data.ptr, d_step, stripe_size, aperture_size ));
644            IPPI_CALL( ipp_sobel_horiz( shifted_ptr, temp_step,
645                Dy->data.ptr, d_step, stripe_size, aperture_size ));
646            IPPI_CALL( ipp_sobel_vert_second( shifted_ptr, temp_step,
647                D2x->data.ptr, d_step, stripe_size, aperture_size ));
648            IPPI_CALL( ipp_sobel_horiz_second( shifted_ptr, temp_step,
649                D2y->data.ptr, d_step, stripe_size, aperture_size ));
650            IPPI_CALL( ipp_sobel_cross( shifted_ptr, temp_step,
651                Dxy->data.ptr, d_step, stripe_size, aperture_size ));
652        }
653
654        for( i = 0; i < stripe_size.height; i++, dst_y++ )
655        {
656            float* dstdata = (float*)(dst->data.ptr + dst_y*dst->step);
657
658            if( d_depth == CV_16S )
659            {
660                const short* dxdata = (const short*)(Dx->data.ptr + i*Dx->step);
661                const short* dydata = (const short*)(Dy->data.ptr + i*Dy->step);
662                const short* d2xdata = (const short*)(D2x->data.ptr + i*D2x->step);
663                const short* d2ydata = (const short*)(D2y->data.ptr + i*D2y->step);
664                const short* dxydata = (const short*)(Dxy->data.ptr + i*Dxy->step);
665
666                for( j = 0; j < stripe_size.width; j++ )
667                {
668                    double dx = dxdata[j];
669                    double dx2 = dx * dx;
670                    double dy = dydata[j];
671                    double dy2 = dy * dy;
672
673                    dstdata[j] = (float)(factor*(dx2*d2ydata[j] + dy2*d2xdata[j] - 2*dx*dy*dxydata[j]));
674                }
675            }
676            else
677            {
678                const float* dxdata = (const float*)(Dx->data.ptr + i*Dx->step);
679                const float* dydata = (const float*)(Dy->data.ptr + i*Dy->step);
680                const float* d2xdata = (const float*)(D2x->data.ptr + i*D2x->step);
681                const float* d2ydata = (const float*)(D2y->data.ptr + i*D2y->step);
682                const float* dxydata = (const float*)(Dxy->data.ptr + i*Dxy->step);
683
684                for( j = 0; j < stripe_size.width; j++ )
685                {
686                    double dx = dxdata[j];
687                    double dy = dydata[j];
688                    dstdata[j] = (float)(factor*(dx*dx*d2ydata[j] + dy*dy*d2xdata[j] - 2*dx*dy*dxydata[j]));
689                }
690            }
691        }
692
693        stage = CV_MIDDLE;
694    }
695
696    __END__;
697
698    cvReleaseMat( &Dx );
699    cvReleaseMat( &Dy );
700    cvReleaseMat( &D2x );
701    cvReleaseMat( &D2y );
702    cvReleaseMat( &Dxy );
703    cvReleaseMat( &tempsrc );
704}
705
706/* End of file */
707