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.
|
|
|
|
//
|
|
|
|
// License Agreement
|
|
|
|
// For Open Source Computer Vision Library
|
|
|
|
//
|
|
|
|
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
|
2011-04-17 21:14:45 +08:00
|
|
|
// Copyright (C) 2009-2011, Willow Garage Inc., all rights reserved.
|
2010-05-12 01:44:00 +08:00
|
|
|
// 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 the copyright holders 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*/
|
|
|
|
|
|
|
|
/* ////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// Mat basic operations: Copy, Set
|
|
|
|
//
|
|
|
|
// */
|
|
|
|
|
|
|
|
#include "precomp.hpp"
|
2014-08-01 22:11:20 +08:00
|
|
|
#include "opencl_kernels_core.hpp"
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
namespace cv
|
|
|
|
{
|
|
|
|
|
|
|
|
template<typename T> static void
|
2011-04-17 21:14:45 +08:00
|
|
|
copyMask_(const uchar* _src, size_t sstep, const uchar* mask, size_t mstep, uchar* _dst, size_t dstep, Size size)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2011-04-17 21:14:45 +08:00
|
|
|
for( ; size.height--; mask += mstep, _src += sstep, _dst += dstep )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2011-04-17 21:14:45 +08:00
|
|
|
const T* src = (const T*)_src;
|
|
|
|
T* dst = (T*)_dst;
|
2010-05-12 01:44:00 +08:00
|
|
|
int x = 0;
|
2012-06-09 23:00:04 +08:00
|
|
|
#if CV_ENABLE_UNROLLED
|
2010-05-12 01:44:00 +08:00
|
|
|
for( ; x <= size.width - 4; x += 4 )
|
|
|
|
{
|
|
|
|
if( mask[x] )
|
|
|
|
dst[x] = src[x];
|
|
|
|
if( mask[x+1] )
|
|
|
|
dst[x+1] = src[x+1];
|
|
|
|
if( mask[x+2] )
|
|
|
|
dst[x+2] = src[x+2];
|
|
|
|
if( mask[x+3] )
|
|
|
|
dst[x+3] = src[x+3];
|
|
|
|
}
|
2012-02-10 14:05:04 +08:00
|
|
|
#endif
|
2010-05-12 01:44:00 +08:00
|
|
|
for( ; x < size.width; x++ )
|
|
|
|
if( mask[x] )
|
|
|
|
dst[x] = src[x];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-24 22:25:46 +08:00
|
|
|
template<> void
|
2012-07-24 21:24:31 +08:00
|
|
|
copyMask_<uchar>(const uchar* _src, size_t sstep, const uchar* mask, size_t mstep, uchar* _dst, size_t dstep, Size size)
|
|
|
|
{
|
2014-04-28 23:31:18 +08:00
|
|
|
#if defined HAVE_IPP
|
2014-04-13 20:44:37 +08:00
|
|
|
if (ippiCopy_8u_C1MR(_src, (int)sstep, _dst, (int)dstep, ippiSize(size), mask, (int)mstep) >= 0)
|
|
|
|
return;
|
2014-04-18 18:25:38 +08:00
|
|
|
setIppErrorStatus();
|
2014-04-13 20:44:37 +08:00
|
|
|
#endif
|
|
|
|
|
2012-07-24 21:24:31 +08:00
|
|
|
for( ; size.height--; mask += mstep, _src += sstep, _dst += dstep )
|
|
|
|
{
|
|
|
|
const uchar* src = (const uchar*)_src;
|
|
|
|
uchar* dst = (uchar*)_dst;
|
|
|
|
int x = 0;
|
|
|
|
#if CV_SSE4_2
|
2012-10-17 15:12:04 +08:00
|
|
|
if(USE_SSE4_2)//
|
|
|
|
{
|
|
|
|
__m128i zero = _mm_setzero_si128 ();
|
|
|
|
|
|
|
|
for( ; x <= size.width - 16; x += 16 )
|
|
|
|
{
|
|
|
|
const __m128i rSrc = _mm_lddqu_si128((const __m128i*)(src+x));
|
|
|
|
__m128i _mask = _mm_lddqu_si128((const __m128i*)(mask+x));
|
|
|
|
__m128i rDst = _mm_lddqu_si128((__m128i*)(dst+x));
|
|
|
|
__m128i _negMask = _mm_cmpeq_epi8(_mask, zero);
|
|
|
|
rDst = _mm_blendv_epi8(rSrc, rDst, _negMask);
|
|
|
|
_mm_storeu_si128((__m128i*)(dst + x), rDst);
|
|
|
|
}
|
|
|
|
}
|
2012-07-24 21:24:31 +08:00
|
|
|
#endif
|
|
|
|
for( ; x < size.width; x++ )
|
|
|
|
if( mask[x] )
|
|
|
|
dst[x] = src[x];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-24 22:25:46 +08:00
|
|
|
template<> void
|
2012-07-24 21:24:31 +08:00
|
|
|
copyMask_<ushort>(const uchar* _src, size_t sstep, const uchar* mask, size_t mstep, uchar* _dst, size_t dstep, Size size)
|
|
|
|
{
|
2014-04-28 23:31:18 +08:00
|
|
|
#if defined HAVE_IPP
|
2014-04-13 20:44:37 +08:00
|
|
|
if (ippiCopy_16u_C1MR((const Ipp16u *)_src, (int)sstep, (Ipp16u *)_dst, (int)dstep, ippiSize(size), mask, (int)mstep) >= 0)
|
|
|
|
return;
|
2014-04-18 18:25:38 +08:00
|
|
|
setIppErrorStatus();
|
2014-04-13 20:44:37 +08:00
|
|
|
#endif
|
|
|
|
|
2012-07-24 21:24:31 +08:00
|
|
|
for( ; size.height--; mask += mstep, _src += sstep, _dst += dstep )
|
|
|
|
{
|
2012-10-17 15:12:04 +08:00
|
|
|
const ushort* src = (const ushort*)_src;
|
2012-07-24 21:24:31 +08:00
|
|
|
ushort* dst = (ushort*)_dst;
|
|
|
|
int x = 0;
|
|
|
|
#if CV_SSE4_2
|
2012-10-17 15:12:04 +08:00
|
|
|
if(USE_SSE4_2)//
|
|
|
|
{
|
|
|
|
__m128i zero = _mm_setzero_si128 ();
|
|
|
|
for( ; x <= size.width - 8; x += 8 )
|
|
|
|
{
|
|
|
|
const __m128i rSrc =_mm_lddqu_si128((const __m128i*)(src+x));
|
|
|
|
__m128i _mask = _mm_loadl_epi64((const __m128i*)(mask+x));
|
|
|
|
_mask = _mm_unpacklo_epi8(_mask, _mask);
|
|
|
|
__m128i rDst = _mm_lddqu_si128((const __m128i*)(dst+x));
|
|
|
|
__m128i _negMask = _mm_cmpeq_epi8(_mask, zero);
|
|
|
|
rDst = _mm_blendv_epi8(rSrc, rDst, _negMask);
|
|
|
|
_mm_storeu_si128((__m128i*)(dst + x), rDst);
|
|
|
|
}
|
|
|
|
}
|
2012-07-24 21:24:31 +08:00
|
|
|
#endif
|
|
|
|
for( ; x < size.width; x++ )
|
|
|
|
if( mask[x] )
|
|
|
|
dst[x] = src[x];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
static void
|
|
|
|
copyMaskGeneric(const uchar* _src, size_t sstep, const uchar* mask, size_t mstep, uchar* _dst, size_t dstep, Size size, void* _esz)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2011-04-17 21:14:45 +08:00
|
|
|
size_t k, esz = *(size_t*)_esz;
|
|
|
|
for( ; size.height--; mask += mstep, _src += sstep, _dst += dstep )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2011-04-17 21:14:45 +08:00
|
|
|
const uchar* src = _src;
|
|
|
|
uchar* dst = _dst;
|
|
|
|
int x = 0;
|
|
|
|
for( ; x < size.width; x++, src += esz, dst += esz )
|
|
|
|
{
|
|
|
|
if( !mask[x] )
|
|
|
|
continue;
|
|
|
|
for( k = 0; k < esz; k++ )
|
|
|
|
dst[k] = src[k];
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
2011-04-17 21:14:45 +08:00
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
|
|
|
|
2011-06-14 04:56:27 +08:00
|
|
|
#define DEF_COPY_MASK(suffix, type) \
|
2011-04-17 21:14:45 +08:00
|
|
|
static void copyMask##suffix(const uchar* src, size_t sstep, const uchar* mask, size_t mstep, \
|
|
|
|
uchar* dst, size_t dstep, Size size, void*) \
|
|
|
|
{ \
|
|
|
|
copyMask_<type>(src, sstep, mask, mstep, dst, dstep, size); \
|
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2014-04-28 23:31:18 +08:00
|
|
|
#if defined HAVE_IPP
|
2014-04-13 20:44:37 +08:00
|
|
|
#define DEF_COPY_MASK_F(suffix, type, ippfavor, ipptype) \
|
|
|
|
static void copyMask##suffix(const uchar* src, size_t sstep, const uchar* mask, size_t mstep, \
|
|
|
|
uchar* dst, size_t dstep, Size size, void*) \
|
|
|
|
{ \
|
|
|
|
if (ippiCopy_##ippfavor((const ipptype *)src, (int)sstep, (ipptype *)dst, (int)dstep, ippiSize(size), (const Ipp8u *)mask, (int)mstep) >= 0) \
|
|
|
|
return; \
|
2014-04-18 18:25:38 +08:00
|
|
|
setIppErrorStatus(); \
|
2014-04-13 20:44:37 +08:00
|
|
|
copyMask_<type>(src, sstep, mask, mstep, dst, dstep, size); \
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#define DEF_COPY_MASK_F(suffix, type, ippfavor, ipptype) \
|
|
|
|
static void copyMask##suffix(const uchar* src, size_t sstep, const uchar* mask, size_t mstep, \
|
|
|
|
uchar* dst, size_t dstep, Size size, void*) \
|
|
|
|
{ \
|
|
|
|
copyMask_<type>(src, sstep, mask, mstep, dst, dstep, size); \
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2014-01-07 06:38:41 +08:00
|
|
|
DEF_COPY_MASK(8u, uchar)
|
|
|
|
DEF_COPY_MASK(16u, ushort)
|
2014-04-13 20:44:37 +08:00
|
|
|
DEF_COPY_MASK_F(8uC3, Vec3b, 8u_C3MR, Ipp8u)
|
|
|
|
DEF_COPY_MASK_F(32s, int, 32s_C1MR, Ipp32s)
|
|
|
|
DEF_COPY_MASK_F(16uC3, Vec3s, 16u_C3MR, Ipp16u)
|
2014-01-07 06:38:41 +08:00
|
|
|
DEF_COPY_MASK(32sC2, Vec2i)
|
2014-04-13 20:44:37 +08:00
|
|
|
DEF_COPY_MASK_F(32sC3, Vec3i, 32s_C3MR, Ipp32s)
|
|
|
|
DEF_COPY_MASK_F(32sC4, Vec4i, 32s_C4MR, Ipp32s)
|
2014-01-07 06:38:41 +08:00
|
|
|
DEF_COPY_MASK(32sC6, Vec6i)
|
|
|
|
DEF_COPY_MASK(32sC8, Vec8i)
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
BinaryFunc copyMaskTab[] =
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
0,
|
2011-04-17 21:14:45 +08:00
|
|
|
copyMask8u,
|
|
|
|
copyMask16u,
|
|
|
|
copyMask8uC3,
|
|
|
|
copyMask32s,
|
2010-05-12 01:44:00 +08:00
|
|
|
0,
|
2011-04-17 21:14:45 +08:00
|
|
|
copyMask16uC3,
|
2010-05-12 01:44:00 +08:00
|
|
|
0,
|
2011-04-17 21:14:45 +08:00
|
|
|
copyMask32sC2,
|
2010-05-12 01:44:00 +08:00
|
|
|
0, 0, 0,
|
2011-04-17 21:14:45 +08:00
|
|
|
copyMask32sC3,
|
2010-05-12 01:44:00 +08:00
|
|
|
0, 0, 0,
|
2011-04-17 21:14:45 +08:00
|
|
|
copyMask32sC4,
|
2010-05-12 01:44:00 +08:00
|
|
|
0, 0, 0, 0, 0, 0, 0,
|
2011-04-17 21:14:45 +08:00
|
|
|
copyMask32sC6,
|
2010-05-12 01:44:00 +08:00
|
|
|
0, 0, 0, 0, 0, 0, 0,
|
2011-04-17 21:14:45 +08:00
|
|
|
copyMask32sC8
|
2010-05-12 01:44:00 +08:00
|
|
|
};
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
BinaryFunc getCopyMaskFunc(size_t esz)
|
|
|
|
{
|
|
|
|
return esz <= 32 && copyMaskTab[esz] ? copyMaskTab[esz] : copyMaskGeneric;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
|
|
|
|
/* dst = src */
|
2011-04-17 21:14:45 +08:00
|
|
|
void Mat::copyTo( OutputArray _dst ) const
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2011-04-17 21:14:45 +08:00
|
|
|
int dtype = _dst.type();
|
|
|
|
if( _dst.fixedType() && dtype != type() )
|
|
|
|
{
|
2012-09-04 17:34:52 +08:00
|
|
|
CV_Assert( channels() == CV_MAT_CN(dtype) );
|
2011-04-17 21:14:45 +08:00
|
|
|
convertTo( _dst, dtype );
|
|
|
|
return;
|
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
if( empty() )
|
|
|
|
{
|
|
|
|
_dst.release();
|
2010-05-12 01:44:00 +08:00
|
|
|
return;
|
2011-04-17 21:14:45 +08:00
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2013-11-19 00:48:00 +08:00
|
|
|
if( _dst.isUMat() )
|
|
|
|
{
|
|
|
|
_dst.create( dims, size.p, type() );
|
|
|
|
UMat dst = _dst.getUMat();
|
|
|
|
|
|
|
|
size_t i, sz[CV_MAX_DIM], dstofs[CV_MAX_DIM], esz = elemSize();
|
|
|
|
for( i = 0; i < (size_t)dims; i++ )
|
|
|
|
sz[i] = size.p[i];
|
|
|
|
sz[dims-1] *= esz;
|
|
|
|
dst.ndoffset(dstofs);
|
|
|
|
dstofs[dims-1] *= esz;
|
|
|
|
dst.u->currAllocator->upload(dst.u, data, dims, sz, dstofs, dst.step.p, step.p);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
if( dims <= 2 )
|
2010-10-12 20:31:40 +08:00
|
|
|
{
|
2011-04-17 21:14:45 +08:00
|
|
|
_dst.create( rows, cols, type() );
|
|
|
|
Mat dst = _dst.getMat();
|
|
|
|
if( data == dst.data )
|
|
|
|
return;
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
if( rows > 0 && cols > 0 )
|
|
|
|
{
|
|
|
|
const uchar* sptr = data;
|
|
|
|
uchar* dptr = dst.data;
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2014-01-17 18:16:22 +08:00
|
|
|
Size sz = getContinuousSize(*this, dst);
|
2011-06-30 20:06:26 +08:00
|
|
|
size_t len = sz.width*elemSize();
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2014-04-28 23:31:18 +08:00
|
|
|
#if defined HAVE_IPP
|
2014-04-13 20:50:38 +08:00
|
|
|
if (ippiCopy_8u_C1R(sptr, (int)step, dptr, (int)dst.step, ippiSize((int)len, sz.height)) >= 0)
|
|
|
|
return;
|
2014-04-18 18:25:38 +08:00
|
|
|
setIppErrorStatus();
|
2014-04-13 20:50:38 +08:00
|
|
|
#endif
|
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
for( ; sz.height--; sptr += step, dptr += dst.step )
|
2011-06-30 20:06:26 +08:00
|
|
|
memcpy( dptr, sptr, len );
|
2010-10-18 16:51:46 +08:00
|
|
|
}
|
2010-10-12 20:31:40 +08:00
|
|
|
return;
|
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
_dst.create( dims, size, type() );
|
|
|
|
Mat dst = _dst.getMat();
|
|
|
|
if( data == dst.data )
|
|
|
|
return;
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
if( total() != 0 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2011-04-17 21:14:45 +08:00
|
|
|
const Mat* arrays[] = { this, &dst };
|
|
|
|
uchar* ptrs[2];
|
|
|
|
NAryMatIterator it(arrays, ptrs, 2);
|
2012-06-09 23:00:04 +08:00
|
|
|
size_t sz = it.size*elemSize();
|
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
for( size_t i = 0; i < it.nplanes; i++, ++it )
|
2012-06-09 23:00:04 +08:00
|
|
|
memcpy(ptrs[1], ptrs[0], sz);
|
2010-10-18 16:51:46 +08:00
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2011-06-06 22:51:27 +08:00
|
|
|
void Mat::copyTo( OutputArray _dst, InputArray _mask ) const
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2011-04-17 21:14:45 +08:00
|
|
|
Mat mask = _mask.getMat();
|
2010-05-12 01:44:00 +08:00
|
|
|
if( !mask.data )
|
|
|
|
{
|
2011-04-17 21:14:45 +08:00
|
|
|
copyTo(_dst);
|
2010-05-12 01:44:00 +08:00
|
|
|
return;
|
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2011-07-24 18:34:14 +08:00
|
|
|
int cn = channels(), mcn = mask.channels();
|
|
|
|
CV_Assert( mask.depth() == CV_8U && (mcn == 1 || mcn == cn) );
|
|
|
|
bool colorMask = mcn > 1;
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2011-07-24 18:34:14 +08:00
|
|
|
size_t esz = colorMask ? elemSize1() : elemSize();
|
2011-04-17 21:14:45 +08:00
|
|
|
BinaryFunc copymask = getCopyMaskFunc(esz);
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
uchar* data0 = _dst.getMat().data;
|
|
|
|
_dst.create( dims, size, type() );
|
|
|
|
Mat dst = _dst.getMat();
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
if( dst.data != data0 ) // do not leave dst uninitialized
|
|
|
|
dst = Scalar(0);
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
if( dims <= 2 )
|
2010-10-12 20:31:40 +08:00
|
|
|
{
|
2014-01-17 18:13:21 +08:00
|
|
|
CV_Assert( size() == mask.size() );
|
2011-07-24 18:34:14 +08:00
|
|
|
Size sz = getContinuousSize(*this, dst, mask, mcn);
|
2011-04-17 21:14:45 +08:00
|
|
|
copymask(data, step, mask.data, mask.step, dst.data, dst.step, sz, &esz);
|
2010-10-12 20:31:40 +08:00
|
|
|
return;
|
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
const Mat* arrays[] = { this, &dst, &mask, 0 };
|
|
|
|
uchar* ptrs[3];
|
|
|
|
NAryMatIterator it(arrays, ptrs);
|
2011-07-24 18:34:14 +08:00
|
|
|
Size sz((int)(it.size*mcn), 1);
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
for( size_t i = 0; i < it.nplanes; i++, ++it )
|
|
|
|
copymask(ptrs[0], 0, ptrs[2], 0, ptrs[1], 0, sz, &esz);
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Mat& Mat::operator = (const Scalar& s)
|
|
|
|
{
|
2011-04-17 21:14:45 +08:00
|
|
|
const Mat* arrays[] = { this };
|
2012-06-09 23:00:04 +08:00
|
|
|
uchar* dptr;
|
|
|
|
NAryMatIterator it(arrays, &dptr, 1);
|
|
|
|
size_t elsize = it.size*elemSize();
|
2012-10-09 20:28:31 +08:00
|
|
|
const int64* is = (const int64*)&s.val[0];
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2012-10-09 20:28:31 +08:00
|
|
|
if( is[0] == 0 && is[1] == 0 && is[2] == 0 && is[3] == 0 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-04-14 22:42:21 +08:00
|
|
|
#if defined HAVE_IPP && !defined HAVE_IPP_ICV_ONLY && 0
|
2014-04-13 21:00:00 +08:00
|
|
|
if (dims <= 2 || isContinuous())
|
|
|
|
{
|
|
|
|
IppiSize roisize = { cols, rows };
|
|
|
|
if (isContinuous())
|
|
|
|
{
|
|
|
|
roisize.width = (int)total();
|
|
|
|
roisize.height = 1;
|
|
|
|
|
2014-04-14 22:42:21 +08:00
|
|
|
if (ippsZero_8u(data, static_cast<int>(roisize.width * elemSize())) >= 0)
|
2014-04-13 21:00:00 +08:00
|
|
|
return *this;
|
2014-04-18 18:25:38 +08:00
|
|
|
setIppErrorStatus();
|
2014-04-13 21:00:00 +08:00
|
|
|
}
|
2014-04-14 22:42:21 +08:00
|
|
|
roisize.width *= (int)elemSize();
|
2014-04-13 21:00:00 +08:00
|
|
|
|
|
|
|
if (ippiSet_8u_C1R(0, data, (int)step, roisize) >= 0)
|
|
|
|
return *this;
|
2014-04-18 18:25:38 +08:00
|
|
|
setIppErrorStatus();
|
2014-04-13 21:00:00 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
for( size_t i = 0; i < it.nplanes; i++, ++it )
|
2012-06-09 23:00:04 +08:00
|
|
|
memset( dptr, 0, elsize );
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-04-17 21:14:45 +08:00
|
|
|
if( it.nplanes > 0 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2011-04-17 21:14:45 +08:00
|
|
|
double scalar[12];
|
|
|
|
scalarToRawData(s, scalar, type(), 12);
|
|
|
|
size_t blockSize = 12*elemSize1();
|
2012-06-09 23:00:04 +08:00
|
|
|
|
|
|
|
for( size_t j = 0; j < elsize; j += blockSize )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2012-06-09 23:00:04 +08:00
|
|
|
size_t sz = MIN(blockSize, elsize - j);
|
|
|
|
memcpy( dptr + j, scalar, sz );
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
for( size_t i = 1; i < it.nplanes; i++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2011-04-17 21:14:45 +08:00
|
|
|
++it;
|
2012-06-09 23:00:04 +08:00
|
|
|
memcpy( dptr, data, elsize );
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2011-06-14 04:56:27 +08:00
|
|
|
Mat& Mat::setTo(InputArray _value, InputArray _mask)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2014-02-24 01:07:20 +08:00
|
|
|
if( empty() )
|
2011-06-14 04:56:27 +08:00
|
|
|
return *this;
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2011-06-14 04:56:27 +08:00
|
|
|
Mat value = _value.getMat(), mask = _mask.getMat();
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2011-06-14 04:56:27 +08:00
|
|
|
CV_Assert( checkScalar(value, type(), _value.kind(), _InputArray::MAT ));
|
2014-04-13 22:15:41 +08:00
|
|
|
CV_Assert( mask.empty() || (mask.type() == CV_8U && size == mask.size) );
|
|
|
|
|
2014-04-28 23:31:18 +08:00
|
|
|
#if defined HAVE_IPP
|
2014-04-18 18:25:38 +08:00
|
|
|
int cn = channels(), depth0 = depth();
|
|
|
|
|
|
|
|
if (!mask.empty() && (dims <= 2 || (isContinuous() && mask.isContinuous())) &&
|
2014-04-24 18:15:37 +08:00
|
|
|
(/*depth0 == CV_8U ||*/ depth0 == CV_16U || depth0 == CV_16S || depth0 == CV_32S || depth0 == CV_32F) &&
|
2014-04-18 18:25:38 +08:00
|
|
|
(cn == 1 || cn == 3 || cn == 4))
|
2014-04-13 22:15:41 +08:00
|
|
|
{
|
2014-04-14 22:42:21 +08:00
|
|
|
uchar _buf[32];
|
|
|
|
void * buf = _buf;
|
|
|
|
convertAndUnrollScalar( value, type(), _buf, 1 );
|
2014-04-13 22:15:41 +08:00
|
|
|
|
|
|
|
IppStatus status = (IppStatus)-1;
|
|
|
|
IppiSize roisize = { cols, rows };
|
2014-07-30 22:19:47 +08:00
|
|
|
int mstep = (int)mask.step[0], dstep = (int)step[0];
|
2014-04-13 22:15:41 +08:00
|
|
|
|
|
|
|
if (isContinuous() && mask.isContinuous())
|
|
|
|
{
|
|
|
|
roisize.width = (int)total();
|
|
|
|
roisize.height = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cn == 1)
|
|
|
|
{
|
2014-04-24 18:15:37 +08:00
|
|
|
/*if (depth0 == CV_8U)
|
2014-04-13 22:15:41 +08:00
|
|
|
status = ippiSet_8u_C1MR(*(Ipp8u *)buf, (Ipp8u *)data, dstep, roisize, mask.data, mstep);
|
2014-04-24 18:15:37 +08:00
|
|
|
else*/ if (depth0 == CV_16U)
|
2014-04-13 22:15:41 +08:00
|
|
|
status = ippiSet_16u_C1MR(*(Ipp16u *)buf, (Ipp16u *)data, dstep, roisize, mask.data, mstep);
|
|
|
|
else if (depth0 == CV_16S)
|
|
|
|
status = ippiSet_16s_C1MR(*(Ipp16s *)buf, (Ipp16s *)data, dstep, roisize, mask.data, mstep);
|
|
|
|
else if (depth0 == CV_32S)
|
|
|
|
status = ippiSet_32s_C1MR(*(Ipp32s *)buf, (Ipp32s *)data, dstep, roisize, mask.data, mstep);
|
|
|
|
else if (depth0 == CV_32F)
|
|
|
|
status = ippiSet_32f_C1MR(*(Ipp32f *)buf, (Ipp32f *)data, dstep, roisize, mask.data, mstep);
|
|
|
|
}
|
2014-04-18 18:25:38 +08:00
|
|
|
else if (cn == 3 || cn == 4)
|
2014-04-13 22:15:41 +08:00
|
|
|
{
|
|
|
|
#define IPP_SET(ippfavor, ippcn) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
typedef Ipp##ippfavor ipptype; \
|
2014-04-18 18:25:38 +08:00
|
|
|
ipptype ippvalue[4] = { ((ipptype *)buf)[0], ((ipptype *)buf)[1], ((ipptype *)buf)[2], ((ipptype *)buf)[3] }; \
|
2014-04-13 22:15:41 +08:00
|
|
|
status = ippiSet_##ippfavor##_C##ippcn##MR(ippvalue, (ipptype *)data, dstep, roisize, mask.data, mstep); \
|
|
|
|
} while ((void)0, 0)
|
|
|
|
|
|
|
|
#define IPP_SET_CN(ippcn) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
if (cn == ippcn) \
|
|
|
|
{ \
|
2014-04-24 18:15:37 +08:00
|
|
|
/*if (depth0 == CV_8U) \
|
2014-04-13 22:15:41 +08:00
|
|
|
IPP_SET(8u, ippcn); \
|
2014-04-24 18:15:37 +08:00
|
|
|
else*/ if (depth0 == CV_16U) \
|
2014-04-13 22:15:41 +08:00
|
|
|
IPP_SET(16u, ippcn); \
|
|
|
|
else if (depth0 == CV_16S) \
|
|
|
|
IPP_SET(16s, ippcn); \
|
|
|
|
else if (depth0 == CV_32S) \
|
|
|
|
IPP_SET(32s, ippcn); \
|
|
|
|
else if (depth0 == CV_32F) \
|
|
|
|
IPP_SET(32f, ippcn); \
|
|
|
|
} \
|
|
|
|
} while ((void)0, 0)
|
|
|
|
|
|
|
|
IPP_SET_CN(3);
|
|
|
|
IPP_SET_CN(4);
|
|
|
|
|
|
|
|
#undef IPP_SET_CN
|
|
|
|
#undef IPP_SET
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status >= 0)
|
|
|
|
return *this;
|
2014-04-18 18:25:38 +08:00
|
|
|
setIppErrorStatus();
|
2014-04-13 22:15:41 +08:00
|
|
|
}
|
|
|
|
#endif
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2011-06-14 04:56:27 +08:00
|
|
|
size_t esz = elemSize();
|
|
|
|
BinaryFunc copymask = getCopyMaskFunc(esz);
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2011-06-14 04:56:27 +08:00
|
|
|
const Mat* arrays[] = { this, !mask.empty() ? &mask : 0, 0 };
|
|
|
|
uchar* ptrs[2]={0,0};
|
|
|
|
NAryMatIterator it(arrays, ptrs);
|
2012-06-09 23:00:04 +08:00
|
|
|
int totalsz = (int)it.size, blockSize0 = std::min(totalsz, (int)((BLOCK_SIZE + esz-1)/esz));
|
2011-06-14 04:56:27 +08:00
|
|
|
AutoBuffer<uchar> _scbuf(blockSize0*esz + 32);
|
|
|
|
uchar* scbuf = alignPtr((uchar*)_scbuf, (int)sizeof(double));
|
|
|
|
convertAndUnrollScalar( value, type(), scbuf, blockSize0 );
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2011-06-14 04:56:27 +08:00
|
|
|
for( size_t i = 0; i < it.nplanes; i++, ++it )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2012-06-09 23:00:04 +08:00
|
|
|
for( int j = 0; j < totalsz; j += blockSize0 )
|
2011-06-14 04:56:27 +08:00
|
|
|
{
|
2012-06-09 23:00:04 +08:00
|
|
|
Size sz(std::min(blockSize0, totalsz - j), 1);
|
2011-06-14 04:56:27 +08:00
|
|
|
size_t blockSize = sz.width*esz;
|
|
|
|
if( ptrs[1] )
|
|
|
|
{
|
|
|
|
copymask(scbuf, 0, ptrs[1], 0, ptrs[0], 0, sz, &esz);
|
|
|
|
ptrs[1] += sz.width;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
memcpy(ptrs[0], scbuf, blockSize);
|
|
|
|
ptrs[0] += blockSize;
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
static void
|
|
|
|
flipHoriz( const uchar* src, size_t sstep, uchar* dst, size_t dstep, Size size, size_t esz )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2011-07-19 20:27:07 +08:00
|
|
|
int i, j, limit = (int)(((size.width + 1)/2)*esz);
|
2011-04-17 21:14:45 +08:00
|
|
|
AutoBuffer<int> _tab(size.width*esz);
|
|
|
|
int* tab = _tab;
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
for( i = 0; i < size.width; i++ )
|
|
|
|
for( size_t k = 0; k < esz; k++ )
|
2011-07-19 20:27:07 +08:00
|
|
|
tab[i*esz + k] = (int)((size.width - i - 1)*esz + k);
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
for( ; size.height--; src += sstep, dst += dstep )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2011-04-17 21:14:45 +08:00
|
|
|
for( i = 0; i < limit; i++ )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2011-04-17 21:14:45 +08:00
|
|
|
j = tab[i];
|
|
|
|
uchar t0 = src[i], t1 = src[j];
|
|
|
|
dst[i] = t1; dst[j] = t0;
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-04-17 21:14:45 +08:00
|
|
|
flipVert( const uchar* src0, size_t sstep, uchar* dst0, size_t dstep, Size size, size_t esz )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2011-04-17 21:14:45 +08:00
|
|
|
const uchar* src1 = src0 + (size.height - 1)*sstep;
|
|
|
|
uchar* dst1 = dst0 + (size.height - 1)*dstep;
|
|
|
|
size.width *= (int)esz;
|
|
|
|
|
|
|
|
for( int y = 0; y < (size.height + 1)/2; y++, src0 += sstep, src1 -= sstep,
|
|
|
|
dst0 += dstep, dst1 -= dstep )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
int i = 0;
|
2011-04-17 21:14:45 +08:00
|
|
|
if( ((size_t)src0|(size_t)dst0|(size_t)src1|(size_t)dst1) % sizeof(int) == 0 )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
|
|
|
for( ; i <= size.width - 16; i += 16 )
|
|
|
|
{
|
2011-04-17 21:14:45 +08:00
|
|
|
int t0 = ((int*)(src0 + i))[0];
|
2010-05-12 01:44:00 +08:00
|
|
|
int t1 = ((int*)(src1 + i))[0];
|
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
((int*)(dst0 + i))[0] = t1;
|
2010-05-12 01:44:00 +08:00
|
|
|
((int*)(dst1 + i))[0] = t0;
|
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
t0 = ((int*)(src0 + i))[1];
|
2010-05-12 01:44:00 +08:00
|
|
|
t1 = ((int*)(src1 + i))[1];
|
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
((int*)(dst0 + i))[1] = t1;
|
2010-05-12 01:44:00 +08:00
|
|
|
((int*)(dst1 + i))[1] = t0;
|
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
t0 = ((int*)(src0 + i))[2];
|
2010-05-12 01:44:00 +08:00
|
|
|
t1 = ((int*)(src1 + i))[2];
|
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
((int*)(dst0 + i))[2] = t1;
|
2010-05-12 01:44:00 +08:00
|
|
|
((int*)(dst1 + i))[2] = t0;
|
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
t0 = ((int*)(src0 + i))[3];
|
2010-05-12 01:44:00 +08:00
|
|
|
t1 = ((int*)(src1 + i))[3];
|
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
((int*)(dst0 + i))[3] = t1;
|
2010-05-12 01:44:00 +08:00
|
|
|
((int*)(dst1 + i))[3] = t0;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( ; i <= size.width - 4; i += 4 )
|
|
|
|
{
|
2011-04-17 21:14:45 +08:00
|
|
|
int t0 = ((int*)(src0 + i))[0];
|
2010-05-12 01:44:00 +08:00
|
|
|
int t1 = ((int*)(src1 + i))[0];
|
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
((int*)(dst0 + i))[0] = t1;
|
2010-05-12 01:44:00 +08:00
|
|
|
((int*)(dst1 + i))[0] = t0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for( ; i < size.width; i++ )
|
|
|
|
{
|
2011-04-17 21:14:45 +08:00
|
|
|
uchar t0 = src0[i];
|
2010-05-12 01:44:00 +08:00
|
|
|
uchar t1 = src1[i];
|
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
dst0[i] = t1;
|
2010-05-12 01:44:00 +08:00
|
|
|
dst1[i] = t0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-25 01:03:31 +08:00
|
|
|
#ifdef HAVE_OPENCL
|
|
|
|
|
2013-12-05 18:36:45 +08:00
|
|
|
enum { FLIP_COLS = 1 << 0, FLIP_ROWS = 1 << 1, FLIP_BOTH = FLIP_ROWS | FLIP_COLS };
|
|
|
|
|
|
|
|
static bool ocl_flip(InputArray _src, OutputArray _dst, int flipCode )
|
|
|
|
{
|
2014-06-02 18:21:03 +08:00
|
|
|
CV_Assert(flipCode >= -1 && flipCode <= 1);
|
2014-07-01 17:49:03 +08:00
|
|
|
|
|
|
|
const ocl::Device & dev = ocl::Device::getDefault();
|
2014-06-02 18:21:03 +08:00
|
|
|
int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type),
|
2014-07-31 19:21:52 +08:00
|
|
|
flipType, kercn = std::min(ocl::predictOptimalVectorWidth(_src, _dst), 4);
|
2013-12-05 18:36:45 +08:00
|
|
|
|
2014-07-01 17:49:03 +08:00
|
|
|
bool doubleSupport = dev.doubleFPConfig() > 0;
|
|
|
|
if (!doubleSupport && depth == CV_64F)
|
|
|
|
kercn = cn;
|
|
|
|
|
2014-07-31 19:21:52 +08:00
|
|
|
if (cn > 4)
|
2013-12-05 18:36:45 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
const char * kernelName;
|
|
|
|
if (flipCode == 0)
|
|
|
|
kernelName = "arithm_flip_rows", flipType = FLIP_ROWS;
|
|
|
|
else if (flipCode > 0)
|
|
|
|
kernelName = "arithm_flip_cols", flipType = FLIP_COLS;
|
|
|
|
else
|
|
|
|
kernelName = "arithm_flip_rows_cols", flipType = FLIP_BOTH;
|
|
|
|
|
2014-05-12 20:30:47 +08:00
|
|
|
int pxPerWIy = (dev.isIntel() && (dev.type() & ocl::Device::TYPE_GPU)) ? 4 : 1;
|
2014-07-31 19:21:52 +08:00
|
|
|
kercn = (cn!=3 || flipType == FLIP_ROWS) ? std::max(kercn, cn) : cn;
|
2014-05-12 20:30:47 +08:00
|
|
|
|
2013-12-05 18:36:45 +08:00
|
|
|
ocl::Kernel k(kernelName, ocl::core::flip_oclsrc,
|
2014-06-02 18:21:03 +08:00
|
|
|
format( "-D T=%s -D T1=%s -D cn=%d -D PIX_PER_WI_Y=%d -D kercn=%d",
|
2014-07-01 17:49:03 +08:00
|
|
|
kercn != cn ? ocl::typeToStr(CV_MAKE_TYPE(depth, kercn)) : ocl::memopTypeToStr(CV_MAKE_TYPE(depth, kercn)),
|
|
|
|
kercn != cn ? ocl::typeToStr(depth) : ocl::memopTypeToStr(depth), cn, pxPerWIy, kercn));
|
2013-12-05 18:36:45 +08:00
|
|
|
if (k.empty())
|
|
|
|
return false;
|
|
|
|
|
2014-04-06 01:54:25 +08:00
|
|
|
Size size = _src.size();
|
2013-12-05 18:36:45 +08:00
|
|
|
_dst.create(size, type);
|
|
|
|
UMat src = _src.getUMat(), dst = _dst.getUMat();
|
|
|
|
|
2014-06-02 18:21:03 +08:00
|
|
|
int cols = size.width * cn / kercn, rows = size.height;
|
2014-03-03 23:04:32 +08:00
|
|
|
cols = flipType == FLIP_COLS ? (cols + 1) >> 1 : cols;
|
|
|
|
rows = flipType & FLIP_ROWS ? (rows + 1) >> 1 : rows;
|
|
|
|
|
|
|
|
k.args(ocl::KernelArg::ReadOnlyNoSize(src),
|
2014-06-02 18:21:03 +08:00
|
|
|
ocl::KernelArg::WriteOnly(dst, cn, kercn), rows, cols);
|
2013-12-05 18:36:45 +08:00
|
|
|
|
2014-05-12 20:30:47 +08:00
|
|
|
size_t maxWorkGroupSize = dev.maxWorkGroupSize();
|
2014-03-03 23:04:32 +08:00
|
|
|
CV_Assert(maxWorkGroupSize % 4 == 0);
|
2014-05-12 20:30:47 +08:00
|
|
|
|
2014-06-02 18:21:03 +08:00
|
|
|
size_t globalsize[2] = { cols, (rows + pxPerWIy - 1) / pxPerWIy },
|
|
|
|
localsize[2] = { maxWorkGroupSize / 4, 4 };
|
|
|
|
return k.run(2, globalsize, (flipType == FLIP_COLS) && !dev.isIntel() ? localsize : NULL, false);
|
2013-12-05 18:36:45 +08:00
|
|
|
}
|
|
|
|
|
2014-01-25 01:03:31 +08:00
|
|
|
#endif
|
|
|
|
|
2011-06-06 22:51:27 +08:00
|
|
|
void flip( InputArray _src, OutputArray _dst, int flip_mode )
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2013-12-05 18:36:45 +08:00
|
|
|
CV_Assert( _src.dims() <= 2 );
|
2014-04-06 01:54:25 +08:00
|
|
|
Size size = _src.size();
|
|
|
|
|
|
|
|
if (flip_mode < 0)
|
|
|
|
{
|
|
|
|
if (size.width == 1)
|
|
|
|
flip_mode = 0;
|
|
|
|
if (size.height == 1)
|
|
|
|
flip_mode = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((size.width == 1 && flip_mode > 0) ||
|
|
|
|
(size.height == 1 && flip_mode == 0) ||
|
|
|
|
(size.height == 1 && size.width == 1 && flip_mode < 0))
|
|
|
|
{
|
|
|
|
return _src.copyTo(_dst);
|
|
|
|
}
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2014-04-06 01:54:25 +08:00
|
|
|
CV_OCL_RUN( _dst.isUMat(), ocl_flip(_src, _dst, flip_mode))
|
2013-12-05 18:36:45 +08:00
|
|
|
|
|
|
|
Mat src = _src.getMat();
|
2014-04-06 01:54:25 +08:00
|
|
|
int type = src.type();
|
|
|
|
_dst.create( size, type );
|
2011-04-17 21:14:45 +08:00
|
|
|
Mat dst = _dst.getMat();
|
2014-04-06 01:54:25 +08:00
|
|
|
size_t esz = CV_ELEM_SIZE(type);
|
|
|
|
|
2014-04-28 23:31:18 +08:00
|
|
|
#if defined HAVE_IPP
|
2014-04-06 01:54:25 +08:00
|
|
|
typedef IppStatus (CV_STDCALL * ippiMirror)(const void * pSrc, int srcStep, void * pDst, int dstStep, IppiSize roiSize, IppiAxis flip);
|
2014-04-13 20:19:34 +08:00
|
|
|
typedef IppStatus (CV_STDCALL * ippiMirrorI)(const void * pSrcDst, int srcDstStep, IppiSize roiSize, IppiAxis flip);
|
|
|
|
ippiMirror ippFunc = 0;
|
|
|
|
ippiMirrorI ippFuncI = 0;
|
|
|
|
|
|
|
|
if (src.data == dst.data)
|
|
|
|
{
|
2014-04-14 22:42:21 +08:00
|
|
|
CV_SUPPRESS_DEPRECATED_START
|
2014-04-13 20:19:34 +08:00
|
|
|
ippFuncI =
|
|
|
|
type == CV_8UC1 ? (ippiMirrorI)ippiMirror_8u_C1IR :
|
|
|
|
type == CV_8UC3 ? (ippiMirrorI)ippiMirror_8u_C3IR :
|
|
|
|
type == CV_8UC4 ? (ippiMirrorI)ippiMirror_8u_C4IR :
|
|
|
|
type == CV_16UC1 ? (ippiMirrorI)ippiMirror_16u_C1IR :
|
|
|
|
type == CV_16UC3 ? (ippiMirrorI)ippiMirror_16u_C3IR :
|
|
|
|
type == CV_16UC4 ? (ippiMirrorI)ippiMirror_16u_C4IR :
|
|
|
|
type == CV_16SC1 ? (ippiMirrorI)ippiMirror_16s_C1IR :
|
|
|
|
type == CV_16SC3 ? (ippiMirrorI)ippiMirror_16s_C3IR :
|
|
|
|
type == CV_16SC4 ? (ippiMirrorI)ippiMirror_16s_C4IR :
|
|
|
|
type == CV_32SC1 ? (ippiMirrorI)ippiMirror_32s_C1IR :
|
|
|
|
type == CV_32SC3 ? (ippiMirrorI)ippiMirror_32s_C3IR :
|
|
|
|
type == CV_32SC4 ? (ippiMirrorI)ippiMirror_32s_C4IR :
|
|
|
|
type == CV_32FC1 ? (ippiMirrorI)ippiMirror_32f_C1IR :
|
|
|
|
type == CV_32FC3 ? (ippiMirrorI)ippiMirror_32f_C3IR :
|
|
|
|
type == CV_32FC4 ? (ippiMirrorI)ippiMirror_32f_C4IR : 0;
|
2014-04-14 22:42:21 +08:00
|
|
|
CV_SUPPRESS_DEPRECATED_END
|
2014-04-13 20:19:34 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ippFunc =
|
|
|
|
type == CV_8UC1 ? (ippiMirror)ippiMirror_8u_C1R :
|
|
|
|
type == CV_8UC3 ? (ippiMirror)ippiMirror_8u_C3R :
|
|
|
|
type == CV_8UC4 ? (ippiMirror)ippiMirror_8u_C4R :
|
|
|
|
type == CV_16UC1 ? (ippiMirror)ippiMirror_16u_C1R :
|
|
|
|
type == CV_16UC3 ? (ippiMirror)ippiMirror_16u_C3R :
|
|
|
|
type == CV_16UC4 ? (ippiMirror)ippiMirror_16u_C4R :
|
|
|
|
type == CV_16SC1 ? (ippiMirror)ippiMirror_16s_C1R :
|
|
|
|
type == CV_16SC3 ? (ippiMirror)ippiMirror_16s_C3R :
|
|
|
|
type == CV_16SC4 ? (ippiMirror)ippiMirror_16s_C4R :
|
|
|
|
type == CV_32SC1 ? (ippiMirror)ippiMirror_32s_C1R :
|
|
|
|
type == CV_32SC3 ? (ippiMirror)ippiMirror_32s_C3R :
|
|
|
|
type == CV_32SC4 ? (ippiMirror)ippiMirror_32s_C4R :
|
|
|
|
type == CV_32FC1 ? (ippiMirror)ippiMirror_32f_C1R :
|
|
|
|
type == CV_32FC3 ? (ippiMirror)ippiMirror_32f_C3R :
|
|
|
|
type == CV_32FC4 ? (ippiMirror)ippiMirror_32f_C4R : 0;
|
|
|
|
}
|
2014-04-06 01:54:25 +08:00
|
|
|
IppiAxis axis = flip_mode == 0 ? ippAxsHorizontal :
|
|
|
|
flip_mode > 0 ? ippAxsVertical : ippAxsBoth;
|
2014-04-13 20:19:34 +08:00
|
|
|
IppiSize roisize = { dst.cols, dst.rows };
|
2014-04-06 01:54:25 +08:00
|
|
|
|
|
|
|
if (ippFunc != 0)
|
|
|
|
{
|
2014-08-13 19:08:27 +08:00
|
|
|
if (ippFunc(src.ptr(), (int)src.step, dst.ptr(), (int)dst.step, ippiSize(src.cols, src.rows), axis) >= 0)
|
2014-04-13 20:19:34 +08:00
|
|
|
return;
|
|
|
|
setIppErrorStatus();
|
|
|
|
}
|
|
|
|
else if (ippFuncI != 0)
|
|
|
|
{
|
2014-08-13 19:08:27 +08:00
|
|
|
if (ippFuncI(dst.ptr(), (int)dst.step, roisize, axis) >= 0)
|
2014-04-06 01:54:25 +08:00
|
|
|
return;
|
2014-04-16 22:50:23 +08:00
|
|
|
setIppErrorStatus();
|
2014-04-06 01:54:25 +08:00
|
|
|
}
|
2014-04-18 18:25:38 +08:00
|
|
|
#endif
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
if( flip_mode <= 0 )
|
2014-08-13 19:08:27 +08:00
|
|
|
flipVert( src.ptr(), src.step, dst.ptr(), dst.step, src.size(), esz );
|
2010-05-12 01:44:00 +08:00
|
|
|
else
|
2014-08-13 19:08:27 +08:00
|
|
|
flipHoriz( src.ptr(), src.step, dst.ptr(), dst.step, src.size(), esz );
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2011-04-17 21:14:45 +08:00
|
|
|
if( flip_mode < 0 )
|
2014-08-13 19:08:27 +08:00
|
|
|
flipHoriz( dst.ptr(), dst.step, dst.ptr(), dst.step, dst.size(), esz );
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2014-08-13 20:32:01 +08:00
|
|
|
#if defined HAVE_OPENCL && !defined __APPLE__
|
2010-05-12 01:44:00 +08:00
|
|
|
|
2013-12-19 18:27:21 +08:00
|
|
|
static bool ocl_repeat(InputArray _src, int ny, int nx, OutputArray _dst)
|
|
|
|
{
|
2014-05-23 17:45:24 +08:00
|
|
|
if (ny == 1 && nx == 1)
|
|
|
|
{
|
|
|
|
_src.copyTo(_dst);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type),
|
|
|
|
rowsPerWI = ocl::Device::getDefault().isIntel() ? 4 : 1,
|
|
|
|
kercn = std::min(ocl::predictOptimalVectorWidth(_src, _dst), 4);
|
|
|
|
|
|
|
|
ocl::Kernel k("repeat", ocl::core::repeat_oclsrc,
|
|
|
|
format("-D T=%s -D nx=%d -D ny=%d -D rowsPerWI=%d -D cn=%d",
|
|
|
|
ocl::memopTypeToStr(CV_MAKE_TYPE(depth, kercn)),
|
|
|
|
nx, ny, rowsPerWI, kercn));
|
|
|
|
if (k.empty())
|
|
|
|
return false;
|
|
|
|
|
2013-12-19 18:27:21 +08:00
|
|
|
UMat src = _src.getUMat(), dst = _dst.getUMat();
|
2014-05-23 17:45:24 +08:00
|
|
|
k.args(ocl::KernelArg::ReadOnly(src, cn, kercn), ocl::KernelArg::WriteOnlyNoSize(dst));
|
2013-12-19 18:27:21 +08:00
|
|
|
|
2014-05-23 17:45:24 +08:00
|
|
|
size_t globalsize[] = { src.cols * cn / kercn, (src.rows + rowsPerWI - 1) / rowsPerWI };
|
|
|
|
return k.run(2, globalsize, NULL, false);
|
2013-12-19 18:27:21 +08:00
|
|
|
}
|
|
|
|
|
2014-08-13 20:32:01 +08:00
|
|
|
#endif
|
2014-01-25 01:03:31 +08:00
|
|
|
|
2011-06-06 22:51:27 +08:00
|
|
|
void repeat(InputArray _src, int ny, int nx, OutputArray _dst)
|
2010-05-12 01:44:00 +08:00
|
|
|
{
|
2013-12-19 18:27:21 +08:00
|
|
|
CV_Assert( _src.dims() <= 2 );
|
2013-11-05 03:59:56 +08:00
|
|
|
CV_Assert( ny > 0 && nx > 0 );
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2013-12-19 18:27:21 +08:00
|
|
|
Size ssize = _src.size();
|
|
|
|
_dst.create(ssize.height*ny, ssize.width*nx, _src.type());
|
|
|
|
|
2014-08-13 20:32:01 +08:00
|
|
|
#if !defined __APPLE__
|
|
|
|
CV_OCL_RUN(_dst.isUMat(),
|
|
|
|
ocl_repeat(_src, ny, nx, _dst))
|
|
|
|
#endif
|
2013-12-19 18:27:21 +08:00
|
|
|
|
|
|
|
Mat src = _src.getMat(), dst = _dst.getMat();
|
|
|
|
Size dsize = dst.size();
|
2010-05-12 01:44:00 +08:00
|
|
|
int esz = (int)src.elemSize();
|
|
|
|
int x, y;
|
|
|
|
ssize.width *= esz; dsize.width *= esz;
|
|
|
|
|
|
|
|
for( y = 0; y < ssize.height; y++ )
|
|
|
|
{
|
|
|
|
for( x = 0; x < dsize.width; x += ssize.width )
|
2014-08-13 19:08:27 +08:00
|
|
|
memcpy( dst.ptr(y) + x, src.ptr(y), ssize.width );
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
for( ; y < dsize.height; y++ )
|
2014-08-13 19:08:27 +08:00
|
|
|
memcpy( dst.ptr(y), dst.ptr(y - ssize.height), dsize.width );
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
2010-11-24 00:39:20 +08:00
|
|
|
Mat repeat(const Mat& src, int ny, int nx)
|
|
|
|
{
|
|
|
|
if( nx == 1 && ny == 1 )
|
|
|
|
return src;
|
|
|
|
Mat dst;
|
|
|
|
repeat(src, ny, nx, dst);
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2013-04-10 19:54:14 +08:00
|
|
|
|
|
|
|
} // cv
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
Various border types, image boundaries are denoted with '|'
|
|
|
|
|
|
|
|
* BORDER_REPLICATE: aaaaaa|abcdefgh|hhhhhhh
|
|
|
|
* BORDER_REFLECT: fedcba|abcdefgh|hgfedcb
|
|
|
|
* BORDER_REFLECT_101: gfedcb|abcdefgh|gfedcba
|
|
|
|
* BORDER_WRAP: cdefgh|abcdefgh|abcdefg
|
|
|
|
* BORDER_CONSTANT: iiiiii|abcdefgh|iiiiiii with some specified 'i'
|
|
|
|
*/
|
|
|
|
int cv::borderInterpolate( int p, int len, int borderType )
|
|
|
|
{
|
|
|
|
if( (unsigned)p < (unsigned)len )
|
|
|
|
;
|
|
|
|
else if( borderType == BORDER_REPLICATE )
|
|
|
|
p = p < 0 ? 0 : len - 1;
|
|
|
|
else if( borderType == BORDER_REFLECT || borderType == BORDER_REFLECT_101 )
|
|
|
|
{
|
|
|
|
int delta = borderType == BORDER_REFLECT_101;
|
|
|
|
if( len == 1 )
|
|
|
|
return 0;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if( p < 0 )
|
|
|
|
p = -p - 1 + delta;
|
|
|
|
else
|
|
|
|
p = len - 1 - (p - len) - delta;
|
|
|
|
}
|
|
|
|
while( (unsigned)p >= (unsigned)len );
|
|
|
|
}
|
|
|
|
else if( borderType == BORDER_WRAP )
|
|
|
|
{
|
2014-02-24 01:07:20 +08:00
|
|
|
CV_Assert(len > 0);
|
2013-04-10 19:54:14 +08:00
|
|
|
if( p < 0 )
|
|
|
|
p -= ((p-len+1)/len)*len;
|
|
|
|
if( p >= len )
|
|
|
|
p %= len;
|
|
|
|
}
|
|
|
|
else if( borderType == BORDER_CONSTANT )
|
|
|
|
p = -1;
|
|
|
|
else
|
|
|
|
CV_Error( CV_StsBadArg, "Unknown/unsupported border type" );
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
|
|
|
void copyMakeBorder_8u( const uchar* src, size_t srcstep, cv::Size srcroi,
|
|
|
|
uchar* dst, size_t dststep, cv::Size dstroi,
|
|
|
|
int top, int left, int cn, int borderType )
|
|
|
|
{
|
|
|
|
const int isz = (int)sizeof(int);
|
|
|
|
int i, j, k, elemSize = 1;
|
|
|
|
bool intMode = false;
|
|
|
|
|
|
|
|
if( (cn | srcstep | dststep | (size_t)src | (size_t)dst) % isz == 0 )
|
|
|
|
{
|
|
|
|
cn /= isz;
|
|
|
|
elemSize = isz;
|
|
|
|
intMode = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
cv::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 = cv::borderInterpolate(i - left, srcroi.width, borderType)*cn;
|
|
|
|
for( k = 0; k < cn; k++ )
|
|
|
|
tab[i*cn + k] = j + k;
|
|
|
|
}
|
|
|
|
|
|
|
|
for( i = 0; i < right; i++ )
|
|
|
|
{
|
|
|
|
j = cv::borderInterpolate(srcroi.width + i, srcroi.width, borderType)*cn;
|
|
|
|
for( k = 0; k < cn; k++ )
|
|
|
|
tab[(i+left)*cn + k] = j + k;
|
|
|
|
}
|
|
|
|
|
|
|
|
srcroi.width *= cn;
|
|
|
|
dstroi.width *= cn;
|
|
|
|
left *= cn;
|
|
|
|
right *= cn;
|
|
|
|
|
|
|
|
uchar* dstInner = dst + dststep*top + left*elemSize;
|
|
|
|
|
|
|
|
for( i = 0; i < srcroi.height; i++, dstInner += dststep, src += srcstep )
|
|
|
|
{
|
|
|
|
if( dstInner != src )
|
|
|
|
memcpy(dstInner, src, srcroi.width*elemSize);
|
|
|
|
|
|
|
|
if( intMode )
|
|
|
|
{
|
|
|
|
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]];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for( j = 0; j < left; j++ )
|
|
|
|
dstInner[j - left] = src[tab[j]];
|
|
|
|
for( j = 0; j < right; j++ )
|
|
|
|
dstInner[j + srcroi.width] = src[tab[j + left]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dstroi.width *= elemSize;
|
|
|
|
dst += dststep*top;
|
|
|
|
|
|
|
|
for( i = 0; i < top; i++ )
|
|
|
|
{
|
|
|
|
j = cv::borderInterpolate(i - top, srcroi.height, borderType);
|
|
|
|
memcpy(dst + (i - top)*dststep, dst + j*dststep, dstroi.width);
|
|
|
|
}
|
|
|
|
|
|
|
|
for( i = 0; i < bottom; i++ )
|
|
|
|
{
|
|
|
|
j = cv::borderInterpolate(i + srcroi.height, srcroi.height, borderType);
|
|
|
|
memcpy(dst + (i + srcroi.height)*dststep, dst + j*dststep, dstroi.width);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void copyMakeConstBorder_8u( const uchar* src, size_t srcstep, cv::Size srcroi,
|
|
|
|
uchar* dst, size_t dststep, cv::Size dstroi,
|
|
|
|
int top, int left, int cn, const uchar* value )
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
cv::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++ )
|
|
|
|
{
|
|
|
|
for( j = 0; j < cn; j++ )
|
|
|
|
constBuf[i*cn + j] = value[j];
|
|
|
|
}
|
|
|
|
|
|
|
|
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 )
|
|
|
|
{
|
|
|
|
if( dstInner != src )
|
|
|
|
memcpy( dstInner, src, srcroi.width );
|
|
|
|
memcpy( dstInner - left, constBuf, left );
|
|
|
|
memcpy( dstInner + srcroi.width, constBuf, right );
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-01-25 01:03:31 +08:00
|
|
|
#ifdef HAVE_OPENCL
|
|
|
|
|
2013-12-03 03:45:16 +08:00
|
|
|
namespace cv {
|
|
|
|
|
|
|
|
static bool ocl_copyMakeBorder( InputArray _src, OutputArray _dst, int top, int bottom,
|
|
|
|
int left, int right, int borderType, const Scalar& value )
|
|
|
|
{
|
2014-05-27 20:12:52 +08:00
|
|
|
int type = _src.type(), cn = CV_MAT_CN(type), depth = CV_MAT_DEPTH(type),
|
|
|
|
rowsPerWI = ocl::Device::getDefault().isIntel() ? 4 : 1;
|
2013-12-03 03:45:16 +08:00
|
|
|
bool isolated = (borderType & BORDER_ISOLATED) != 0;
|
|
|
|
borderType &= ~cv::BORDER_ISOLATED;
|
|
|
|
|
|
|
|
if ( !(borderType == BORDER_CONSTANT || borderType == BORDER_REPLICATE || borderType == BORDER_REFLECT ||
|
|
|
|
borderType == BORDER_WRAP || borderType == BORDER_REFLECT_101) ||
|
2014-02-27 18:49:36 +08:00
|
|
|
cn > 4)
|
2013-12-03 03:45:16 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
const char * const borderMap[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", "BORDER_WRAP", "BORDER_REFLECT_101" };
|
2014-02-27 18:49:36 +08:00
|
|
|
int scalarcn = cn == 3 ? 4 : cn;
|
|
|
|
int sctype = CV_MAKETYPE(depth, scalarcn);
|
2014-05-27 20:12:52 +08:00
|
|
|
String buildOptions = format("-D T=%s -D %s -D T1=%s -D cn=%d -D ST=%s -D rowsPerWI=%d",
|
|
|
|
ocl::memopTypeToStr(type), borderMap[borderType],
|
|
|
|
ocl::memopTypeToStr(depth), cn,
|
|
|
|
ocl::memopTypeToStr(sctype), rowsPerWI);
|
2014-02-27 18:49:36 +08:00
|
|
|
|
|
|
|
ocl::Kernel k("copyMakeBorder", ocl::core::copymakeborder_oclsrc, buildOptions);
|
2013-12-03 03:45:16 +08:00
|
|
|
if (k.empty())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
UMat src = _src.getUMat();
|
|
|
|
if( src.isSubmatrix() && !isolated )
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
bottom -= dbottom;
|
|
|
|
right -= dright;
|
|
|
|
}
|
|
|
|
|
|
|
|
_dst.create(src.rows + top + bottom, src.cols + left + right, type);
|
|
|
|
UMat dst = _dst.getUMat();
|
|
|
|
|
|
|
|
if (top == 0 && left == 0 && bottom == 0 && right == 0)
|
|
|
|
{
|
|
|
|
if(src.u != dst.u || src.step != dst.step)
|
|
|
|
src.copyTo(dst);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
k.args(ocl::KernelArg::ReadOnly(src), ocl::KernelArg::WriteOnly(dst),
|
2014-02-27 18:49:36 +08:00
|
|
|
top, left, ocl::KernelArg::Constant(Mat(1, 1, sctype, value)));
|
2013-12-03 03:45:16 +08:00
|
|
|
|
2014-05-27 20:12:52 +08:00
|
|
|
size_t globalsize[2] = { dst.cols, (dst.rows + rowsPerWI - 1) / rowsPerWI };
|
2013-12-03 03:45:16 +08:00
|
|
|
return k.run(2, globalsize, NULL, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-01-25 01:03:31 +08:00
|
|
|
#endif
|
|
|
|
|
2013-04-10 19:54:14 +08:00
|
|
|
void cv::copyMakeBorder( InputArray _src, OutputArray _dst, int top, int bottom,
|
|
|
|
int left, int right, int borderType, const Scalar& value )
|
|
|
|
{
|
|
|
|
CV_Assert( top >= 0 && bottom >= 0 && left >= 0 && right >= 0 );
|
|
|
|
|
2014-01-25 01:03:31 +08:00
|
|
|
CV_OCL_RUN(_dst.isUMat() && _src.dims() <= 2,
|
|
|
|
ocl_copyMakeBorder(_src, _dst, top, bottom, left, right, borderType, value))
|
2013-12-03 03:45:16 +08:00
|
|
|
|
|
|
|
Mat src = _src.getMat();
|
2014-04-23 18:18:20 +08:00
|
|
|
int type = src.type();
|
2013-12-03 03:45:16 +08:00
|
|
|
|
2013-04-10 19:54:14 +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;
|
|
|
|
bottom -= dbottom;
|
|
|
|
right -= dright;
|
|
|
|
}
|
|
|
|
|
2014-04-23 18:18:20 +08:00
|
|
|
_dst.create( src.rows + top + bottom, src.cols + left + right, type );
|
2013-04-10 19:54:14 +08:00
|
|
|
Mat dst = _dst.getMat();
|
|
|
|
|
|
|
|
if(top == 0 && left == 0 && bottom == 0 && right == 0)
|
|
|
|
{
|
|
|
|
if(src.data != dst.data || src.step != dst.step)
|
|
|
|
src.copyTo(dst);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
borderType &= ~BORDER_ISOLATED;
|
|
|
|
|
2014-04-23 18:18:20 +08:00
|
|
|
#if defined HAVE_IPP && 0
|
|
|
|
typedef IppStatus (CV_STDCALL * ippiCopyMakeBorder)(const void * pSrc, int srcStep, IppiSize srcRoiSize, void * pDst,
|
|
|
|
int dstStep, IppiSize dstRoiSize, int topBorderHeight, int leftBorderWidth);
|
|
|
|
typedef IppStatus (CV_STDCALL * ippiCopyMakeBorderI)(const void * pSrc, int srcDstStep, IppiSize srcRoiSize, IppiSize dstRoiSize,
|
|
|
|
int topBorderHeight, int leftborderwidth);
|
|
|
|
typedef IppStatus (CV_STDCALL * ippiCopyConstBorder)(const void * pSrc, int srcStep, IppiSize srcRoiSize, void * pDst, int dstStep,
|
|
|
|
IppiSize dstRoiSize, int topBorderHeight, int leftBorderWidth, void * value);
|
|
|
|
|
|
|
|
IppiSize srcRoiSize = { src.cols, src.rows }, dstRoiSize = { dst.cols, dst.rows };
|
|
|
|
ippiCopyMakeBorder ippFunc = 0;
|
|
|
|
ippiCopyMakeBorderI ippFuncI = 0;
|
|
|
|
ippiCopyConstBorder ippFuncConst = 0;
|
|
|
|
bool inplace = dst.datastart == src.datastart;
|
|
|
|
|
|
|
|
if (borderType == BORDER_CONSTANT)
|
|
|
|
{
|
|
|
|
ippFuncConst =
|
|
|
|
// type == CV_8UC1 ? (ippiCopyConstBorder)ippiCopyConstBorder_8u_C1R : bug in IPP 8.1
|
|
|
|
type == CV_16UC1 ? (ippiCopyConstBorder)ippiCopyConstBorder_16u_C1R :
|
|
|
|
// type == CV_16SC1 ? (ippiCopyConstBorder)ippiCopyConstBorder_16s_C1R : bug in IPP 8.1
|
|
|
|
// type == CV_32SC1 ? (ippiCopyConstBorder)ippiCopyConstBorder_32s_C1R : bug in IPP 8.1
|
|
|
|
// type == CV_32FC1 ? (ippiCopyConstBorder)ippiCopyConstBorder_32f_C1R : bug in IPP 8.1
|
|
|
|
type == CV_8UC3 ? (ippiCopyConstBorder)ippiCopyConstBorder_8u_C3R :
|
|
|
|
type == CV_16UC3 ? (ippiCopyConstBorder)ippiCopyConstBorder_16u_C3R :
|
|
|
|
type == CV_16SC3 ? (ippiCopyConstBorder)ippiCopyConstBorder_16s_C3R :
|
|
|
|
type == CV_32SC3 ? (ippiCopyConstBorder)ippiCopyConstBorder_32s_C3R :
|
|
|
|
type == CV_32FC3 ? (ippiCopyConstBorder)ippiCopyConstBorder_32f_C3R :
|
|
|
|
type == CV_8UC4 ? (ippiCopyConstBorder)ippiCopyConstBorder_8u_C4R :
|
|
|
|
type == CV_16UC4 ? (ippiCopyConstBorder)ippiCopyConstBorder_16u_C4R :
|
|
|
|
type == CV_16SC4 ? (ippiCopyConstBorder)ippiCopyConstBorder_16s_C4R :
|
|
|
|
type == CV_32SC4 ? (ippiCopyConstBorder)ippiCopyConstBorder_32s_C4R :
|
|
|
|
type == CV_32FC4 ? (ippiCopyConstBorder)ippiCopyConstBorder_32f_C4R : 0;
|
|
|
|
}
|
|
|
|
else if (borderType == BORDER_WRAP)
|
|
|
|
{
|
|
|
|
if (inplace)
|
|
|
|
{
|
|
|
|
CV_SUPPRESS_DEPRECATED_START
|
|
|
|
ippFuncI =
|
|
|
|
type == CV_32SC1 ? (ippiCopyMakeBorderI)ippiCopyWrapBorder_32s_C1IR :
|
|
|
|
type == CV_32FC1 ? (ippiCopyMakeBorderI)ippiCopyWrapBorder_32s_C1IR : 0;
|
|
|
|
CV_SUPPRESS_DEPRECATED_END
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ippFunc =
|
|
|
|
type == CV_32SC1 ? (ippiCopyMakeBorder)ippiCopyWrapBorder_32s_C1R :
|
|
|
|
type == CV_32FC1 ? (ippiCopyMakeBorder)ippiCopyWrapBorder_32s_C1R : 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (borderType == BORDER_REPLICATE)
|
|
|
|
{
|
|
|
|
if (inplace)
|
|
|
|
{
|
|
|
|
CV_SUPPRESS_DEPRECATED_START
|
|
|
|
ippFuncI =
|
|
|
|
type == CV_8UC1 ? (ippiCopyMakeBorderI)ippiCopyReplicateBorder_8u_C1IR :
|
|
|
|
type == CV_16UC1 ? (ippiCopyMakeBorderI)ippiCopyReplicateBorder_16u_C1IR :
|
|
|
|
type == CV_16SC1 ? (ippiCopyMakeBorderI)ippiCopyReplicateBorder_16s_C1IR :
|
|
|
|
type == CV_32SC1 ? (ippiCopyMakeBorderI)ippiCopyReplicateBorder_32s_C1IR :
|
|
|
|
type == CV_32FC1 ? (ippiCopyMakeBorderI)ippiCopyReplicateBorder_32f_C1IR :
|
|
|
|
type == CV_8UC3 ? (ippiCopyMakeBorderI)ippiCopyReplicateBorder_8u_C3IR :
|
|
|
|
type == CV_16UC3 ? (ippiCopyMakeBorderI)ippiCopyReplicateBorder_16u_C3IR :
|
|
|
|
type == CV_16SC3 ? (ippiCopyMakeBorderI)ippiCopyReplicateBorder_16s_C3IR :
|
|
|
|
type == CV_32SC3 ? (ippiCopyMakeBorderI)ippiCopyReplicateBorder_32s_C3IR :
|
|
|
|
type == CV_32FC3 ? (ippiCopyMakeBorderI)ippiCopyReplicateBorder_32f_C3IR :
|
|
|
|
type == CV_8UC4 ? (ippiCopyMakeBorderI)ippiCopyReplicateBorder_8u_C4IR :
|
|
|
|
type == CV_16UC4 ? (ippiCopyMakeBorderI)ippiCopyReplicateBorder_16u_C4IR :
|
|
|
|
type == CV_16SC4 ? (ippiCopyMakeBorderI)ippiCopyReplicateBorder_16s_C4IR :
|
|
|
|
type == CV_32SC4 ? (ippiCopyMakeBorderI)ippiCopyReplicateBorder_32s_C4IR :
|
|
|
|
type == CV_32FC4 ? (ippiCopyMakeBorderI)ippiCopyReplicateBorder_32f_C4IR : 0;
|
|
|
|
CV_SUPPRESS_DEPRECATED_END
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ippFunc =
|
|
|
|
type == CV_8UC1 ? (ippiCopyMakeBorder)ippiCopyReplicateBorder_8u_C1R :
|
|
|
|
type == CV_16UC1 ? (ippiCopyMakeBorder)ippiCopyReplicateBorder_16u_C1R :
|
|
|
|
type == CV_16SC1 ? (ippiCopyMakeBorder)ippiCopyReplicateBorder_16s_C1R :
|
|
|
|
type == CV_32SC1 ? (ippiCopyMakeBorder)ippiCopyReplicateBorder_32s_C1R :
|
|
|
|
type == CV_32FC1 ? (ippiCopyMakeBorder)ippiCopyReplicateBorder_32f_C1R :
|
|
|
|
type == CV_8UC3 ? (ippiCopyMakeBorder)ippiCopyReplicateBorder_8u_C3R :
|
|
|
|
type == CV_16UC3 ? (ippiCopyMakeBorder)ippiCopyReplicateBorder_16u_C3R :
|
|
|
|
type == CV_16SC3 ? (ippiCopyMakeBorder)ippiCopyReplicateBorder_16s_C3R :
|
|
|
|
type == CV_32SC3 ? (ippiCopyMakeBorder)ippiCopyReplicateBorder_32s_C3R :
|
|
|
|
type == CV_32FC3 ? (ippiCopyMakeBorder)ippiCopyReplicateBorder_32f_C3R :
|
|
|
|
type == CV_8UC4 ? (ippiCopyMakeBorder)ippiCopyReplicateBorder_8u_C4R :
|
|
|
|
type == CV_16UC4 ? (ippiCopyMakeBorder)ippiCopyReplicateBorder_16u_C4R :
|
|
|
|
type == CV_16SC4 ? (ippiCopyMakeBorder)ippiCopyReplicateBorder_16s_C4R :
|
|
|
|
type == CV_32SC4 ? (ippiCopyMakeBorder)ippiCopyReplicateBorder_32s_C4R :
|
|
|
|
type == CV_32FC4 ? (ippiCopyMakeBorder)ippiCopyReplicateBorder_32f_C4R : 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ippFunc || ippFuncI || ippFuncConst)
|
|
|
|
{
|
|
|
|
uchar scbuf[32];
|
|
|
|
scalarToRawData(value, scbuf, type);
|
|
|
|
|
|
|
|
if ( (ippFunc && ippFunc(src.data, (int)src.step, srcRoiSize, dst.data, (int)dst.step, dstRoiSize, top, left) >= 0) ||
|
|
|
|
(ippFuncI && ippFuncI(src.data, (int)src.step, srcRoiSize, dstRoiSize, top, left) >= 0) ||
|
|
|
|
(ippFuncConst && ippFuncConst(src.data, (int)src.step, srcRoiSize, dst.data, (int)dst.step,
|
|
|
|
dstRoiSize, top, left, scbuf) >= 0))
|
|
|
|
return;
|
|
|
|
|
|
|
|
setIppErrorStatus();
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2013-04-10 19:54:14 +08:00
|
|
|
if( borderType != BORDER_CONSTANT )
|
2014-08-13 19:08:27 +08:00
|
|
|
copyMakeBorder_8u( src.ptr(), src.step, src.size(),
|
|
|
|
dst.ptr(), dst.step, dst.size(),
|
2013-04-10 19:54:14 +08:00
|
|
|
top, left, (int)src.elemSize(), borderType );
|
|
|
|
else
|
|
|
|
{
|
|
|
|
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);
|
2014-08-13 19:08:27 +08:00
|
|
|
copyMakeConstBorder_8u( src.ptr(), src.step, src.size(),
|
|
|
|
dst.ptr(), dst.step, dst.size(),
|
2013-04-10 19:54:14 +08:00
|
|
|
top, left, (int)src.elemSize(), (uchar*)(double*)buf );
|
|
|
|
}
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* dst = src */
|
|
|
|
CV_IMPL void
|
|
|
|
cvCopy( const void* srcarr, void* dstarr, const void* maskarr )
|
|
|
|
{
|
|
|
|
if( CV_IS_SPARSE_MAT(srcarr) && CV_IS_SPARSE_MAT(dstarr))
|
|
|
|
{
|
|
|
|
CV_Assert( maskarr == 0 );
|
|
|
|
CvSparseMat* src1 = (CvSparseMat*)srcarr;
|
|
|
|
CvSparseMat* dst1 = (CvSparseMat*)dstarr;
|
|
|
|
CvSparseMatIterator iterator;
|
|
|
|
CvSparseNode* node;
|
|
|
|
|
|
|
|
dst1->dims = src1->dims;
|
|
|
|
memcpy( dst1->size, src1->size, src1->dims*sizeof(src1->size[0]));
|
|
|
|
dst1->valoffset = src1->valoffset;
|
|
|
|
dst1->idxoffset = src1->idxoffset;
|
|
|
|
cvClearSet( dst1->heap );
|
|
|
|
|
|
|
|
if( src1->heap->active_count >= dst1->hashsize*CV_SPARSE_HASH_RATIO )
|
|
|
|
{
|
|
|
|
cvFree( &dst1->hashtable );
|
|
|
|
dst1->hashsize = src1->hashsize;
|
|
|
|
dst1->hashtable =
|
|
|
|
(void**)cvAlloc( dst1->hashsize*sizeof(dst1->hashtable[0]));
|
|
|
|
}
|
|
|
|
|
|
|
|
memset( dst1->hashtable, 0, dst1->hashsize*sizeof(dst1->hashtable[0]));
|
|
|
|
|
|
|
|
for( node = cvInitSparseMatIterator( src1, &iterator );
|
|
|
|
node != 0; node = cvGetNextSparseNode( &iterator ))
|
|
|
|
{
|
|
|
|
CvSparseNode* node_copy = (CvSparseNode*)cvSetNew( dst1->heap );
|
|
|
|
int tabidx = node->hashval & (dst1->hashsize - 1);
|
2011-04-17 21:14:45 +08:00
|
|
|
memcpy( node_copy, node, dst1->heap->elem_size );
|
2010-05-12 01:44:00 +08:00
|
|
|
node_copy->next = (CvSparseNode*)dst1->hashtable[tabidx];
|
|
|
|
dst1->hashtable[tabidx] = node_copy;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cv::Mat src = cv::cvarrToMat(srcarr, false, true, 1), dst = cv::cvarrToMat(dstarr, false, true, 1);
|
2010-10-20 20:33:57 +08:00
|
|
|
CV_Assert( src.depth() == dst.depth() && src.size == dst.size );
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
int coi1 = 0, coi2 = 0;
|
|
|
|
if( CV_IS_IMAGE(srcarr) )
|
|
|
|
coi1 = cvGetImageCOI((const IplImage*)srcarr);
|
|
|
|
if( CV_IS_IMAGE(dstarr) )
|
|
|
|
coi2 = cvGetImageCOI((const IplImage*)dstarr);
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if( coi1 || coi2 )
|
|
|
|
{
|
|
|
|
CV_Assert( (coi1 != 0 || src.channels() == 1) &&
|
|
|
|
(coi2 != 0 || dst.channels() == 1) );
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
int pair[] = { std::max(coi1-1, 0), std::max(coi2-1, 0) };
|
|
|
|
cv::mixChannels( &src, 1, &dst, 1, pair, 1 );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
CV_Assert( src.channels() == dst.channels() );
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if( !maskarr )
|
|
|
|
src.copyTo(dst);
|
|
|
|
else
|
|
|
|
src.copyTo(dst, cv::cvarrToMat(maskarr));
|
|
|
|
}
|
|
|
|
|
|
|
|
CV_IMPL void
|
|
|
|
cvSet( void* arr, CvScalar value, const void* maskarr )
|
|
|
|
{
|
|
|
|
cv::Mat m = cv::cvarrToMat(arr);
|
|
|
|
if( !maskarr )
|
|
|
|
m = value;
|
|
|
|
else
|
2011-06-14 04:56:27 +08:00
|
|
|
m.setTo(cv::Scalar(value), cv::cvarrToMat(maskarr));
|
2010-05-12 01:44:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
CV_IMPL void
|
|
|
|
cvSetZero( CvArr* arr )
|
|
|
|
{
|
|
|
|
if( CV_IS_SPARSE_MAT(arr) )
|
|
|
|
{
|
|
|
|
CvSparseMat* mat1 = (CvSparseMat*)arr;
|
|
|
|
cvClearSet( mat1->heap );
|
|
|
|
if( mat1->hashtable )
|
|
|
|
memset( mat1->hashtable, 0, mat1->hashsize*sizeof(mat1->hashtable[0]));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
cv::Mat m = cv::cvarrToMat(arr);
|
|
|
|
m = cv::Scalar(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
CV_IMPL void
|
|
|
|
cvFlip( const CvArr* srcarr, CvArr* dstarr, int flip_mode )
|
|
|
|
{
|
|
|
|
cv::Mat src = cv::cvarrToMat(srcarr);
|
|
|
|
cv::Mat dst;
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
if (!dstarr)
|
|
|
|
dst = src;
|
|
|
|
else
|
|
|
|
dst = cv::cvarrToMat(dstarr);
|
2012-06-09 23:00:04 +08:00
|
|
|
|
2010-05-12 01:44:00 +08:00
|
|
|
CV_Assert( src.type() == dst.type() && src.size() == dst.size() );
|
|
|
|
cv::flip( src, dst, flip_mode );
|
|
|
|
}
|
|
|
|
|
|
|
|
CV_IMPL void
|
|
|
|
cvRepeat( const CvArr* srcarr, CvArr* dstarr )
|
|
|
|
{
|
|
|
|
cv::Mat src = cv::cvarrToMat(srcarr), dst = cv::cvarrToMat(dstarr);
|
|
|
|
CV_Assert( src.type() == dst.type() &&
|
|
|
|
dst.rows % src.rows == 0 && dst.cols % src.cols == 0 );
|
|
|
|
cv::repeat(src, dst.rows/src.rows, dst.cols/src.cols, dst);
|
|
|
|
}
|
|
|
|
|
2012-07-24 22:25:46 +08:00
|
|
|
/* End of file. */
|