📄 uiplutils.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"
#include <math.h>
#include <assert.h>
#include <float.h>
#define clip(x,a,b) ((x)<(a) ? (a) : (x) > (b) ? (b) : (x))
void atsGetImageInfo( IplImage* img, void** pData, int* pStep,
CvSize* pSz, int* pDepth, int* pChannels,
int* pBtPix )
{
if( !img )
{
assert(0);
}
else
{
int channels = img->nChannels;
int depth = img->depth;
int step = img->widthStep;
int bt_pix;
if( img->origin != IPL_ORIGIN_TL ||
img->dataOrder != IPL_DATA_ORDER_PIXEL )
{
assert(0);
return;
}
bt_pix = ((depth & 255) >> 3) * channels;
if( pDepth ) *pDepth = depth;
if( pChannels ) *pChannels = channels;
if( pStep ) *pStep = step;
if( pBtPix ) *pBtPix = bt_pix;
if( pSz )
{
pSz->width = img->roi ? img->roi->width : img->width;
pSz->height= img->roi ? img->roi->height : img->height;
}
if( pData )
{
*pData = img->imageData + (!img->roi ? 0 :
img->roi->yOffset*step + img->roi->xOffset*bt_pix );
}
}
}
/*
Fills the whole image or selected ROI by random numbers.
Supports only 8u, 8s and 32f formats.
*/
void atsFillRandomImage( IplImage* img, double low, double high )
{
int depth, channels, step;
CvSize sz;
void* data;
atsGetImageInfo( img, (void**)&data, &step, &sz, &depth, &channels, 0 );
sz.width *= channels;
switch( depth )
{
case IPL_DEPTH_32F:
{
float *fdata = (float*)data;
int x, y;
step /= sizeof(float);
high = (high-low)/RAND_MAX;
for( y = 0; y < sz.height; y++, fdata += step )
for( x = 0; x < sz.width; x++ )
{
fdata[x] = (float)(rand()*high + low);
}
break;
}
case IPL_DEPTH_8U:
case IPL_DEPTH_8S:
{
int l = (int)(low + (low > 0 ? .5 : low == 0 ? 0 : -.5));
int h = (int)floor(high+(high > 0 ? .5 : high == 0 ? 0 : -.5));
int x, y;
uchar *udata = (uchar*)data;
if( img->depth == IPL_DEPTH_8U )
{
l = clip( l, 0, 255 );
h = clip( h, 0, 255 );
}
else
{
l = clip( l, -128, 127 );
h = clip( h, -128, 127 );
}
if( l > h ) x = l, l = h, h = x;
h -= l - 1;
if( h == 256 ) /* speedup */
{
int w1 = sz.width&1;
for( y = 0; y < sz.height; y++, udata += step )
{
for( x = 0; x+1 < sz.width; x += 2 )
{
*((short*)(udata + x)) = (short)rand();
}
if( w1 ) udata[sz.width-1] = (uchar)(rand()&255);
}
}
else
{
for( y = 0; y < sz.height; y++, udata += step )
for( x = 0; x < sz.width; x++ )
{
udata[x] = (uchar)(rand()%h + l);
}
}
break;
}
default: assert(0);
}
}
/*
Fills the whole image or selected ROI by random numbers using 32-bit RNG.
Supports only 8u, 8s and 32f formats.
*/
void atsFillRandomImageEx( IplImage* img, AtsRandState* state )
{
int depth, channels, step;
CvSize sz;
void* _data;
atsGetImageInfo( img, (void**)&_data, &step, &sz, &depth, &channels, 0 );
sz.width *= channels;
switch( depth )
{
case IPL_DEPTH_32F:
{
float *data = (float*)_data;
int y;
step /= sizeof(data[0]);
for( y = 0; y < sz.height; y++, data += step )
{
atsbRand32f( state, data, sz.width );
}
break;
}
case IPL_DEPTH_8U:
case IPL_DEPTH_8S:
{
uchar *data = (uchar*)_data;
int y;
step /= sizeof(data[0]);
for( y = 0; y < sz.height; y++, data += step )
{
atsbRand8u( state, data, sz.width );
}
break;
}
case IPL_DEPTH_16S:
{
short *data = (short*)_data;
int y;
step /= sizeof(data[0]);
for( y = 0; y < sz.height; y++, data += step )
{
atsbRand16s( state, data, sz.width );
}
break;
}
case IPL_DEPTH_32S:
{
int *data = (int*)_data;
int y;
step /= sizeof(data[0]);
for( y = 0; y < sz.height; y++, data += step )
{
atsbRand32s( state, data, sz.width );
}
break;
}
default: assert(0);
}
}
/* Allocates the IPL image and (may be) clears it */
IplImage* atsCreateImage( int w, int h, int depth, int nch, int clear_flag )
{
IplImage *img = iplCreateImageHeader
( nch, 0, depth, "", "", IPL_DATA_ORDER_PIXEL,
IPL_ORIGIN_TL, 8, w, h, 0, 0, 0, 0 );
if( depth == IPL_DEPTH_32F )
{
iplAllocateImageFP( img, clear_flag, 0 );
}
else
{
iplAllocateImage( img, clear_flag, 0 );
}
return img;
}
void atsReleaseImage( IplImage* img )
{
assert( img->roi == 0 && img->maskROI == 0 );
iplDeallocate( img, IPL_IMAGE_ALL );
}
/* extracts ROI data from the image and writes it in a single row */
void atsGetDataFromImage( IplImage* img, void* data )
{
char* src;
char* dst = (char*)data;
int bt_pix, step;
CvSize sz;
int y;
atsGetImageInfo( img, (void**)&src, &step, &sz, 0, 0, &bt_pix );
sz.width *= bt_pix;
for( y = 0; y < sz.height; y++, src += step, dst += sz.width )
{
memcpy( dst, src, sz.width );
}
}
/* writes linear data to the image ROI */
void atsPutDataToImage( IplImage *img, void *data )
{
char *src = (char*)data, *dst;
int bt_pix, step;
CvSize sz;
int y;
atsGetImageInfo( img, (void**)&dst, &step, &sz, 0, 0, &bt_pix );
sz.width *= bt_pix;
for( y = 0; y < sz.height; y++, dst += step, src += sz.width )
{
memcpy( dst, src, sz.width );
}
}
void atsConvert( IplImage* src, IplImage* dst )
{
char* src_data = 0;
char* dst_data = 0;
int src_step = 0, dst_step = 0;
int src_depth = 0, dst_depth = 0;
int src_channels = 0, dst_channels = 0;
CvSize sz, sz2;
atsGetImageInfo( src, (void**)&src_data, &src_step, &sz, &src_depth,
&src_channels, 0 );
atsGetImageInfo( dst, (void**)&dst_data, &dst_step, &sz2,
&dst_depth, &dst_channels, 0 );
assert( src_channels == dst_channels );
if( src_depth == dst_depth )
iplCopy( src, dst );
else if( src_depth != IPL_DEPTH_32F && dst_depth != IPL_DEPTH_32F )
iplConvert( src, dst );
else
{
int i, j;
sz.width = (MIN( sz.width, sz2.width ))*src_channels;
sz.height = MIN( sz.height, sz2.height );
if( dst_depth == IPL_DEPTH_32F )
{
switch( src_depth )
{
case IPL_DEPTH_8U:
for( i = 0; i < sz.height; i++, src_data += src_step, dst_data += dst_step )
for( j = 0; j < sz.width; j++ )
((float*)dst_data)[j] = ((uchar*)src_data)[j];
break;
case IPL_DEPTH_8S:
for( i = 0; i < sz.height; i++, src_data += src_step, dst_data += dst_step )
for( j = 0; j < sz.width; j++ )
((float*)dst_data)[j] = src_data[j];
break;
case IPL_DEPTH_16S:
for( i = 0; i < sz.height; i++, src_data += src_step, dst_data += dst_step )
for( j = 0; j < sz.width; j++ )
((float*)dst_data)[j] = ((short*)src_data)[j];
break;
case IPL_DEPTH_32S:
for( i = 0; i < sz.height; i++, src_data += src_step, dst_data += dst_step )
for( j = 0; j < sz.width; j++ )
((float*)dst_data)[j] = (float)((int*)src_data)[j];
break;
}
}
else
{
switch( dst_depth )
{
case IPL_DEPTH_8U:
for( i = 0; i < sz.height; i++, src_data += src_step, dst_data += dst_step )
for( j = 0; j < sz.width; j++ )
{
int t = atsRound(((float*)src_data)[j]);
((uchar*)dst_data)[j] = (uchar)(!(t & ~255) ? t : t < 0 ? 0 : 255);
}
break;
case IPL_DEPTH_8S:
for( i = 0; i < sz.height; i++, src_data += src_step, dst_data += dst_step )
for( j = 0; j < sz.width; j++ )
{
int t = atsRound(((float*)src_data)[j]);
((char*)dst_data)[j] = (char)(!((t + 128) & ~255) ? t : t < 0 ? -128 : 127);
}
break;
case IPL_DEPTH_16S:
for( i = 0; i < sz.height; i++, src_data += src_step, dst_data += dst_step )
for( j = 0; j < sz.width; j++ )
{
int t = atsRound(((float*)src_data)[j]);
((short*)dst_data)[j] = (short)(!((t + 32768) & ~65535) ? t :
t < 0 ? -32768 : 32767);
}
break;
case IPL_DEPTH_32S:
for( i = 0; i < sz.height; i++, src_data += src_step, dst_data += dst_step )
for( j = 0; j < sz.width; j++ )
((int*)dst_data)[j] = (int)atsRound(((float*)src_data)[j]);
break;
}
}
}
}
/*
The function applies min filter using specified structuring element.
float numbers are processed using integer arithmetics (IEEE 754 format is assumed)
*/
void atsMinFilterEx( IplImage* src, IplImage* dst, IplConvKernel* B )
{
const int int_extr0 = 0x7fffffff;
uchar* srcData;
uchar* dstData;
int srcStep, dstStep;
int depth;
int ach; /* how many non-alpha channels and all the channels */
CvSize sz;
int cols = B->nCols;
int rows = B->nRows;
int anchorX = B->anchorX;
int anchorY = B->anchorY;
int* mask = B->values;
int x, y, c;
atsGetImageInfo( src, (void**)&srcData, &srcStep, &sz, &depth, &ach, 0 );
atsGetImageInfo( dst, (void**)&dstData, &dstStep, 0, 0, 0, 0 );
switch( depth )
{
case IPL_DEPTH_8U:
for( y = 0; y < sz.height; y++ )
for( x = 0; x < sz.width; x++ )
for( c = 0; c < ach; c++ )
{
int val = int_extr0;
int x1, y1 = y - anchorY;
int i, j;
for( i = 0; i < rows; i++, y1++ )
for( j = 0, x1 = x - anchorX; j < cols; j++, x1++ )
{
int x2 = x1, y2 = y1;
if( x1 < 0 ) x2 = 0;
if( x1 >= sz.width ) x2 = sz.width - 1;
if( y1 < 0 ) y2 = 0;
if( y1 >= sz.height ) y2 = sz.height - 1;
/* unsigned comparsion helps to check for positivity */
if( mask[i*cols + j] != 0 )
{
int temp = srcData[y2*srcStep + x2*ach + c];
val = ATS_MIN( val, temp );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -