📄 cvcorner.cpp
字号:
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// Intel License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of Intel Corporation may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "_cv.h"
#include <stdio.h>
static void
icvCalcMinEigenVal( const float* cov, int cov_step, float* dst,
int dst_step, CvSize size, CvMat* buffer )
{
int j;
float* buf = buffer->data.fl;
cov_step /= sizeof(cov[0]);
dst_step /= sizeof(dst[0]);
buffer->rows = 1;
for( ; size.height--; cov += cov_step, dst += dst_step )
{
for( j = 0; j < size.width; j++ )
{
double a = cov[j*3]*0.5;
double b = cov[j*3+1];
double c = cov[j*3+2]*0.5;
buf[j + size.width] = (float)(a + c);
buf[j] = (float)((a - c)*(a - c) + b*b);
}
cvPow( buffer, buffer, 0.5 );
for( j = 0; j < size.width ; j++ )
dst[j] = (float)(buf[j + size.width] - buf[j]);
}
}
static void
icvCalcHarris( const float* cov, int cov_step, float* dst,
int dst_step, CvSize size, CvMat* /*buffer*/, double k )
{
int j;
cov_step /= sizeof(cov[0]);
dst_step /= sizeof(dst[0]);
for( ; size.height--; cov += cov_step, dst += dst_step )
{
for( j = 0; j < size.width; j++ )
{
double a = cov[j*3];
double b = cov[j*3+1];
double c = cov[j*3+2];
dst[j] = (float)(a*c - b*b - k*(a + c)*(a + c));
}
}
}
static void
icvCalcEigenValsVecs( const float* cov, int cov_step, float* dst,
int dst_step, CvSize size, CvMat* buffer )
{
static int y0 = 0;
int j;
float* buf = buffer->data.fl;
cov_step /= sizeof(cov[0]);
dst_step /= sizeof(dst[0]);
for( ; size.height--; cov += cov_step, dst += dst_step )
{
for( j = 0; j < size.width; j++ )
{
double a = cov[j*3]*0.5;
double b = cov[j*3+1];
double c = cov[j*3+2]*0.5;
buf[j + size.width] = (float)(a + c);
buf[j] = (float)((a - c)*(a - c) + b*b);
}
buffer->rows = 1;
cvPow( buffer, buffer, 0.5 );
for( j = 0; j < size.width; j++ )
{
double a = cov[j*3];
double b = cov[j*3+1];
double c = cov[j*3+2];
double l1 = buf[j + size.width] + buf[j];
double l2 = buf[j + size.width] - buf[j];
double x = b;
double y = l1 - a;
double e = fabs(x);
if( e + fabs(y) < 1e-4 )
{
y = b;
x = l1 - c;
e = fabs(x);
if( e + fabs(y) < 1e-4 )
{
e = 1./(e + fabs(y) + FLT_EPSILON);
x *= e, y *= e;
}
}
buf[j] = (float)(x*x + y*y + DBL_EPSILON);
dst[6*j] = (float)l1;
dst[6*j + 2] = (float)x;
dst[6*j + 3] = (float)y;
x = b;
y = l2 - a;
e = fabs(x);
if( e + fabs(y) < 1e-4 )
{
y = b;
x = l2 - c;
e = fabs(x);
if( e + fabs(y) < 1e-4 )
{
e = 1./(e + fabs(y) + FLT_EPSILON);
x *= e, y *= e;
}
}
buf[j + size.width] = (float)(x*x + y*y + DBL_EPSILON);
dst[6*j + 1] = (float)l2;
dst[6*j + 4] = (float)x;
dst[6*j + 5] = (float)y;
}
buffer->rows = 2;
cvPow( buffer, buffer, -0.5 );
for( j = 0; j < size.width; j++ )
{
double t0 = buf[j]*dst[6*j + 2];
double t1 = buf[j]*dst[6*j + 3];
dst[6*j + 2] = (float)t0;
dst[6*j + 3] = (float)t1;
t0 = buf[j + size.width]*dst[6*j + 4];
t1 = buf[j + size.width]*dst[6*j + 5];
dst[6*j + 4] = (float)t0;
dst[6*j + 5] = (float)t1;
}
y0++;
}
}
#define ICV_MINEIGENVAL 0
#define ICV_HARRIS 1
#define ICV_EIGENVALSVECS 2
static void
icvCornerEigenValsVecs( const CvMat* src, CvMat* eigenv, int block_size,
int aperture_size, int op_type, double k=0. )
{
CvSepFilter dx_filter, dy_filter;
CvBoxFilter blur_filter;
CvMat *tempsrc = 0;
CvMat *Dx = 0, *Dy = 0, *cov = 0;
CvMat *sqrt_buf = 0;
int buf_size = 1 << 12;
CV_FUNCNAME( "icvCornerEigenValsVecs" );
__BEGIN__;
int i, j, y, dst_y = 0, max_dy, delta = 0;
int aperture_size0 = aperture_size;
int temp_step = 0, d_step;
uchar* shifted_ptr = 0;
int depth, d_depth;
int stage = CV_START;
CvSobelFixedIPPFunc ipp_sobel_vert = 0, ipp_sobel_horiz = 0;
CvFilterFixedIPPFunc ipp_scharr_vert = 0, ipp_scharr_horiz = 0;
CvSize el_size, size, stripe_size;
int aligned_width;
CvPoint el_anchor;
double factorx, factory;
bool use_ipp = false;
if( block_size < 3 || !(block_size & 1) )
CV_ERROR( CV_StsOutOfRange, "averaging window size must be an odd number >= 3" );
if( aperture_size < 3 && aperture_size != CV_SCHARR || !(aperture_size & 1) )
CV_ERROR( CV_StsOutOfRange,
"Derivative filter aperture size must be a positive odd number >=3 or CV_SCHARR" );
depth = CV_MAT_DEPTH(src->type);
d_depth = depth == CV_8U ? CV_16S : CV_32F;
size = cvGetMatSize(src);
aligned_width = cvAlign(size.width, 4);
aperture_size = aperture_size == CV_SCHARR ? 3 : aperture_size;
el_size = cvSize( aperture_size, aperture_size );
el_anchor = cvPoint( aperture_size/2, aperture_size/2 );
if( aperture_size <= 5 && icvFilterSobelVert_8u16s_C1R_p )
{
if( depth == CV_8U && aperture_size0 == CV_SCHARR )
{
ipp_scharr_vert = icvFilterScharrVert_8u16s_C1R_p;
ipp_scharr_horiz = icvFilterScharrHoriz_8u16s_C1R_p;
}
else if( depth == CV_32F && aperture_size0 == CV_SCHARR )
{
ipp_scharr_vert = icvFilterScharrVert_32f_C1R_p;
ipp_scharr_horiz = icvFilterScharrHoriz_32f_C1R_p;
}
else if( depth == CV_8U )
{
ipp_sobel_vert = icvFilterSobelVert_8u16s_C1R_p;
ipp_sobel_horiz = icvFilterSobelHoriz_8u16s_C1R_p;
}
else if( depth == CV_32F )
{
ipp_sobel_vert = icvFilterSobelVert_32f_C1R_p;
ipp_sobel_horiz = icvFilterSobelHoriz_32f_C1R_p;
}
}
if( ipp_sobel_vert && ipp_sobel_horiz ||
ipp_scharr_vert && ipp_scharr_horiz )
{
CV_CALL( tempsrc = icvIPPFilterInit( src, buf_size,
cvSize(el_size.width,el_size.height + block_size)));
shifted_ptr = tempsrc->data.ptr + el_anchor.y*tempsrc->step +
el_anchor.x*CV_ELEM_SIZE(depth);
temp_step = tempsrc->step ? tempsrc->step : CV_STUB_STEP;
max_dy = tempsrc->rows - aperture_size + 1;
use_ipp = true;
}
else
{
ipp_sobel_vert = ipp_sobel_horiz = 0;
ipp_scharr_vert = ipp_scharr_horiz = 0;
CV_CALL( dx_filter.init_deriv( size.width, depth, d_depth, 1, 0, aperture_size0 ));
CV_CALL( dy_filter.init_deriv( size.width, depth, d_depth, 0, 1, aperture_size0 ));
max_dy = buf_size / src->cols;
max_dy = MAX( max_dy, aperture_size + block_size );
}
CV_CALL( Dx = cvCreateMat( max_dy, aligned_width, d_depth ));
CV_CALL( Dy = cvCreateMat( max_dy, aligned_width, d_depth ));
CV_CALL( cov = cvCreateMat( max_dy + block_size + 1, size.width, CV_32FC3 ));
CV_CALL( sqrt_buf = cvCreateMat( 2, size.width, CV_32F ));
Dx->cols = Dy->cols = size.width;
if( !use_ipp )
max_dy -= aperture_size - 1;
d_step = Dx->step ? Dx->step : CV_STUB_STEP;
CV_CALL(blur_filter.init(size.width, CV_32FC3, CV_32FC3, 0, cvSize(block_size,block_size)));
stripe_size = size;
factorx = (double)(1 << (aperture_size - 1)) * block_size;
if( aperture_size0 == CV_SCHARR )
factorx *= 2;
if( depth == CV_8U )
factorx *= 255.;
factory = factorx = 1./factorx;
if( ipp_sobel_vert )
factory = -factory;
for( y = 0; y < size.height; y += delta )
{
if( !use_ipp )
{
delta = MIN( size.height - y, max_dy );
if( y + delta == size.height )
stage = stage & CV_START ? CV_START + CV_END : CV_END;
dx_filter.process( src, Dx, cvRect(0,y,-1,delta), cvPoint(0,0), stage );
stripe_size.height = dy_filter.process( src, Dy, cvRect(0,y,-1,delta),
cvPoint(0,0), stage );
}
else
{
delta = icvIPPFilterNextStripe( src, tempsrc, y, el_size, el_anchor );
stripe_size.height = delta;
if( ipp_sobel_vert )
{
IPPI_CALL( ipp_sobel_vert( shifted_ptr, temp_step,
Dx->data.ptr, d_step, stripe_size,
aperture_size*10 + aperture_size ));
IPPI_CALL( ipp_sobel_horiz( shifted_ptr, temp_step,
Dy->data.ptr, d_step, stripe_size,
aperture_size*10 + aperture_size ));
}
else /*if( ipp_scharr_vert )*/
{
IPPI_CALL( ipp_scharr_vert( shifted_ptr, temp_step,
Dx->data.ptr, d_step, stripe_size ));
IPPI_CALL( ipp_scharr_horiz( shifted_ptr, temp_step,
Dy->data.ptr, d_step, stripe_size ));
}
}
for( i = 0; i < stripe_size.height; i++ )
{
float* cov_data = (float*)(cov->data.ptr + i*cov->step);
if( d_depth == CV_16S )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -