utils.cpp 9.39 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/*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;
50
    CvMat hdr;
51
    CvMat* mat = (CvMat*)arr;
52

53
    if( !CV_IS_MAT( mat ))
54
        CV_Error( CV_StsBadArg, "Input array is not a valid matrix" );
55

56 57 58
    if( CV_MAT_CN(mat->type) == 1 && mat->width == 2 )
        mat = cvReshape(mat, &hdr, 2);

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
    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;
}

78 79
namespace cv
{
80

81 82
static void copyMakeBorder_8u( const uchar* src, size_t srcstep, Size srcroi,
                               uchar* dst, size_t dststep, Size dstroi,
83
                               int top, int left, int cn, int borderType )
84 85
{
    const int isz = (int)sizeof(int);
86 87
    int i, j, k, elemSize = 1;
    bool intMode = false;
88 89 90 91

    if( (cn | srcstep | dststep | (size_t)src | (size_t)dst) % isz == 0 )
    {
        cn /= isz;
92 93
        elemSize = isz;
        intMode = true;
94 95
    }

96 97 98 99
    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;
100

101 102 103 104 105
    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;
106
    }
107

108
    for( i = 0; i < right; i++ )
109
    {
110 111 112
        j = borderInterpolate(srcroi.width + i, srcroi.width, borderType)*cn;
        for( k = 0; k < cn; k++ )
            tab[(i+left)*cn + k] = j + k;
113 114 115 116 117
    }

    srcroi.width *= cn;
    dstroi.width *= cn;
    left *= cn;
118
    right *= cn;
119

120
    uchar* dstInner = dst + dststep*top + left*elemSize;
121

122
    for( i = 0; i < srcroi.height; i++, dstInner += dststep, src += srcstep )
123
    {
124 125
        if( dstInner != src )
            memcpy(dstInner, src, srcroi.width*elemSize);
126

127
        if( intMode )
128
        {
129 130 131 132 133 134
            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]];
135
        }
136
        else
137 138
        {
            for( j = 0; j < left; j++ )
139 140 141
                dstInner[j - left] = src[tab[j]];
            for( j = 0; j < right; j++ )
                dstInner[j + srcroi.width] = src[tab[j + left]];
142 143
        }
    }
144

145 146
    dstroi.width *= elemSize;
    dst += dststep*top;
147

148
    for( i = 0; i < top; i++ )
149
    {
150 151
        j = borderInterpolate(i - top, srcroi.height, borderType);
        memcpy(dst + (i - top)*dststep, dst + j*dststep, dstroi.width);
152
    }
153

154
    for( i = 0; i < bottom; i++ )
155
    {
156 157
        j = borderInterpolate(i + srcroi.height, srcroi.height, borderType);
        memcpy(dst + (i + srcroi.height)*dststep, dst + j*dststep, dstroi.width);
158 159 160 161
    }
}


162 163
static void copyMakeConstBorder_8u( const uchar* src, size_t srcstep, Size srcroi,
                                    uchar* dst, size_t dststep, Size dstroi,
164
                                    int top, int left, int cn, const uchar* value )
165
{
166 167 168 169 170
    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;
171

172
    for( i = 0; i < dstroi.width; i++ )
173
    {
174 175
        for( j = 0; j < cn; j++ )
            constBuf[i*cn + j] = value[j];
176
    }
177

178 179 180 181
    srcroi.width *= cn;
    dstroi.width *= cn;
    left *= cn;
    right *= cn;
182

183
    uchar* dstInner = dst + dststep*top + left;
184

185
    for( i = 0; i < srcroi.height; i++, dstInner += dststep, src += srcstep )
186
    {
187 188 189
        if( dstInner != src )
            memcpy( dstInner, src, srcroi.width );
        memcpy( dstInner - left, constBuf, left );
190
        memcpy( dstInner + srcroi.width, constBuf, right );
191
    }
192

193
    dst += dststep*top;
194

195 196
    for( i = 0; i < top; i++ )
        memcpy(dst + (i - top)*dststep, constBuf, dstroi.width);
197

198 199
    for( i = 0; i < bottom; i++ )
        memcpy(dst + (i + srcroi.height)*dststep, constBuf, dstroi.width);
200 201
}

202
}
203

204
void cv::copyMakeBorder( InputArray _src, OutputArray _dst, int top, int bottom,
205
                         int left, int right, int borderType, const Scalar& value )
206
{
207
    Mat src = _src.getMat();
208
    CV_Assert( top >= 0 && bottom >= 0 && left >= 0 && right >= 0 );
209

210 211 212 213 214 215 216 217 218 219 220 221
    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;
222 223
        bottom -= dbottom;
        right -= dright;
224
    }
225 226 227 228 229 230

    _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)
    {
231
        if(src.data != dst.data || src.step != dst.step)
232 233 234
            src.copyTo(dst);
        return;
    }
235

236
    borderType &= ~BORDER_ISOLATED;
237

238 239 240
    if( borderType != BORDER_CONSTANT )
        copyMakeBorder_8u( src.data, src.step, src.size(),
                           dst.data, dst.step, dst.size(),
241
                           top, left, (int)src.elemSize(), borderType );
242
    else
243
    {
244 245 246 247 248 249 250 251
        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);
252 253
        copyMakeConstBorder_8u( src.data, src.step, src.size(),
                                dst.data, dst.step, dst.size(),
254
                                top, left, (int)src.elemSize(), (uchar*)(double*)buf );
255
    }
256
}
257 258


259 260 261 262 263 264 265 266 267
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));
}


268 269 270
CV_IMPL void
cvCopyMakeBorder( const CvArr* srcarr, CvArr* dstarr, CvPoint offset,
                  int borderType, CvScalar value )
271
{
272 273 274
    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;
275

276 277
    CV_Assert( dst.type() == src.type() );
    cv::copyMakeBorder( src, dst, top, bottom, left, right, borderType, value );
278 279 280
}

/* End of file. */