📄 asamplers.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 <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <float.h>
#include "CvTest.h"
static char* funcs[] =
{
"cvInitLineIterator, cvSampleLine",
"cvGetRectSubPix"
};
static char *test_desc = "Test of sampler functions";
/* actual parameters */
static int img_size, base_iters;
/* which tests have to run */
static int dt_l = 0, dt_h = 2,
ch_l = 0, ch_h = 1;
static int init_smp_params = 0;
static const int img8u_range = 256;
static const int img8s_range = 128;
static const float img32f_range = 1000.f;
static const int img32f_bits = 23;
static void read_smp_params( void )
{
if( !init_smp_params )
{
int data_types, channels;
/* Determine which tests are needed to run */
trsCaseRead( &data_types,"/a/8u/8s/32f", "a",
"a - all, 8u - unsigned char, 32f - float" );
if( data_types != 0 ) dt_l = dt_h = data_types - 1;
trsCaseRead( &channels, "/a/1/3", "1",
"a - all, 1 - single channel, 3 - three channels" );
if( channels != 0 ) ch_l = ch_h = channels - 1;
/* read tests params */
trsiRead( &img_size, "537", "width or height of image" );
trsiRead( &base_iters, "100", "Base number of iterations" );
init_smp_params = 1;
}
}
typedef CvStatus (*AtsInitLineIteratorR)( void* img, int step, CvSize size,
CvPoint pt1, CvPoint pt2,
CvLineIterator* iterator );
typedef CvStatus (*AtsSampleLineR)( void* img, int step, CvSize size,
CvPoint pt1, CvPoint pt2, void* buffer );
static CvPoint get_coordinates( void* ptr, IplImage* img )
{
CvPoint pt;
int ofs = (char*)ptr - img->imageData;
pt.y = ofs / img->widthStep;
pt.x = (ofs - pt.y * img->widthStep)/(img->nChannels * ((img->depth & 255)>>3));
return pt;
}
/* ///////////////////// line_smp_test ///////////////////////// */
static int line_smp_test( void* arg )
{
const int success_error_level = 0;
const char* message = "No errors";
int param = (int)arg;
int depth = param/2;
int channels = (param & 1);
int seed = atsGetSeed();
/* position where the maximum error occured */
int merr_iter = 0;
/* test parameters */
int i = 0, count;
double max_err = 0.;
int code = TRS_OK;
IplROI img_roi;
IplImage *img;
AtsRandState rng_state;
uchar* src_buf = 0;
uchar* dst_buf = 0;
uchar* ptr = 0;
read_smp_params();
if( !(ATS_RANGE( depth, dt_l, dt_h+1 ) &&
ATS_RANGE( channels, ch_l, ch_h+1 ))) return TRS_UNDEF;
depth = depth == 0 ? IPL_DEPTH_8U : depth == 1 ? IPL_DEPTH_8S : IPL_DEPTH_32F;
channels = channels ? 3 : 1;
img = atsCreateImage( img_size, img_size, depth, channels, 1 );
img->roi = &img_roi;
img_roi.coi = 0;
img_roi.xOffset = img_roi.yOffset = 0;
img_roi.width = img_size;
img_roi.height = img_size;
src_buf = (uchar*)icvAlloc( img_size * ((depth & 255)>>3) * channels );
dst_buf = (uchar*)icvAlloc( img_size * ((depth & 255)>>3) * channels );
assert( src_buf != 0 && dst_buf != 0 );
atsRandInit( &rng_state, 0, 1, seed );
switch( depth )
{
case IPL_DEPTH_8U:
atsRandSetBounds( &rng_state, 0, img8u_range );
break;
case IPL_DEPTH_32F:
atsRandSetBounds( &rng_state, -img32f_range, img32f_range );
atsRandSetFloatBits( &rng_state, img32f_bits );
break;
}
atsFillRandomImageEx( img, &rng_state );
for( i = 0; i < base_iters; i++ )
{
double err = 0;
CvPoint pt1, pt2;
CvPoint start_pt, end_pt;
int color = (atsRandPlain32s( &rng_state )|1)
& (channels == 1 ? 0xff : 0xffffff);
int j;
int64 result;
CvLineIterator iterator;
pt1.x = atsRandPlain32s( &rng_state ) % img_roi.width;
pt1.y = atsRandPlain32s( &rng_state ) % img_roi.height;
pt2.x = atsRandPlain32s( &rng_state ) % img_roi.width;
pt2.y = atsRandPlain32s( &rng_state ) % img_roi.height;
count = cvInitLineIterator( img, pt1, pt2, &iterator );
if( count != MAX( abs(pt1.x - pt2.x), abs(pt1.y - pt2.y)) + 1 )
{
message = "InitLineIterator returns wrong value";
code = TRS_FAIL;
goto test_exit;
}
start_pt = get_coordinates( iterator.ptr, img );
if( start_pt.x != pt1.x || start_pt.y != pt1.y )
{
message = "Staring point of the iterator is wrong";
code = TRS_FAIL;
goto test_exit;
}
switch( depth )
{
case IPL_DEPTH_8U:
{
uchar* buf = src_buf;
cvLine( img, pt1, pt2, color );
for( j = 0; j < count; j++ )
{
int clr = 0;
memcpy( &clr, iterator.ptr, channels );
if( clr != color ) break;
CV_NEXT_LINE_POINT( iterator );
}
if( j < count )
{
CvPoint temp = pt1;
pt1 = pt2;
pt2 = temp;
}
atsbRand8u( &rng_state, buf, count*channels );
count = cvInitLineIterator( img, pt1, pt2, &iterator );
if( count != MAX( abs(pt1.x - pt2.x), abs(pt1.y - pt2.y)) + 1 )
{
message = "InitLineIterator returns wrong value";
code = TRS_FAIL;
goto test_exit;
}
for( j = 0; j < count; j++, buf += channels )
{
int clr = 0;
ptr = iterator.ptr;
memcpy( &clr, iterator.ptr, channels );
if( clr != color )
{
message = "Iteration for 8u doesn't match line drawing function";
code = TRS_FAIL;
goto test_exit;
}
memcpy( iterator.ptr, buf, channels );
CV_NEXT_LINE_POINT( iterator );
}
if( err > success_error_level ) break;
result = cvSampleLine( img, pt1, pt2, dst_buf );
if( (int)result != count )
{
message = "SampleLine fails";
code = TRS_FAIL;
goto test_exit;
}
count *= channels;
for( j = 0; j < count; j++ )
{
double t = fabs(src_buf[j] - dst_buf[j]);
err = MAX( err, t );
}
}
break;
case IPL_DEPTH_32F:
{
float* buf = (float*)src_buf;
CvLineIterator byte_iterator;
int count_byte;
img->depth = IPL_DEPTH_8U;
count_byte = cvInitLineIterator( img, pt1, pt2, &byte_iterator );
img->depth = IPL_DEPTH_32F;
if( count_byte != count )
{
message = "Line iterator for 8u images differs from 32f";
code = TRS_FAIL;
goto test_exit;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -