fixed problem with Nx2 numpy arrays in geometrical functions (#2783)
[profile/ivi/opencv.git] / modules / imgproc / src / utils.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 //                        Intel License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
15 //
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
18 //
19 //   * Redistribution's of source code must retain the above copyright notice,
20 //     this list of conditions and the following disclaimer.
21 //
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.
25 //
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.
28 //
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.
39 //
40 //M*/
41
42 #include "precomp.hpp"
43
44 CV_IMPL CvSeq* cvPointSeqFromMat( int seq_kind, const CvArr* arr,
45                                   CvContour* contour_header, CvSeqBlock* block )
46 {
47     CV_Assert( arr != 0 && contour_header != 0 && block != 0 );
48
49     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     if( CV_MAT_CN(mat->type) == 1 && mat->width == 2 )
57         mat = cvReshape(mat, &hdr, 2);
58
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" );
64
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" );
69
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 );
74
75     return (CvSeq*)contour_header;
76 }
77
78 namespace cv
79 {
80
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 )
84 {
85     const int isz = (int)sizeof(int);
86     int i, j, k, elemSize = 1;
87     bool intMode = false;
88
89     if( (cn | srcstep | dststep | (size_t)src | (size_t)dst) % isz == 0 )
90     {
91         cn /= isz;
92         elemSize = isz;
93         intMode = true;
94     }
95
96     AutoBuffer<int> _tab((dstroi.width - srcroi.width)*cn);
97     int* tab = _tab;
98     int right = dstroi.width - srcroi.width - left;
99     int bottom = dstroi.height - srcroi.height - top;
100
101     for( i = 0; i < left; i++ )
102     {
103         j = borderInterpolate(i - left, srcroi.width, borderType)*cn;
104         for( k = 0; k < cn; k++ )
105             tab[i*cn + k] = j + k;
106     }
107
108     for( i = 0; i < right; i++ )
109     {
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;
113     }
114
115     srcroi.width *= cn;
116     dstroi.width *= cn;
117     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         if( dstInner != src )
125             memcpy(dstInner, src, srcroi.width*elemSize);
126
127         if( intMode )
128         {
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]];
135         }
136         else
137         {
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]];
142         }
143     }
144
145     dstroi.width *= elemSize;
146     dst += dststep*top;
147
148     for( i = 0; i < top; i++ )
149     {
150         j = borderInterpolate(i - top, srcroi.height, borderType);
151         memcpy(dst + (i - top)*dststep, dst + j*dststep, dstroi.width);
152     }
153
154     for( i = 0; i < bottom; i++ )
155     {
156         j = borderInterpolate(i + srcroi.height, srcroi.height, borderType);
157         memcpy(dst + (i + srcroi.height)*dststep, dst + j*dststep, dstroi.width);
158     }
159 }
160
161
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 )
165 {
166     int i, j;
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;
171
172     for( i = 0; i < dstroi.width; i++ )
173     {
174         for( j = 0; j < cn; j++ )
175             constBuf[i*cn + j] = value[j];
176     }
177
178     srcroi.width *= cn;
179     dstroi.width *= cn;
180     left *= cn;
181     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         if( dstInner != src )
188             memcpy( dstInner, src, srcroi.width );
189         memcpy( dstInner - left, constBuf, left );
190         memcpy( dstInner + srcroi.width, constBuf, right );
191     }
192
193     dst += dststep*top;
194
195     for( i = 0; i < top; i++ )
196         memcpy(dst + (i - top)*dststep, constBuf, dstroi.width);
197
198     for( i = 0; i < bottom; i++ )
199         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     if( src.isSubmatrix() && (borderType & BORDER_ISOLATED) == 0 )
211     {
212         Size wholeSize;
213         Point ofs;
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);
220         top -= dtop;
221         left -= dleft;
222         bottom -= dbottom;
223         right -= dright;
224     }
225
226     _dst.create( src.rows + top + bottom, src.cols + left + right, src.type() );
227     Mat dst = _dst.getMat();
228
229     if(top == 0 && left == 0 && bottom == 0 && right == 0)
230     {
231         if(src.data != dst.data || src.step != dst.step)
232             src.copyTo(dst);
233         return;
234     }
235
236     borderType &= ~BORDER_ISOLATED;
237
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 );
242     else
243     {
244         int cn = src.channels(), cn1 = cn;
245         AutoBuffer<double> buf(cn);
246         if( cn > 4 )
247         {
248             CV_Assert( value[0] == value[1] && value[0] == value[2] && value[0] == value[3] );
249             cn1 = 1;
250         }
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 );
255     }
256 }
257
258
259 double cv::PSNR(InputArray _src1, InputArray _src2)
260 {
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));
265 }
266
267
268 CV_IMPL void
269 cvCopyMakeBorder( const CvArr* srcarr, CvArr* dstarr, CvPoint offset,
270                   int borderType, CvScalar value )
271 {
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;
275
276     CV_Assert( dst.type() == src.type() );
277     cv::copyMakeBorder( src, dst, top, bottom, left, right, borderType, value );
278 }
279
280 /* End of file. */