1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
42 #include "precomp.hpp"
44 CV_IMPL CvSeq* cvPointSeqFromMat( int seq_kind, const CvArr* arr,
45 CvContour* contour_header, CvSeqBlock* block )
47 CV_Assert( arr != 0 && contour_header != 0 && block != 0 );
51 CvMat* mat = (CvMat*)arr;
53 if( !CV_IS_MAT( mat ))
54 CV_Error( CV_StsBadArg, "Input array is not a valid matrix" );
56 if( CV_MAT_CN(mat->type) == 1 && mat->width == 2 )
57 mat = cvReshape(mat, &hdr, 2);
59 eltype = CV_MAT_TYPE( mat->type );
60 if( eltype != CV_32SC2 && eltype != CV_32FC2 )
61 CV_Error( CV_StsUnsupportedFormat,
62 "The matrix can not be converted to point sequence because of "
63 "inappropriate element type" );
65 if( (mat->width != 1 && mat->height != 1) || !CV_IS_MAT_CONT(mat->type))
66 CV_Error( CV_StsBadArg,
67 "The matrix converted to point sequence must be "
68 "1-dimensional and continuous" );
70 cvMakeSeqHeaderForArray(
71 (seq_kind & (CV_SEQ_KIND_MASK|CV_SEQ_FLAG_CLOSED)) | eltype,
72 sizeof(CvContour), CV_ELEM_SIZE(eltype), mat->data.ptr,
73 mat->width*mat->height, (CvSeq*)contour_header, block );
75 return (CvSeq*)contour_header;
81 static void copyMakeBorder_8u( const uchar* src, size_t srcstep, Size srcroi,
82 uchar* dst, size_t dststep, Size dstroi,
83 int top, int left, int cn, int borderType )
85 const int isz = (int)sizeof(int);
86 int i, j, k, elemSize = 1;
89 if( (cn | srcstep | dststep | (size_t)src | (size_t)dst) % isz == 0 )
96 AutoBuffer<int> _tab((dstroi.width - srcroi.width)*cn);
98 int right = dstroi.width - srcroi.width - left;
99 int bottom = dstroi.height - srcroi.height - top;
101 for( i = 0; i < left; i++ )
103 j = borderInterpolate(i - left, srcroi.width, borderType)*cn;
104 for( k = 0; k < cn; k++ )
105 tab[i*cn + k] = j + k;
108 for( i = 0; i < right; i++ )
110 j = borderInterpolate(srcroi.width + i, srcroi.width, borderType)*cn;
111 for( k = 0; k < cn; k++ )
112 tab[(i+left)*cn + k] = j + k;
120 uchar* dstInner = dst + dststep*top + left*elemSize;
122 for( i = 0; i < srcroi.height; i++, dstInner += dststep, src += srcstep )
124 if( dstInner != src )
125 memcpy(dstInner, src, srcroi.width*elemSize);
129 const int* isrc = (int*)src;
130 int* idstInner = (int*)dstInner;
131 for( j = 0; j < left; j++ )
132 idstInner[j - left] = isrc[tab[j]];
133 for( j = 0; j < right; j++ )
134 idstInner[j + srcroi.width] = isrc[tab[j + left]];
138 for( j = 0; j < left; j++ )
139 dstInner[j - left] = src[tab[j]];
140 for( j = 0; j < right; j++ )
141 dstInner[j + srcroi.width] = src[tab[j + left]];
145 dstroi.width *= elemSize;
148 for( i = 0; i < top; i++ )
150 j = borderInterpolate(i - top, srcroi.height, borderType);
151 memcpy(dst + (i - top)*dststep, dst + j*dststep, dstroi.width);
154 for( i = 0; i < bottom; i++ )
156 j = borderInterpolate(i + srcroi.height, srcroi.height, borderType);
157 memcpy(dst + (i + srcroi.height)*dststep, dst + j*dststep, dstroi.width);
162 static void copyMakeConstBorder_8u( const uchar* src, size_t srcstep, Size srcroi,
163 uchar* dst, size_t dststep, Size dstroi,
164 int top, int left, int cn, const uchar* value )
167 AutoBuffer<uchar> _constBuf(dstroi.width*cn);
168 uchar* constBuf = _constBuf;
169 int right = dstroi.width - srcroi.width - left;
170 int bottom = dstroi.height - srcroi.height - top;
172 for( i = 0; i < dstroi.width; i++ )
174 for( j = 0; j < cn; j++ )
175 constBuf[i*cn + j] = value[j];
183 uchar* dstInner = dst + dststep*top + left;
185 for( i = 0; i < srcroi.height; i++, dstInner += dststep, src += srcstep )
187 if( dstInner != src )
188 memcpy( dstInner, src, srcroi.width );
189 memcpy( dstInner - left, constBuf, left );
190 memcpy( dstInner + srcroi.width, constBuf, right );
195 for( i = 0; i < top; i++ )
196 memcpy(dst + (i - top)*dststep, constBuf, dstroi.width);
198 for( i = 0; i < bottom; i++ )
199 memcpy(dst + (i + srcroi.height)*dststep, constBuf, dstroi.width);
204 void cv::copyMakeBorder( InputArray _src, OutputArray _dst, int top, int bottom,
205 int left, int right, int borderType, const Scalar& value )
207 Mat src = _src.getMat();
208 CV_Assert( top >= 0 && bottom >= 0 && left >= 0 && right >= 0 );
210 if( src.isSubmatrix() && (borderType & BORDER_ISOLATED) == 0 )
214 src.locateROI(wholeSize, ofs);
215 int dtop = std::min(ofs.y, top);
216 int dbottom = std::min(wholeSize.height - src.rows - ofs.y, bottom);
217 int dleft = std::min(ofs.x, left);
218 int dright = std::min(wholeSize.width - src.cols - ofs.x, right);
219 src.adjustROI(dtop, dbottom, dleft, dright);
226 _dst.create( src.rows + top + bottom, src.cols + left + right, src.type() );
227 Mat dst = _dst.getMat();
229 if(top == 0 && left == 0 && bottom == 0 && right == 0)
231 if(src.data != dst.data || src.step != dst.step)
236 borderType &= ~BORDER_ISOLATED;
238 if( borderType != BORDER_CONSTANT )
239 copyMakeBorder_8u( src.data, src.step, src.size(),
240 dst.data, dst.step, dst.size(),
241 top, left, (int)src.elemSize(), borderType );
244 int cn = src.channels(), cn1 = cn;
245 AutoBuffer<double> buf(cn);
248 CV_Assert( value[0] == value[1] && value[0] == value[2] && value[0] == value[3] );
251 scalarToRawData(value, buf, CV_MAKETYPE(src.depth(), cn1), cn);
252 copyMakeConstBorder_8u( src.data, src.step, src.size(),
253 dst.data, dst.step, dst.size(),
254 top, left, (int)src.elemSize(), (uchar*)(double*)buf );
259 double cv::PSNR(InputArray _src1, InputArray _src2)
261 Mat src1 = _src1.getMat(), src2 = _src2.getMat();
262 CV_Assert( src1.depth() == CV_8U );
263 double diff = std::sqrt(norm(src1, src2, NORM_L2SQR)/(src1.total()*src1.channels()));
264 return 20*log10(255./(diff+DBL_EPSILON));
269 cvCopyMakeBorder( const CvArr* srcarr, CvArr* dstarr, CvPoint offset,
270 int borderType, CvScalar value )
272 cv::Mat src = cv::cvarrToMat(srcarr), dst = cv::cvarrToMat(dstarr);
273 int left = offset.x, right = dst.cols - src.cols - left;
274 int top = offset.y, bottom = dst.rows - src.rows - top;
276 CV_Assert( dst.type() == src.type() );
277 cv::copyMakeBorder( src, dst, top, bottom, left, right, borderType, value );