📄 anorm.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 "CvTest.h"
/* Testing parameters */
static char FuncName[] = "cvNorm";
static char TestName[] = "Norm calculation functions test";
static char TestClass[] = "Algorithm";
static const double EPS = 1.e-13;
static const double FEPS = 1.e-6;
static int Min_Image_Width, Max_Image_Width, Max_ROI_Offset;
#define TYPE_8U 0
#define TYPE_8S 1
#define TYPE_32F 2
#define MIN_VAL(type) type==IPL_DEPTH_8U ? 0 : type==IPL_DEPTH_8S ? -128 : type==IPL_DEPTH_32F ? -1.f : 0
#define MAX_VAL(type) type==IPL_DEPTH_8U ? 255 : type==IPL_DEPTH_8S ? 127 : type==IPL_DEPTH_32F ? 1.f : 0
static const int cvlType[9]={_CV_C,_CV_L1,_CV_L2,_CV_C,_CV_L1,_CV_L2,
_CV_RELATIVE_C,_CV_RELATIVE_L1,_CV_RELATIVE_L2};
/*------------------------------------------------ Test functions --------------------- */
double calcTestNorm( double* a, double* b, int n, int type )
{
int i;
double n1=0, n2=0;
switch( type )
{
case _CV_C:
if(b)
for( i=0; i<n; i++ )
{
double t = fabs( a[i] - b[i] );
if( n1<t ) n1=t;
}
else
for( i=0; i<n; i++ )
{
double t = fabs( a[i] );
if( n1<t ) n1=t;
}
break;
case _CV_L1:
if(b)
for( i=0; i<n; i++ )
n1 += fabs( a[i] - b[i] );
else
for( i=0; i<n; i++ )
n1 += fabs( a[i] );
break;
case _CV_L2:
if(b)
for( i=0; i<n; i++ )
{
double t = a[i] - b[i];
n1 += t*t;
}
else
for( i=0; i<n; i++ )
{
double t = a[i];
n1 += t*t;
}
n1 = sqrt( n1 );
break;
case _CV_RELATIVE_C:
for( i=0; i<n; i++ )
{
double bi = b[i];
double t = fabs( a[i] - bi );
if( n1<t ) n1 = t;
t = fabs( bi );
if( n2<t ) n2 = t;
}
n1 = n2 ? n1/n2 : -20.0;
break;
case _CV_RELATIVE_L1:
for( i=0; i<n; i++ )
{
double bi = b[i];
n1 += fabs( a[i] - bi );
n2 += fabs( bi );
}
n1 = n2 ? n1/n2 : -20.0;
break;
case _CV_RELATIVE_L2:
for( i=0; i<n; i++ )
{
double bi = b[i];
double t = fabs( a[i] - bi );
t *= t;
n1 += t;
t = fabs( bi );
t *= t;
n2 += t;
}
n1 = n2 ? n1/n2 : -20.0;
n1 = sqrt( n1 );
break;
default:
n1 = -30.0;
}
return n1;
}
/*......................................................................................*/
double TestNorm( IplImage* A, IplImage* B, int type )
{
uchar *A_data = 0, *B_data = 0;
int A_step = 0, B_step = 0;
CvSize A_size, B_size;
int i, j, j4, k, coi, channels, nx, ny, n;
double *a=0, *b=0, norm;
cvGetImageRawData( A, &A_data, &A_step, &A_size );
coi = A->roi->coi;
channels = A->nChannels;
if( coi<0 || coi>3 ) return -1.0;
if( coi > 0 && channels != 3 ) return -2.0;
if( B )
{
cvGetImageRawData( B, &B_data, &B_step, &B_size );
if( A_step!=B_step ) return -3.0;
if( A_size.height!=B_size.height || A_size.width!=B_size.width ) return -4.0;
if( coi!=B->roi->coi ) return -5.0;
}
else
if( type==_CV_RELATIVE_C || type==_CV_RELATIVE_L1 || type==_CV_RELATIVE_L2 ) return -10.0;
nx = A_size.width;
ny = A_size.height;
n = nx*ny;
a = (double*)icvAlloc( n*sizeof(double));
if(B) b = (double*)icvAlloc( n*sizeof(double));
k = 0;
if( channels < 3 )
{
if( B )
{
if( A->depth == IPL_DEPTH_8U )
{
for( i=0; i<ny; i++, A_data+=A_step, B_data+=B_step )
for( j=0; j<nx; j++, k++ )
{
a[k] = A_data[j];
b[k] = B_data[j];
}
}
if( A->depth == IPL_DEPTH_8S )
{
for( i=0; i<ny; i++, A_data+=A_step, B_data+=B_step )
for( j=0; j<nx; j++, k++ )
{
a[k] = *(char*)(A_data+j);
b[k] = *(char*)(B_data+j);
}
}
if( A->depth == IPL_DEPTH_32F )
{
for( i=0; i<ny; i++, A_data+=A_step, B_data+=B_step )
for( j=0, j4=0; j<nx; j++, j4+=4, k++ )
{
a[k] = *(float*)(A_data+j4);
b[k] = *(float*)(B_data+j4);
}
}
}
else
{
if( A->depth == IPL_DEPTH_8U )
{
for( i=0; i<ny; i++, A_data+=A_step )
for( j=0; j<nx; j++, k++ )
a[k] = A_data[j];
}
if( A->depth == IPL_DEPTH_8S )
{
for( i=0; i<ny; i++, A_data+=A_step )
for( j=0; j<nx; j++, k++ )
a[k] = *(char*)(A_data+j);
}
if( A->depth == IPL_DEPTH_32F )
{
for( i=0; i<ny; i++, A_data+=A_step )
for( j=0, j4=0; j<nx; j++, j4+=4, k++ )
a[k] = *(float*)(A_data+j4);
}
}
}
else /* channels = 3 */
{
coi--;
if( B )
{
if( A->depth == IPL_DEPTH_8U )
{
A_data += coi;
B_data += coi;
for( i=0; i<ny; i++, A_data+=A_step, B_data+=B_step )
for( j=0, j4=0; j<nx; j++, j4+=3, k++ )
{
a[k] = A_data[j4];
b[k] = B_data[j4];
}
}
if( A->depth == IPL_DEPTH_8S )
{
A_data += coi;
B_data += coi;
for( i=0; i<ny; i++, A_data+=A_step, B_data+=B_step )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -