2010-05-12 01:44:00 +08:00
|
|
|
/*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 "precomp.hpp"
|
|
|
|
|
|
|
|
CV_IMPL CvSeq* cvPointSeqFromMat( int seq_kind, const CvArr* arr,
|
|
|
|
CvContour* contour_header, CvSeqBlock* block )
|
|
|
|
{
|
|
|
|
CV_Assert( arr != 0 && contour_header != 0 && block != 0 );
|
|
|
|
|
|
|
|
int eltype;
|
|
|
|
CvMat* mat = (CvMat*)arr;
|
|
|
|
|
|
|
|
if( !CV_IS_MAT( mat ))
|
|
|
|
CV_Error( CV_StsBadArg, "Input array is not a valid matrix" );
|
|
|
|
|
|
|
|
eltype = CV_MAT_TYPE( mat->type );
|
|
|
|
if( eltype != CV_32SC2 && eltype != CV_32FC2 )
|
|
|
|
CV_Error( CV_StsUnsupportedFormat,
|
|
|
|
"The matrix can not be converted to point sequence because of "
|
|
|
|
"inappropriate element type" );
|
|
|
|
|
|
|
|
if( (mat->width != 1 && mat->height != 1) || !CV_IS_MAT_CONT(mat->type))
|
|
|
|
CV_Error( CV_StsBadArg,
|
|
|
|
"The matrix converted to point sequence must be "
|
|
|
|
"1-dimensional and continuous" );
|
|
|
|
|
|
|
|
cvMakeSeqHeaderForArray(
|
|
|
|
(seq_kind & (CV_SEQ_KIND_MASK|CV_SEQ_FLAG_CLOSED)) | eltype,
|
|
|
|
sizeof(CvContour), CV_ELEM_SIZE(eltype), mat->data.ptr,
|
|
|
|
mat->width*mat->height, (CvSeq*)contour_header, block );
|
|
|
|
|
|
|
|
return (CvSeq*)contour_header;
|
|
|
|
}
|
|
|
|
|
2010-12-12 05:50:31 +08:00
|
|
|
namespace cv
|
|
|
|
{
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-12-27 20:01:38 +08:00
|
|
|
static void copyMakeBorder_8u( const uchar* src, size_t srcstep, Size srcroi,
|
|
|
|
uchar* dst, size_t dststep, Size dstroi,
|
2010-12-12 05:50:31 +08:00
|
|
|
int top, int left, int cn, int borderType )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
const int isz = (int)sizeof(int);
|
2010-12-12 05:50:31 +08:00
|
|
|
int i, j, k, elemSize = 1;
|
|
|
|
bool intMode = false;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
if( (cn | srcstep | dststep | (size_t)src | (size_t)dst) % isz == 0 )
|
|
|
|
{
|
|
|
|
cn /= isz;
|
2010-12-12 05:50:31 +08:00
|
|
|
elemSize = isz;
|
|
|
|
intMode = true;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2010-12-12 05:50:31 +08:00
|
|
|
AutoBuffer<int> _tab((dstroi.width - srcroi.width)*cn);
|
|
|
|
int* tab = _tab;
|
|
|
|
int right = dstroi.width - srcroi.width - left;
|
|
|
|
int bottom = dstroi.height - srcroi.height - top;
|
|
|
|
|
|
|
|
for( i = 0; i < left; i++ )
|
|
|
|
{
|
|
|
|
j = borderInterpolate(i - left, srcroi.width, borderType)*cn;
|
|
|
|
for( k = 0; k < cn; k++ )
|
|
|
|
tab[i*cn + k] = j + k;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2010-12-12 05:50:31 +08:00
|
|
|
|
|
|
|
for( i = 0; i < right; i++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-12-12 05:50:31 +08:00
|
|
|
j = borderInterpolate(srcroi.width + i, srcroi.width, borderType)*cn;
|
|
|
|
for( k = 0; k < cn; k++ )
|
|
|
|
tab[(i+left)*cn + k] = j + k;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
srcroi.width *= cn;
|
|
|
|
dstroi.width *= cn;
|
|
|
|
left *= cn;
|
2010-12-12 05:50:31 +08:00
|
|
|
right *= cn;
|
|
|
|
|
2010-12-14 00:53:46 +08:00
|
|
|
uchar* dstInner = dst + dststep*top + left*elemSize;
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-12-12 05:50:31 +08:00
|
|
|
for( i = 0; i < srcroi.height; i++, dstInner += dststep, src += srcstep )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-12-12 05:50:31 +08:00
|
|
|
if( dstInner != src )
|
|
|
|
memcpy(dstInner, src, srcroi.width*elemSize);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2010-12-12 05:50:31 +08:00
|
|
|
if( intMode )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-12-12 05:50:31 +08:00
|
|
|
const int* isrc = (int*)src;
|
|
|
|
int* idstInner = (int*)dstInner;
|
|
|
|
for( j = 0; j < left; j++ )
|
|
|
|
idstInner[j - left] = isrc[tab[j]];
|
|
|
|
for( j = 0; j < right; j++ )
|
|
|
|
idstInner[j + srcroi.width] = isrc[tab[j + left]];
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2010-12-12 05:50:31 +08:00
|
|
|
else
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
for( j = 0; j < left; j++ )
|
2010-12-12 05:50:31 +08:00
|
|
|
dstInner[j - left] = src[tab[j]];
|
|
|
|
for( j = 0; j < right; j++ )
|
|
|
|
dstInner[j + srcroi.width] = src[tab[j + left]];
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
2010-12-12 05:50:31 +08:00
|
|
|
|
|
|
|
dstroi.width *= elemSize;
|
|
|
|
dst += dststep*top;
|
|
|
|
|
|
|
|
for( i = 0; i < top; i++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-12-12 05:50:31 +08:00
|
|
|
j = borderInterpolate(i - top, srcroi.height, borderType);
|
|
|
|
memcpy(dst + (i - top)*dststep, dst + j*dststep, dstroi.width);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2010-12-12 05:50:31 +08:00
|
|
|
|
|
|
|
for( i = 0; i < bottom; i++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-12-12 05:50:31 +08:00
|
|
|
j = borderInterpolate(i + srcroi.height, srcroi.height, borderType);
|
|
|
|
memcpy(dst + (i + srcroi.height)*dststep, dst + j*dststep, dstroi.width);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-27 20:01:38 +08:00
|
|
|
static void copyMakeConstBorder_8u( const uchar* src, size_t srcstep, Size srcroi,
|
|
|
|
uchar* dst, size_t dststep, Size dstroi,
|
2010-12-12 05:50:31 +08:00
|
|
|
int top, int left, int cn, const uchar* value )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-12-12 05:50:31 +08:00
|
|
|
int i, j;
|
|
|
|
AutoBuffer<uchar> _constBuf(dstroi.width*cn);
|
|
|
|
uchar* constBuf = _constBuf;
|
|
|
|
int right = dstroi.width - srcroi.width - left;
|
|
|
|
int bottom = dstroi.height - srcroi.height - top;
|
|
|
|
|
|
|
|
for( i = 0; i < dstroi.width; i++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-12-12 05:50:31 +08:00
|
|
|
for( j = 0; j < cn; j++ )
|
|
|
|
constBuf[i*cn + j] = value[j];
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2010-12-12 05:50:31 +08:00
|
|
|
|
|
|
|
srcroi.width *= cn;
|
|
|
|
dstroi.width *= cn;
|
|
|
|
left *= cn;
|
|
|
|
right *= cn;
|
|
|
|
|
|
|
|
uchar* dstInner = dst + dststep*top + left;
|
|
|
|
|
|
|
|
for( i = 0; i < srcroi.height; i++, dstInner += dststep, src += srcstep )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-12-12 05:50:31 +08:00
|
|
|
if( dstInner != src )
|
|
|
|
memcpy( dstInner, src, srcroi.width );
|
|
|
|
memcpy( dstInner - left, constBuf, left );
|
2010-12-14 00:53:46 +08:00
|
|
|
memcpy( dstInner + srcroi.width, constBuf, right );
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2010-12-12 05:50:31 +08:00
|
|
|
|
|
|
|
dst += dststep*top;
|
|
|
|
|
|
|
|
for( i = 0; i < top; i++ )
|
|
|
|
memcpy(dst + (i - top)*dststep, constBuf, dstroi.width);
|
|
|
|
|
|
|
|
for( i = 0; i < bottom; i++ )
|
|
|
|
memcpy(dst + (i + srcroi.height)*dststep, constBuf, dstroi.width);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
}
|
2010-12-12 05:50:31 +08:00
|
|
|
|
2011-06-06 22:51:27 +08:00
|
|
|
void cv::copyMakeBorder( InputArray _src, OutputArray _dst, int top, int bottom,
|
2011-04-17 21:14:45 +08:00
|
|
|
int left, int right, int borderType, const Scalar& value )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2011-04-17 21:14:45 +08:00
|
|
|
Mat src = _src.getMat();
|
2010-12-12 05:50:31 +08:00
|
|
|
CV_Assert( top >= 0 && bottom >= 0 && left >= 0 && right >= 0 );
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2011-11-08 20:01:49 +08:00
|
|
|
if( src.isSubmatrix() && (borderType & BORDER_ISOLATED) == 0 )
|
|
|
|
{
|
|
|
|
Size wholeSize;
|
|
|
|
Point ofs;
|
|
|
|
src.locateROI(wholeSize, ofs);
|
|
|
|
int dtop = std::min(ofs.y, top);
|
|
|
|
int dbottom = std::min(wholeSize.height - src.rows - ofs.y, bottom);
|
|
|
|
int dleft = std::min(ofs.x, left);
|
|
|
|
int dright = std::min(wholeSize.width - src.cols - ofs.x, right);
|
|
|
|
src.adjustROI(dtop, dbottom, dleft, dright);
|
|
|
|
top -= dtop;
|
|
|
|
left -= dleft;
|
2011-12-26 16:05:33 +08:00
|
|
|
bottom -= dbottom;
|
|
|
|
right -= dright;
|
2011-11-08 20:01:49 +08:00
|
|
|
}
|
2012-05-18 21:18:37 +08:00
|
|
|
|
|
|
|
_dst.create( src.rows + top + bottom, src.cols + left + right, src.type() );
|
|
|
|
Mat dst = _dst.getMat();
|
|
|
|
|
|
|
|
if(top == 0 && left == 0 && bottom == 0 && right == 0)
|
|
|
|
{
|
|
|
|
if(src.data != dst.data)
|
|
|
|
src.copyTo(dst);
|
|
|
|
return;
|
|
|
|
}
|
2011-11-08 20:01:49 +08:00
|
|
|
|
|
|
|
borderType &= ~BORDER_ISOLATED;
|
|
|
|
|
2010-12-12 05:50:31 +08:00
|
|
|
if( borderType != BORDER_CONSTANT )
|
|
|
|
copyMakeBorder_8u( src.data, src.step, src.size(),
|
|
|
|
dst.data, dst.step, dst.size(),
|
2010-12-27 20:01:38 +08:00
|
|
|
top, left, (int)src.elemSize(), borderType );
|
2010-12-12 05:50:31 +08:00
|
|
|
else
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2011-07-12 07:03:05 +08:00
|
|
|
int cn = src.channels(), cn1 = cn;
|
|
|
|
AutoBuffer<double> buf(cn);
|
|
|
|
if( cn > 4 )
|
|
|
|
{
|
|
|
|
CV_Assert( value[0] == value[1] && value[0] == value[2] && value[0] == value[3] );
|
|
|
|
cn1 = 1;
|
|
|
|
}
|
|
|
|
scalarToRawData(value, buf, CV_MAKETYPE(src.depth(), cn1), cn);
|
2010-12-12 05:50:31 +08:00
|
|
|
copyMakeConstBorder_8u( src.data, src.step, src.size(),
|
|
|
|
dst.data, dst.step, dst.size(),
|
2011-07-12 07:03:05 +08:00
|
|
|
top, left, (int)src.elemSize(), (uchar*)(double*)buf );
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2010-12-12 05:50:31 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
|
2012-04-25 22:31:54 +08:00
|
|
|
double cv::PSNR(InputArray _src1, InputArray _src2)
|
|
|
|
{
|
|
|
|
Mat src1 = _src1.getMat(), src2 = _src2.getMat();
|
|
|
|
CV_Assert( src1.depth() == CV_8U );
|
|
|
|
double diff = std::sqrt(norm(src1, src2, NORM_L2SQR)/(src1.total()*src1.channels()));
|
|
|
|
return 20*log10(255./(diff+DBL_EPSILON));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-12-12 05:50:31 +08:00
|
|
|
CV_IMPL void
|
|
|
|
cvCopyMakeBorder( const CvArr* srcarr, CvArr* dstarr, CvPoint offset,
|
|
|
|
int borderType, CvScalar value )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2010-12-12 05:50:31 +08:00
|
|
|
cv::Mat src = cv::cvarrToMat(srcarr), dst = cv::cvarrToMat(dstarr);
|
|
|
|
int left = offset.x, right = dst.cols - src.cols - left;
|
|
|
|
int top = offset.y, bottom = dst.rows - src.rows - top;
|
|
|
|
|
|
|
|
CV_Assert( dst.type() == src.type() );
|
|
|
|
cv::copyMakeBorder( src, dst, top, bottom, left, right, borderType, value );
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* End of file. */
|