void cv::blendLinear( InputArray _src1, InputArray _src2, InputArray _weights1, InputArray _weights2, OutputArray _dst ) { int type = _src1.type(), depth = CV_MAT_DEPTH(type); Size size = _src1.size(); CV_Assert(depth == CV_8U || depth == CV_32F); CV_Assert(size == _src2.size() && size == _weights1.size() && size == _weights2.size()); CV_Assert(type == _src2.type() && _weights1.type() == CV_32FC1 && _weights2.type() == CV_32FC1); _dst.create(size, type); CV_OCL_RUN(_dst.isUMat(), ocl_blendLinear(_src1, _src2, _weights1, _weights2, _dst)) Mat src1 = _src1.getMat(), src2 = _src2.getMat(), weights1 = _weights1.getMat(), weights2 = _weights2.getMat(), dst = _dst.getMat(); if (depth == CV_8U) { BlendLinearInvoker<uchar> invoker(src1, src2, weights1, weights2, dst); parallel_for_(Range(0, src1.rows), invoker, dst.total()/(double)(1<<16)); } else if (depth == CV_32F) { BlendLinearInvoker<float> invoker(src1, src2, weights1, weights2, dst); parallel_for_(Range(0, src1.rows), invoker, dst.total()/(double)(1<<16)); } }
void CUBIC(InputArray src, OutputArray dest) { int odd_w =0; int odd_h =0; if(src.size().width%2!=0)odd_w=1; if(src.size().height%2!=0)odd_h=1; Mat im; copyMakeBorder(src, im,4,4+odd_h,4,4+odd_w,BORDER_REPLICATE); Mat dst; //resize(im, dst,Size(im.cols*2,im.rows*2),0,0,INTER_CUBIC); resize(im, dst,Size(im.cols*2,im.rows*2),0,0,INTER_LINEAR); warpShift(dst,dst,-0,-0,BORDER_REPLICATE); Mat(dst(Rect(4,4,src.size().width*2,src.size().height*2))).copyTo(dest); }
void addNoise(InputArray src_, OutputArray dest_, const double sigma, const double sprate, const int seed) { if(seed!=0) cv::theRNG().state = seed; if (dest_.empty() || dest_.size() != src_.size() || dest_.type() != src_.type()) dest_.create(src_.size(), src_.type()); Mat src = src_.getMat(); Mat dest = dest_.getMat(); if (src.channels() == 1) { addNoiseMono(src, dest, sigma); if (sprate != 0)addNoiseSoltPepperMono(dest, dest, sprate, seed); return; } else { vector<Mat> s(src.channels()); vector<Mat> d(src.channels()); split(src, s); for (int i = 0; i < src.channels(); i++) { addNoiseMono(s[i], d[i], sigma); if (sprate != 0)addNoiseSoltPepperMono(d[i], d[i], sprate, seed); } cv::merge(d, dest); } if (seed != 0) cv::theRNG().state = cv::getTickCount(); }
cv::GlBuffer::GlBuffer(InputArray mat_, Usage _usage) : rows_(0), cols_(0), type_(0), usage_(_usage) { #ifndef HAVE_OPENGL (void)mat_; (void)_usage; throw_nogl; #else int kind = mat_.kind(); Size _size = mat_.size(); int _type = mat_.type(); if (kind == _InputArray::GPU_MAT) { #if !defined HAVE_CUDA || defined(CUDA_DISABLER) throw_nocuda; #else GpuMat d_mat = mat_.getGpuMat(); impl_ = new Impl(d_mat.rows, d_mat.cols, d_mat.type(), _usage); impl_->copyFrom(d_mat); #endif } else { Mat mat = mat_.getMat(); impl_ = new Impl(mat, _usage); } rows_ = _size.height; cols_ = _size.width; type_ = _type; #endif }
static bool ocl_integral( InputArray _src, OutputArray _sum, int sdepth ) { if ( _src.type() != CV_8UC1 || _src.step() % vlen != 0 || _src.offset() % vlen != 0 || !(sdepth == CV_32S || sdepth == CV_32F) ) return false; ocl::Kernel k1("integral_sum_cols", ocl::imgproc::integral_sum_oclsrc, format("-D sdepth=%d", sdepth)); if (k1.empty()) return false; Size size = _src.size(), t_size = Size(((size.height + vlen - 1) / vlen) * vlen, size.width), ssize(size.width + 1, size.height + 1); _sum.create(ssize, sdepth); UMat src = _src.getUMat(), t_sum(t_size, sdepth), sum = _sum.getUMat(); t_sum = t_sum(Range::all(), Range(0, size.height)); int offset = (int)src.offset / vlen, pre_invalid = (int)src.offset % vlen; int vcols = (pre_invalid + src.cols + vlen - 1) / vlen; int sum_offset = (int)sum.offset / vlen; k1.args(ocl::KernelArg::PtrReadOnly(src), ocl::KernelArg::PtrWriteOnly(t_sum), offset, pre_invalid, src.rows, src.cols, (int)src.step, (int)t_sum.step); size_t gt = ((vcols + 1) / 2) * 256, lt = 256; if (!k1.run(1, >, <, false)) return false; ocl::Kernel k2("integral_sum_rows", ocl::imgproc::integral_sum_oclsrc, format("-D sdepth=%d", sdepth)); k2.args(ocl::KernelArg::PtrReadWrite(t_sum), ocl::KernelArg::PtrWriteOnly(sum), t_sum.rows, t_sum.cols, (int)t_sum.step, (int)sum.step, sum_offset); size_t gt2 = t_sum.cols * 32, lt2 = 256; return k2.run(1, >2, <2, false); }
static bool matchTemplate_SQDIFF(InputArray _image, InputArray _templ, OutputArray _result) { if (useNaive(_templ.size())) return( matchTemplateNaive_SQDIFF(_image, _templ, _result)); else { matchTemplate(_image, _templ, _result, CV_TM_CCORR); int type = _image.type(), cn = CV_MAT_CN(type); ocl::Kernel k("matchTemplate_Prepared_SQDIFF", ocl::imgproc::match_template_oclsrc, format("-D SQDIFF_PREPARED -D T=%s -D cn=%d", ocl::typeToStr(type), cn)); if (k.empty()) return false; UMat image = _image.getUMat(), templ = _templ.getUMat(); _result.create(image.rows - templ.rows + 1, image.cols - templ.cols + 1, CV_32F); UMat result = _result.getUMat(); UMat image_sums, image_sqsums; integral(image.reshape(1), image_sums, image_sqsums, CV_32F, CV_32F); UMat templ_sqsum; if (!sumTemplate(_templ, templ_sqsum)) return false; k.args(ocl::KernelArg::ReadOnlyNoSize(image_sqsums), ocl::KernelArg::ReadWrite(result), templ.rows, templ.cols, ocl::KernelArg::PtrReadOnly(templ_sqsum)); size_t globalsize[2] = { result.cols, result.rows }; return k.run(2, globalsize, NULL, false); } }
void TranslationWarperBase<P>::warpBackward(InputArray src, InputArray K, InputArray R, InputArray t, int interp_mode, int border_mode, Size dst_size, OutputArray dst) { projector_.setCameraParams(K, R, t); Point src_tl, src_br; detectResultRoi(dst_size, src_tl, src_br); Size size = src.size(); CV_Assert(src_br.x - src_tl.x + 1 == size.width && src_br.y - src_tl.y + 1 == size.height); Mat xmap(dst_size, CV_32F); Mat ymap(dst_size, CV_32F); float u, v; for (int y = 0; y < dst_size.height; ++y) { for (int x = 0; x < dst_size.width; ++x) { projector_.mapForward(static_cast<float>(x), static_cast<float>(y), u, v); xmap.at<float>(y, x) = u - src_tl.x; ymap.at<float>(y, x) = v - src_tl.y; } } dst.create(dst_size, src.type()); remap(src, dst, xmap, ymap, interp_mode, border_mode); }
void BackgroundSubtractorGMG::updateBackgroundModel(InputArray _mask) { CV_Assert(_mask.size() == Size(imWidth,imHeight)); // mask should be same size as image Mat maskImg = _mask.getMat(); //#pragma omp parallel for (int i = 0; i < imHeight; ++i) { //#pragma omp parallel for (int j = 0; j < imWidth; ++j) { if (frameNum <= numInitializationFrames + 1) { // insert previously observed feature into the histogram. -1.0 parameter indicates training. pixels[i*imWidth+j].insertFeature(-1.0); if (frameNum >= numInitializationFrames+1) // training is done, normalize { pixels[i*imWidth+j].normalizeHistogram(); } } // if mask is 0, pixel is identified as a background pixel, so update histogram. else if (maskImg.at<uchar>(i,j) == 0) { pixels[i*imWidth+j].insertFeature(learningRate); // updates the histogram for the next iteration. } } } }
void repeat(InputArray _src, int ny, int nx, OutputArray _dst) { CV_Assert( _src.dims() <= 2 ); CV_Assert( ny > 0 && nx > 0 ); Size ssize = _src.size(); _dst.create(ssize.height*ny, ssize.width*nx, _src.type()); CV_OCL_RUN(_dst.isUMat(), ocl_repeat(_src, ny, nx, _dst)) Mat src = _src.getMat(), dst = _dst.getMat(); Size dsize = dst.size(); 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 ) memcpy( dst.data + y*dst.step + x, src.data + y*src.step, ssize.width ); } for( ; y < dsize.height; y++ ) memcpy( dst.data + y*dst.step, dst.data + (y - ssize.height)*dst.step, dsize.width ); }
ObjectState HTCamshift::update(InputArray _rgbFrame, InputArray _depthFrame, InputArray _mask) { CV_Assert(_rgbFrame.size() == rgbFrameSize && _rgbFrame.type() == rgbFrameType); if(useDepth == true) { CV_Assert(_depthFrame.size() == depthFrameSize && _depthFrame.type() == depthFrameType); } split(_rgbFrame, channels); if(state.valid == false) { calculateFeatureSet(_rgbFrame, _depthFrame); } calculateBackPro(_rgbFrame, _depthFrame, _mask); Rect bounding = state.location.boundingRect(); state.location = CamShift(backPro, bounding, term); return state; }
static bool ocl_fastNlMeansDenoising(InputArray _src, OutputArray _dst, float h, int templateWindowSize, int searchWindowSize) { int type = _src.type(), cn = CV_MAT_CN(type); Size size = _src.size(); if ( type != CV_8UC1 || type != CV_8UC2 || type != CV_8UC4 ) return false; int templateWindowHalfWize = templateWindowSize / 2; int searchWindowHalfSize = searchWindowSize / 2; templateWindowSize = templateWindowHalfWize * 2 + 1; searchWindowSize = searchWindowHalfSize * 2 + 1; int nblocksx = divUp(size.width, BLOCK_COLS), nblocksy = divUp(size.height, BLOCK_ROWS); int almostTemplateWindowSizeSqBinShift = -1; char cvt[2][40]; String opts = format("-D OP_CALC_FASTNLMEANS -D TEMPLATE_SIZE=%d -D SEARCH_SIZE=%d" " -D uchar_t=%s -D int_t=%s -D BLOCK_COLS=%d -D BLOCK_ROWS=%d" " -D CTA_SIZE=%d -D TEMPLATE_SIZE2=%d -D SEARCH_SIZE2=%d" " -D convert_int_t=%s -D cn=%d -D CTA_SIZE2=%d -D convert_uchar_t=%s", templateWindowSize, searchWindowSize, ocl::typeToStr(type), ocl::typeToStr(CV_32SC(cn)), BLOCK_COLS, BLOCK_ROWS, CTA_SIZE, templateWindowHalfWize, searchWindowHalfSize, ocl::convertTypeStr(CV_8U, CV_32S, cn, cvt[0]), cn, CTA_SIZE >> 1, ocl::convertTypeStr(CV_32S, CV_8U, cn, cvt[1])); ocl::Kernel k("fastNlMeansDenoising", ocl::photo::nlmeans_oclsrc, opts); if (k.empty()) return false; UMat almostDist2Weight; if (!ocl_calcAlmostDist2Weight<float>(almostDist2Weight, searchWindowSize, templateWindowSize, h, cn, almostTemplateWindowSizeSqBinShift)) return false; CV_Assert(almostTemplateWindowSizeSqBinShift >= 0); UMat srcex; int borderSize = searchWindowHalfSize + templateWindowHalfWize; copyMakeBorder(_src, srcex, borderSize, borderSize, borderSize, borderSize, BORDER_DEFAULT); _dst.create(size, type); UMat dst = _dst.getUMat(); int searchWindowSizeSq = searchWindowSize * searchWindowSize; Size upColSumSize(size.width, searchWindowSizeSq * nblocksy); Size colSumSize(nblocksx * templateWindowSize, searchWindowSizeSq * nblocksy); UMat buffer(upColSumSize + colSumSize, CV_32SC(cn)); srcex = srcex(Rect(Point(borderSize, borderSize), size)); k.args(ocl::KernelArg::ReadOnlyNoSize(srcex), ocl::KernelArg::WriteOnly(dst), ocl::KernelArg::PtrReadOnly(almostDist2Weight), ocl::KernelArg::PtrReadOnly(buffer), almostTemplateWindowSizeSqBinShift); size_t globalsize[2] = { nblocksx * CTA_SIZE, nblocksy }, localsize[2] = { CTA_SIZE, 1 }; return k.run(2, globalsize, localsize, false); }
void cv::imshow( const String& winname, InputArray _img ) { CV_TRACE_FUNCTION(); const Size size = _img.size(); #ifndef HAVE_OPENGL CV_Assert(size.width>0 && size.height>0); { Mat img = _img.getMat(); CvMat c_img = cvMat(img); cvShowImage(winname.c_str(), &c_img); } #else const double useGl = getWindowProperty(winname, WND_PROP_OPENGL); CV_Assert(size.width>0 && size.height>0); if (useGl <= 0) { Mat img = _img.getMat(); CvMat c_img = cvMat(img); cvShowImage(winname.c_str(), &c_img); } else { const double autoSize = getWindowProperty(winname, WND_PROP_AUTOSIZE); if (autoSize > 0) { resizeWindow(winname, size.width, size.height); } setOpenGlContext(winname); cv::ogl::Texture2D& tex = ownWndTexs[winname]; if (_img.kind() == _InputArray::CUDA_GPU_MAT) { cv::ogl::Buffer& buf = ownWndBufs[winname]; buf.copyFrom(_img); buf.setAutoRelease(false); tex.copyFrom(buf); tex.setAutoRelease(false); } else { tex.copyFrom(_img); } tex.setAutoRelease(false); setOpenGlDrawCallback(winname, glDrawTextureCallback, &tex); updateWindow(winname); } #endif }
void cv::Sobel( InputArray _src, OutputArray _dst, int ddepth, int dx, int dy, int ksize, double scale, double delta, int borderType ) { int stype = _src.type(), sdepth = CV_MAT_DEPTH(stype), cn = CV_MAT_CN(stype); if (ddepth < 0) ddepth = sdepth; int dtype = CV_MAKE_TYPE(ddepth, cn); _dst.create( _src.size(), dtype ); #ifdef HAVE_TEGRA_OPTIMIZATION if (tegra::useTegra() && scale == 1.0 && delta == 0) { Mat src = _src.getMat(), dst = _dst.getMat(); if (ksize == 3 && tegra::sobel3x3(src, dst, dx, dy, borderType)) return; if (ksize == -1 && tegra::scharr(src, dst, dx, dy, borderType)) return; } #endif #ifdef HAVE_IPP CV_IPP_CHECK() { if (ksize < 0) { if (IPPDerivScharr(_src, _dst, ddepth, dx, dy, scale, delta, borderType)) { CV_IMPL_ADD(CV_IMPL_IPP); return; } } else if (0 < ksize) { if (IPPDerivSobel(_src, _dst, ddepth, dx, dy, ksize, scale, delta, borderType)) { CV_IMPL_ADD(CV_IMPL_IPP); return; } } } #endif int ktype = std::max(CV_32F, std::max(ddepth, sdepth)); Mat kx, ky; getDerivKernels( kx, ky, dx, dy, ksize, false, ktype ); if( scale != 1 ) { // usually the smoothing part is the slowest to compute, // so try to scale it instead of the faster differenciating part if( dx == 0 ) kx *= scale; else ky *= scale; } sepFilter2D( _src, _dst, ddepth, kx, ky, Point(-1, -1), delta, borderType ); }
Point CylindricalWarper::warp(InputArray src, InputArray K, InputArray R, int interp_mode, int border_mode, OutputArray dst) { UMat uxmap, uymap; Rect dst_roi = buildMaps(src.size(), K, R, uxmap, uymap); dst.create(dst_roi.height + 1, dst_roi.width + 1, src.type()); remap(src, dst, uxmap, uymap, interp_mode, border_mode); return dst_roi.tl(); }
static bool ocl_integral( InputArray _src, OutputArray _sum, OutputArray _sqsum, int sdepth, int sqdepth ) { bool doubleSupport = ocl::Device::getDefault().doubleFPConfig() > 0; if ( _src.type() != CV_8UC1 || _src.step() % vlen != 0 || _src.offset() % vlen != 0 || (!doubleSupport && (sdepth == CV_64F || sqdepth == CV_64F)) ) return false; char cvt[40]; String opts = format("-D sdepth=%d -D sqdepth=%d -D TYPE=%s -D TYPE4=%s4 -D convert_TYPE4=%s%s", sdepth, sqdepth, ocl::typeToStr(sqdepth), ocl::typeToStr(sqdepth), ocl::convertTypeStr(sdepth, sqdepth, 4, cvt), doubleSupport ? " -D DOUBLE_SUPPORT" : ""); ocl::Kernel k1("integral_cols", ocl::imgproc::integral_sqrsum_oclsrc, opts); if (k1.empty()) return false; Size size = _src.size(), dsize = Size(size.width + 1, size.height + 1), t_size = Size(((size.height + vlen - 1) / vlen) * vlen, size.width); UMat src = _src.getUMat(), t_sum(t_size, sdepth), t_sqsum(t_size, sqdepth); t_sum = t_sum(Range::all(), Range(0, size.height)); t_sqsum = t_sqsum(Range::all(), Range(0, size.height)); _sum.create(dsize, sdepth); _sqsum.create(dsize, sqdepth); UMat sum = _sum.getUMat(), sqsum = _sqsum.getUMat(); int offset = src.offset / vlen; int pre_invalid = src.offset % vlen; int vcols = (pre_invalid + src.cols + vlen - 1) / vlen; int sum_offset = sum.offset / sum.elemSize(); int sqsum_offset = sqsum.offset / sqsum.elemSize(); CV_Assert(sqsum.offset % sqsum.elemSize() == 0); k1.args(ocl::KernelArg::PtrReadOnly(src), ocl::KernelArg::PtrWriteOnly(t_sum), ocl::KernelArg::PtrWriteOnly(t_sqsum), offset, pre_invalid, src.rows, src.cols, (int)src.step, (int)t_sum.step, (int)t_sqsum.step); size_t gt = ((vcols + 1) / 2) * 256, lt = 256; if (!k1.run(1, >, <, false)) return false; ocl::Kernel k2("integral_rows", ocl::imgproc::integral_sqrsum_oclsrc, opts); if (k2.empty()) return false; k2.args(ocl::KernelArg::PtrReadOnly(t_sum), ocl::KernelArg::PtrReadOnly(t_sqsum), ocl::KernelArg::PtrWriteOnly(sum), ocl::KernelArg::PtrWriteOnly(sqsum), t_sum.rows, t_sum.cols, (int)t_sum.step, (int)t_sqsum.step, (int)sum.step, (int)sqsum.step, sum_offset, sqsum_offset); size_t gt2 = t_sum.cols * 32, lt2 = 256; return k2.run(1, >2, <2, false); }
Point PlaneWarperOcl::warp(InputArray src, InputArray K, InputArray R, InputArray T, int interp_mode, int border_mode, OutputArray dst) { UMat uxmap, uymap; Rect dst_roi = buildMaps(src.size(), K, R, T, uxmap, uymap); dst.create(dst_roi.height + 1, dst_roi.width + 1, src.type()); UMat udst = dst.getUMat(); remap(src, udst, uxmap, uymap, interp_mode, border_mode); return dst_roi.tl(); }
void Picture::setContent(const InputArray &image) { if (image.size() == this->size()) { image.getMat().convertTo(*this, this->getType()); return; } Mat resizeImage; cv::resize(image, resizeImage, this->size()); resizeImage.convertTo(*this, this->getType()); }
static bool ocl_sepFilter3x3_8UC1(InputArray _src, OutputArray _dst, int ddepth, InputArray _kernelX, InputArray _kernelY, double delta, int borderType) { const ocl::Device & dev = ocl::Device::getDefault(); int type = _src.type(), sdepth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type); if ( !(dev.isIntel() && (type == CV_8UC1) && (ddepth == CV_8U) && (_src.offset() == 0) && (_src.step() % 4 == 0) && (_src.cols() % 16 == 0) && (_src.rows() % 2 == 0)) ) return false; Mat kernelX = _kernelX.getMat().reshape(1, 1); if (kernelX.cols % 2 != 1) return false; Mat kernelY = _kernelY.getMat().reshape(1, 1); if (kernelY.cols % 2 != 1) return false; if (ddepth < 0) ddepth = sdepth; Size size = _src.size(); size_t globalsize[2] = { 0, 0 }; size_t localsize[2] = { 0, 0 }; globalsize[0] = size.width / 16; globalsize[1] = size.height / 2; const char * const borderMap[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", 0, "BORDER_REFLECT_101" }; char build_opts[1024]; sprintf(build_opts, "-D %s %s%s", borderMap[borderType], ocl::kernelToStr(kernelX, CV_32F, "KERNEL_MATRIX_X").c_str(), ocl::kernelToStr(kernelY, CV_32F, "KERNEL_MATRIX_Y").c_str()); ocl::Kernel kernel("sepFilter3x3_8UC1_cols16_rows2", cv::ocl::imgproc::sepFilter3x3_oclsrc, build_opts); if (kernel.empty()) return false; UMat src = _src.getUMat(); _dst.create(size, CV_MAKETYPE(ddepth, cn)); if (!(_dst.offset() == 0 && _dst.step() % 4 == 0)) return false; UMat dst = _dst.getUMat(); int idxArg = kernel.set(0, ocl::KernelArg::PtrReadOnly(src)); idxArg = kernel.set(idxArg, (int)src.step); idxArg = kernel.set(idxArg, ocl::KernelArg::PtrWriteOnly(dst)); idxArg = kernel.set(idxArg, (int)dst.step); idxArg = kernel.set(idxArg, (int)dst.rows); idxArg = kernel.set(idxArg, (int)dst.cols); idxArg = kernel.set(idxArg, static_cast<float>(delta)); return kernel.run(2, globalsize, (localsize[0] == 0) ? NULL : localsize, false); }
void warpShift(InputArray src_, OutputArray dest_, int shiftx, int shifty, int borderType) { Mat src = src_.getMat(); if(dest_.empty() ||dest_.size()!=src_.size() || dest_.type() != src_.type()) dest_.create( src.size(), src.type() ); Mat dest = dest_.getMat(); if(borderType<0) warpShift_(src,dest,shiftx,shifty); else warpShift_(src,dest,shiftx,shifty,borderType); }
Point TranslationWarperBase<P>::warp(InputArray src, InputArray K, InputArray R, InputArray t, int interp_mode, int border_mode, OutputArray dst) { UMat xmap, ymap; Rect dst_roi = buildMaps(src.size(), K, R, t, xmap, ymap); dst.create(dst_roi.height + 1, dst_roi.width + 1, src.type()); remap(src, dst, xmap, ymap, interp_mode, border_mode); return dst_roi.tl(); }
static void _prepareImgAndDrawKeypoints( InputArray img1, const std::vector<KeyPoint>& keypoints1, InputArray img2, const std::vector<KeyPoint>& keypoints2, InputOutputArray _outImg, Mat& outImg1, Mat& outImg2, const Scalar& singlePointColor, DrawMatchesFlags flags ) { Mat outImg; Size img1size = img1.size(), img2size = img2.size(); Size size( img1size.width + img2size.width, MAX(img1size.height, img2size.height) ); if( !!(flags & DrawMatchesFlags::DRAW_OVER_OUTIMG) ) { outImg = _outImg.getMat(); if( size.width > outImg.cols || size.height > outImg.rows ) CV_Error( Error::StsBadSize, "outImg has size less than need to draw img1 and img2 together" ); outImg1 = outImg( Rect(0, 0, img1size.width, img1size.height) ); outImg2 = outImg( Rect(img1size.width, 0, img2size.width, img2size.height) ); } else { const int cn1 = img1.channels(), cn2 = img2.channels(); const int out_cn = std::max(3, std::max(cn1, cn2)); _outImg.create(size, CV_MAKETYPE(img1.depth(), out_cn)); outImg = _outImg.getMat(); outImg = Scalar::all(0); outImg1 = outImg( Rect(0, 0, img1size.width, img1size.height) ); outImg2 = outImg( Rect(img1size.width, 0, img2size.width, img2size.height) ); _prepareImage(img1, outImg1); _prepareImage(img2, outImg2); } // draw keypoints if( !(flags & DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS) ) { Mat _outImg1 = outImg( Rect(0, 0, img1size.width, img1size.height) ); drawKeypoints( _outImg1, keypoints1, _outImg1, singlePointColor, flags | DrawMatchesFlags::DRAW_OVER_OUTIMG ); Mat _outImg2 = outImg( Rect(img1size.width, 0, img2size.width, img2size.height) ); drawKeypoints( _outImg2, keypoints2, _outImg2, singlePointColor, flags | DrawMatchesFlags::DRAW_OVER_OUTIMG ); } }
void cv::ogl::Buffer::copyFrom(InputArray arr, Target target, bool autoRelease) { #ifndef HAVE_OPENGL (void) arr; (void) target; (void) autoRelease; throw_nogl(); #else const int kind = arr.kind(); if (kind == _InputArray::OPENGL_TEXTURE) { ogl::Texture2D tex = arr.getOGlTexture2D(); tex.copyTo(*this); setAutoRelease(autoRelease); return; } const Size asize = arr.size(); const int atype = arr.type(); create(asize, atype, target, autoRelease); switch (kind) { case _InputArray::OPENGL_BUFFER: { ogl::Buffer buf = arr.getOGlBuffer(); impl_->copyFrom(buf.bufId(), asize.area() * CV_ELEM_SIZE(atype)); break; } case _InputArray::GPU_MAT: { #if !defined HAVE_CUDA || defined(CUDA_DISABLER) throw_nocuda(); #else GpuMat dmat = arr.getGpuMat(); impl_->copyFrom(dmat.data, dmat.step, dmat.cols * dmat.elemSize(), dmat.rows); #endif break; } default: { Mat mat = arr.getMat(); CV_Assert( mat.isContinuous() ); impl_->copyFrom(asize.area() * CV_ELEM_SIZE(atype), mat.data); } } #endif }
static void displayFlow( InputArray _flow, OutputArray _img ) { const Size sz = _flow.size(); Mat flow = _flow.getMat(); _img.create( sz, CV_32FC3 ); Mat img = _img.getMat(); for ( int i = 0; i < sz.height; ++i ) for ( int j = 0; j < sz.width; ++j ) img.at< Vec3f >( i, j ) = getFlowColor( flow.at< Point2f >( i, j ) ); cvtColor( img, img, COLOR_HSV2BGR ); }
void DescriptorExtractor::compute( InputArray image, std::vector<KeyPoint>& keypoints, OutputArray descriptors ) const { if( image.empty() || keypoints.empty() ) { descriptors.release(); return; } KeyPointsFilter::runByImageBorder( keypoints, image.size(), 0 ); KeyPointsFilter::runByKeypointSize( keypoints, std::numeric_limits<float>::epsilon() ); computeImpl( image, keypoints, descriptors ); }
inline bool checkScalar(InputArray sc, int atype, int sckind, int akind) { if( sc.dims() > 2 || !sc.isContinuous() ) return false; Size sz = sc.size(); if(sz.width != 1 && sz.height != 1) return false; int cn = CV_MAT_CN(atype); if( akind == _InputArray::MATX && sckind != _InputArray::MATX ) return false; return sz == Size(1, 1) || sz == Size(1, cn) || sz == Size(cn, 1) || (sz == Size(1, 4) && sc.type() == CV_64F && cn <= 4); }
void Detector::prepare(InputArray rawimage, OutputArray undistorted_img, OutputArray small_img) { Mat tmp_img; auto newcamera = getOptimalNewCameraMatrix(cameraMatrix, distCoeffs, rawimage.size(), 0); undistort(rawimage, tmp_img, cameraMatrix, distCoeffs, newcamera); tmp_img(TABLE_ROI).copyTo(undistorted_img); resize(undistorted_img, small_img, Size(), 0.5, 0.5); }
void show_hist(InputArray img) { int height = 200; Mat hist(height, 256, CV_8UC3, Scalar(255,255,255)); vector<int> bins (255); int max = 0; for (int i = 0; i < img.size().height * img.size().width ; i++) { auto val = img.getMat().data[i]; bins[val] ++; if (bins[val] > max) { max = bins[val]; } } for (int i; i <= 255; i++) { line(hist, Point(i,height), Point(i, height - (int)(bins[i] * (double)(height)/max) ), Scalar(0,0,0), 1); } imshow("Histo", hist); }
static bool matchTemplate_CCOEFF(InputArray _image, InputArray _templ, OutputArray _result) { matchTemplate(_image, _templ, _result, CV_TM_CCORR); UMat image_sums, temp; integral(_image, temp); if (temp.depth() == CV_64F) temp.convertTo(image_sums, CV_32F); else image_sums = temp; int type = image_sums.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type); ocl::Kernel k("matchTemplate_Prepared_CCOEFF", ocl::imgproc::match_template_oclsrc, format("-D CCOEFF -D T=%s -D elem_type=%s -D cn=%d", ocl::typeToStr(type), ocl::typeToStr(depth), cn)); if (k.empty()) return false; UMat templ = _templ.getUMat(); Size size = _image.size(), tsize = templ.size(); _result.create(size.height - templ.rows + 1, size.width - templ.cols + 1, CV_32F); UMat result = _result.getUMat(); if (cn == 1) { float templ_sum = static_cast<float>(sum(_templ)[0]) / tsize.area(); k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::ReadWrite(result), templ.rows, templ.cols, templ_sum); } else { Vec4f templ_sum = Vec4f::all(0); templ_sum = sum(templ) / tsize.area(); if (cn == 2) k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::ReadWrite(result), templ.rows, templ.cols, templ_sum[0], templ_sum[1]); else if (cn==3) k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::ReadWrite(result), templ.rows, templ.cols, templ_sum[0], templ_sum[1], templ_sum[2]); else k.args(ocl::KernelArg::ReadOnlyNoSize(image_sums), ocl::KernelArg::ReadWrite(result), templ.rows, templ.cols, templ_sum[0], templ_sum[1], templ_sum[2], templ_sum[3]); } size_t globalsize[2] = { result.cols, result.rows }; return k.run(2, globalsize, NULL, false); }
static bool ocl_pyrUp( InputArray _src, OutputArray _dst, const Size& _dsz, int borderType) { int type = _src.type(), depth = CV_MAT_DEPTH(type), channels = CV_MAT_CN(type); if (channels > 4 || borderType != BORDER_DEFAULT) return false; bool doubleSupport = ocl::Device::getDefault().doubleFPConfig() > 0; if (depth == CV_64F && !doubleSupport) return false; Size ssize = _src.size(); if ((_dsz.area() != 0) && (_dsz != Size(ssize.width * 2, ssize.height * 2))) return false; UMat src = _src.getUMat(); Size dsize = Size(ssize.width * 2, ssize.height * 2); _dst.create( dsize, src.type() ); UMat dst = _dst.getUMat(); int float_depth = depth == CV_64F ? CV_64F : CV_32F; const int local_size = 16; char cvt[2][50]; String buildOptions = format( "-D T=%s -D FT=%s -D convertToT=%s -D convertToFT=%s%s " "-D T1=%s -D cn=%d -D LOCAL_SIZE=%d", ocl::typeToStr(type), ocl::typeToStr(CV_MAKETYPE(float_depth, channels)), ocl::convertTypeStr(float_depth, depth, channels, cvt[0]), ocl::convertTypeStr(depth, float_depth, channels, cvt[1]), doubleSupport ? " -D DOUBLE_SUPPORT" : "", ocl::typeToStr(depth), channels, local_size ); size_t globalThreads[2] = { dst.cols, dst.rows }; size_t localThreads[2] = { local_size, local_size }; ocl::Kernel k; if (ocl::Device::getDefault().isIntel() && channels == 1) { k.create("pyrUp_unrolled", ocl::imgproc::pyr_up_oclsrc, buildOptions); globalThreads[0] = dst.cols/2; globalThreads[1] = dst.rows/2; } else k.create("pyrUp", ocl::imgproc::pyr_up_oclsrc, buildOptions); if (k.empty()) return false; k.args(ocl::KernelArg::ReadOnly(src), ocl::KernelArg::WriteOnly(dst)); return k.run(2, globalThreads, localThreads, false); }
static bool ocl_pyrDown( InputArray _src, OutputArray _dst, const Size& _dsz, int borderType) { int type = _src.type(), depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type); bool doubleSupport = ocl::Device::getDefault().doubleFPConfig() > 0; if (cn > 4 || (depth == CV_64F && !doubleSupport)) return false; Size ssize = _src.size(); Size dsize = _dsz.area() == 0 ? Size((ssize.width + 1) / 2, (ssize.height + 1) / 2) : _dsz; if (dsize.height < 2 || dsize.width < 2) return false; CV_Assert( ssize.width > 0 && ssize.height > 0 && std::abs(dsize.width*2 - ssize.width) <= 2 && std::abs(dsize.height*2 - ssize.height) <= 2 ); UMat src = _src.getUMat(); _dst.create( dsize, src.type() ); UMat dst = _dst.getUMat(); int float_depth = depth == CV_64F ? CV_64F : CV_32F; const int local_size = 256; int kercn = 1; if (depth == CV_8U && float_depth == CV_32F && cn == 1 && ocl::Device::getDefault().isIntel()) kercn = 4; const char * const borderMap[] = { "BORDER_CONSTANT", "BORDER_REPLICATE", "BORDER_REFLECT", "BORDER_WRAP", "BORDER_REFLECT_101" }; char cvt[2][50]; String buildOptions = format( "-D T=%s -D FT=%s -D convertToT=%s -D convertToFT=%s%s " "-D T1=%s -D cn=%d -D kercn=%d -D fdepth=%d -D %s -D LOCAL_SIZE=%d", ocl::typeToStr(type), ocl::typeToStr(CV_MAKETYPE(float_depth, cn)), ocl::convertTypeStr(float_depth, depth, cn, cvt[0]), ocl::convertTypeStr(depth, float_depth, cn, cvt[1]), doubleSupport ? " -D DOUBLE_SUPPORT" : "", ocl::typeToStr(depth), cn, kercn, float_depth, borderMap[borderType], local_size ); ocl::Kernel k("pyrDown", ocl::imgproc::pyr_down_oclsrc, buildOptions); if (k.empty()) return false; k.args(ocl::KernelArg::ReadOnly(src), ocl::KernelArg::WriteOnly(dst)); size_t localThreads[2] = { local_size/kercn, 1 }; size_t globalThreads[2] = { (src.cols + (kercn-1))/kercn, (dst.rows + 1) / 2 }; return k.run(2, globalThreads, localThreads, false); }