void cv::gpu::normalize(const GpuMat& src, GpuMat& dst, double a, double b, int norm_type, int dtype, const GpuMat& mask, GpuMat& norm_buf, GpuMat& cvt_buf) { double scale = 1, shift = 0; if (norm_type == NORM_MINMAX) { double smin = 0, smax = 0; double dmin = std::min(a, b), dmax = std::max(a, b); minMax(src, &smin, &smax, mask, norm_buf); scale = (dmax - dmin) * (smax - smin > numeric_limits<double>::epsilon() ? 1.0 / (smax - smin) : 0.0); shift = dmin - smin * scale; } else if (norm_type == NORM_L2 || norm_type == NORM_L1 || norm_type == NORM_INF) { scale = norm(src, norm_type, mask, norm_buf); scale = scale > numeric_limits<double>::epsilon() ? a / scale : 0.0; shift = 0; } else { CV_Error(CV_StsBadArg, "Unknown/unsupported norm type"); } if (mask.empty()) { src.convertTo(dst, dtype, scale, shift); } else { src.convertTo(cvt_buf, dtype, scale, shift); cvt_buf.copyTo(dst, mask); } }
void cv::gpu::PyrLKOpticalFlow::dense(const GpuMat& prevImg, const GpuMat& nextImg, GpuMat& u, GpuMat& v, GpuMat* err) { using namespace cv::gpu::device::pyrlk; CV_Assert(prevImg.type() == CV_8UC1); CV_Assert(prevImg.size() == nextImg.size() && prevImg.type() == nextImg.type()); CV_Assert(maxLevel >= 0); CV_Assert(winSize.width > 2 && winSize.height > 2); if (err) err->create(prevImg.size(), CV_32FC1); // build the image pyramids. prevPyr_.resize(maxLevel + 1); nextPyr_.resize(maxLevel + 1); prevPyr_[0] = prevImg; nextImg.convertTo(nextPyr_[0], CV_32F); for (int level = 1; level <= maxLevel; ++level) { pyrDown(prevPyr_[level - 1], prevPyr_[level]); pyrDown(nextPyr_[level - 1], nextPyr_[level]); } uPyr_.resize(2); vPyr_.resize(2); ensureSizeIsEnough(prevImg.size(), CV_32FC1, uPyr_[0]); ensureSizeIsEnough(prevImg.size(), CV_32FC1, vPyr_[0]); ensureSizeIsEnough(prevImg.size(), CV_32FC1, uPyr_[1]); ensureSizeIsEnough(prevImg.size(), CV_32FC1, vPyr_[1]); uPyr_[1].setTo(Scalar::all(0)); vPyr_[1].setTo(Scalar::all(0)); int2 winSize2i = make_int2(winSize.width, winSize.height); loadConstants(winSize2i, iters); DevMem2Df derr = err ? *err : DevMem2Df(); int idx = 0; for (int level = maxLevel; level >= 0; level--) { int idx2 = (idx + 1) & 1; lkDense_gpu(prevPyr_[level], nextPyr_[level], uPyr_[idx], vPyr_[idx], uPyr_[idx2], vPyr_[idx2], level == 0 ? derr : DevMem2Df(), winSize2i); if (level > 0) idx = idx2; } uPyr_[idx].copyTo(u); vPyr_[idx].copyTo(v); }
__host__ GpuMat_<T>::GpuMat_(const GpuMat& m, Allocator* allocator) : GpuMat(allocator) { flags = (flags & ~CV_MAT_TYPE_MASK) | DataType<T>::type; if (DataType<T>::type == m.type()) { GpuMat::operator =(m); return; } if (DataType<T>::depth == m.depth()) { GpuMat::operator =(m.reshape(DataType<T>::channels, m.rows)); return; } CV_Assert( DataType<T>::channels == m.channels() ); m.convertTo(*this, type()); }
static void csbp_operator(StereoConstantSpaceBP& rthis, GpuMat& mbuf, GpuMat& temp, GpuMat& out, const GpuMat& left, const GpuMat& right, GpuMat& disp, Stream& stream) { CV_DbgAssert(0 < rthis.ndisp && 0 < rthis.iters && 0 < rthis.levels && 0 < rthis.nr_plane && left.rows == right.rows && left.cols == right.cols && left.type() == right.type()); CV_Assert(rthis.levels <= 8 && (left.type() == CV_8UC1 || left.type() == CV_8UC3 || left.type() == CV_8UC4)); const Scalar zero = Scalar::all(0); cudaStream_t cudaStream = StreamAccessor::getStream(stream); //////////////////////////////////////////////////////////////////////////////////////////// // Init int rows = left.rows; int cols = left.cols; rthis.levels = min(rthis.levels, int(log((double)rthis.ndisp) / log(2.0))); int levels = rthis.levels; // compute sizes AutoBuffer<int> buf(levels * 3); int* cols_pyr = buf; int* rows_pyr = cols_pyr + levels; int* nr_plane_pyr = rows_pyr + levels; cols_pyr[0] = cols; rows_pyr[0] = rows; nr_plane_pyr[0] = rthis.nr_plane; for (int i = 1; i < levels; i++) { cols_pyr[i] = cols_pyr[i-1] / 2; rows_pyr[i] = rows_pyr[i-1] / 2; nr_plane_pyr[i] = nr_plane_pyr[i-1] * 2; } GpuMat u[2], d[2], l[2], r[2], disp_selected_pyr[2], data_cost, data_cost_selected; //allocate buffers int buffers_count = 10; // (up + down + left + right + disp_selected_pyr) * 2 buffers_count += 2; // data_cost has twice more rows than other buffers, what's why +2, not +1; buffers_count += 1; // data_cost_selected mbuf.create(rows * rthis.nr_plane * buffers_count, cols, DataType<T>::type); data_cost = mbuf.rowRange(0, rows * rthis.nr_plane * 2); data_cost_selected = mbuf.rowRange(data_cost.rows, data_cost.rows + rows * rthis.nr_plane); for(int k = 0; k < 2; ++k) // in/out { GpuMat sub1 = mbuf.rowRange(data_cost.rows + data_cost_selected.rows, mbuf.rows); GpuMat sub2 = sub1.rowRange((k+0)*sub1.rows/2, (k+1)*sub1.rows/2); GpuMat *buf_ptrs[] = { &u[k], &d[k], &l[k], &r[k], &disp_selected_pyr[k] }; for(int _r = 0; _r < 5; ++_r) { *buf_ptrs[_r] = sub2.rowRange(_r * sub2.rows/5, (_r+1) * sub2.rows/5); assert(buf_ptrs[_r]->cols == cols && buf_ptrs[_r]->rows == rows * rthis.nr_plane); } }; size_t elem_step = mbuf.step / sizeof(T); Size temp_size = data_cost.size(); if ((size_t)temp_size.area() < elem_step * rows_pyr[levels - 1] * rthis.ndisp) temp_size = Size(static_cast<int>(elem_step), rows_pyr[levels - 1] * rthis.ndisp); temp.create(temp_size, DataType<T>::type); //////////////////////////////////////////////////////////////////////////// // Compute load_constants(rthis.ndisp, rthis.max_data_term, rthis.data_weight, rthis.max_disc_term, rthis.disc_single_jump, rthis.min_disp_th, left, right, temp); if (stream) { stream.enqueueMemSet(l[0], zero); stream.enqueueMemSet(d[0], zero); stream.enqueueMemSet(r[0], zero); stream.enqueueMemSet(u[0], zero); stream.enqueueMemSet(l[1], zero); stream.enqueueMemSet(d[1], zero); stream.enqueueMemSet(r[1], zero); stream.enqueueMemSet(u[1], zero); stream.enqueueMemSet(data_cost, zero); stream.enqueueMemSet(data_cost_selected, zero); } else { l[0].setTo(zero); d[0].setTo(zero); r[0].setTo(zero); u[0].setTo(zero); l[1].setTo(zero); d[1].setTo(zero); r[1].setTo(zero); u[1].setTo(zero); data_cost.setTo(zero); data_cost_selected.setTo(zero); } int cur_idx = 0; for (int i = levels - 1; i >= 0; i--) { if (i == levels - 1) { init_data_cost(left.rows, left.cols, disp_selected_pyr[cur_idx].ptr<T>(), data_cost_selected.ptr<T>(), elem_step, rows_pyr[i], cols_pyr[i], i, nr_plane_pyr[i], rthis.ndisp, left.channels(), rthis.use_local_init_data_cost, cudaStream); } else { compute_data_cost(disp_selected_pyr[cur_idx].ptr<T>(), data_cost.ptr<T>(), elem_step, left.rows, left.cols, rows_pyr[i], cols_pyr[i], rows_pyr[i+1], i, nr_plane_pyr[i+1], left.channels(), cudaStream); int new_idx = (cur_idx + 1) & 1; init_message(u[new_idx].ptr<T>(), d[new_idx].ptr<T>(), l[new_idx].ptr<T>(), r[new_idx].ptr<T>(), u[cur_idx].ptr<T>(), d[cur_idx].ptr<T>(), l[cur_idx].ptr<T>(), r[cur_idx].ptr<T>(), disp_selected_pyr[new_idx].ptr<T>(), disp_selected_pyr[cur_idx].ptr<T>(), data_cost_selected.ptr<T>(), data_cost.ptr<T>(), elem_step, rows_pyr[i], cols_pyr[i], nr_plane_pyr[i], rows_pyr[i+1], cols_pyr[i+1], nr_plane_pyr[i+1], cudaStream); cur_idx = new_idx; } calc_all_iterations(u[cur_idx].ptr<T>(), d[cur_idx].ptr<T>(), l[cur_idx].ptr<T>(), r[cur_idx].ptr<T>(), data_cost_selected.ptr<T>(), disp_selected_pyr[cur_idx].ptr<T>(), elem_step, rows_pyr[i], cols_pyr[i], nr_plane_pyr[i], rthis.iters, cudaStream); } if (disp.empty()) disp.create(rows, cols, CV_16S); out = ((disp.type() == CV_16S) ? disp : (out.create(rows, cols, CV_16S), out)); if (stream) stream.enqueueMemSet(out, zero); else out.setTo(zero); compute_disp(u[cur_idx].ptr<T>(), d[cur_idx].ptr<T>(), l[cur_idx].ptr<T>(), r[cur_idx].ptr<T>(), data_cost_selected.ptr<T>(), disp_selected_pyr[cur_idx].ptr<T>(), elem_step, out, nr_plane_pyr[0], cudaStream); if (disp.type() != CV_16S) { if (stream) stream.enqueueConvert(out, disp, disp.type()); else out.convertTo(disp, disp.type()); } }
static void csbp_operator(StereoConstantSpaceBP& rthis, GpuMat u[2], GpuMat d[2], GpuMat l[2], GpuMat r[2], GpuMat disp_selected_pyr[2], GpuMat& data_cost, GpuMat& data_cost_selected, GpuMat& temp, GpuMat& out, const GpuMat& left, const GpuMat& right, GpuMat& disp, cudaStream_t stream) { CV_DbgAssert(0 < rthis.ndisp && 0 < rthis.iters && 0 < rthis.levels && 0 < rthis.nr_plane && left.rows == right.rows && left.cols == right.cols && left.type() == right.type()); CV_Assert(rthis.levels <= 8 && (left.type() == CV_8UC1 || left.type() == CV_8UC3)); const Scalar zero = Scalar::all(0); //////////////////////////////////////////////////////////////////////////////////////////// // Init int rows = left.rows; int cols = left.cols; rthis.levels = min(rthis.levels, int(log((double)rthis.ndisp) / log(2.0))); int levels = rthis.levels; AutoBuffer<int> buf(levels * 4); int* cols_pyr = buf; int* rows_pyr = cols_pyr + levels; int* nr_plane_pyr = rows_pyr + levels; int* step_pyr = nr_plane_pyr + levels; cols_pyr[0] = cols; rows_pyr[0] = rows; nr_plane_pyr[0] = rthis.nr_plane; const int n = 64; step_pyr[0] = alignSize(cols * sizeof(T), n) / sizeof(T); for (int i = 1; i < levels; i++) { cols_pyr[i] = (cols_pyr[i-1] + 1) / 2; rows_pyr[i] = (rows_pyr[i-1] + 1) / 2; nr_plane_pyr[i] = nr_plane_pyr[i-1] * 2; step_pyr[i] = alignSize(cols_pyr[i] * sizeof(T), n) / sizeof(T); } Size msg_size(step_pyr[0], rows * nr_plane_pyr[0]); Size data_cost_size(step_pyr[0], rows * nr_plane_pyr[0] * 2); u[0].create(msg_size, DataType<T>::type); d[0].create(msg_size, DataType<T>::type); l[0].create(msg_size, DataType<T>::type); r[0].create(msg_size, DataType<T>::type); u[1].create(msg_size, DataType<T>::type); d[1].create(msg_size, DataType<T>::type); l[1].create(msg_size, DataType<T>::type); r[1].create(msg_size, DataType<T>::type); disp_selected_pyr[0].create(msg_size, DataType<T>::type); disp_selected_pyr[1].create(msg_size, DataType<T>::type); data_cost.create(data_cost_size, DataType<T>::type); data_cost_selected.create(msg_size, DataType<T>::type); step_pyr[0] = data_cost.step / sizeof(T); Size temp_size = data_cost_size; if (data_cost_size.width * data_cost_size.height < step_pyr[levels - 1] * rows_pyr[levels - 1] * rthis.ndisp) temp_size = Size(step_pyr[levels - 1], rows_pyr[levels - 1] * rthis.ndisp); temp.create(temp_size, DataType<T>::type); //////////////////////////////////////////////////////////////////////////// // Compute csbp::load_constants(rthis.ndisp, rthis.max_data_term, rthis.data_weight, rthis.max_disc_term, rthis.disc_single_jump, rthis.min_disp_th, left, right, temp); l[0] = zero; d[0] = zero; r[0] = zero; u[0] = zero; l[1] = zero; d[1] = zero; r[1] = zero; u[1] = zero; data_cost = zero; data_cost_selected = zero; int cur_idx = 0; for (int i = levels - 1; i >= 0; i--) { if (i == levels - 1) { csbp::init_data_cost(left.rows, left.cols, disp_selected_pyr[cur_idx].ptr<T>(), data_cost_selected.ptr<T>(), step_pyr[i], rows_pyr[i], cols_pyr[i], i, nr_plane_pyr[i], rthis.ndisp, left.channels(), rthis.use_local_init_data_cost, stream); } else { csbp::compute_data_cost(disp_selected_pyr[cur_idx].ptr<T>(), data_cost.ptr<T>(), step_pyr[i], step_pyr[i+1], left.rows, left.cols, rows_pyr[i], cols_pyr[i], rows_pyr[i+1], i, nr_plane_pyr[i+1], left.channels(), stream); int new_idx = (cur_idx + 1) & 1; csbp::init_message(u[new_idx].ptr<T>(), d[new_idx].ptr<T>(), l[new_idx].ptr<T>(), r[new_idx].ptr<T>(), u[cur_idx].ptr<T>(), d[cur_idx].ptr<T>(), l[cur_idx].ptr<T>(), r[cur_idx].ptr<T>(), disp_selected_pyr[new_idx].ptr<T>(), disp_selected_pyr[cur_idx].ptr<T>(), data_cost_selected.ptr<T>(), data_cost.ptr<T>(), step_pyr[i], step_pyr[i+1], rows_pyr[i], cols_pyr[i], nr_plane_pyr[i], rows_pyr[i+1], cols_pyr[i+1], nr_plane_pyr[i+1], stream); cur_idx = new_idx; } csbp::calc_all_iterations(u[cur_idx].ptr<T>(), d[cur_idx].ptr<T>(), l[cur_idx].ptr<T>(), r[cur_idx].ptr<T>(), data_cost_selected.ptr<T>(), disp_selected_pyr[cur_idx].ptr<T>(), step_pyr[i], rows_pyr[i], cols_pyr[i], nr_plane_pyr[i], rthis.iters, stream); } if (disp.empty()) disp.create(rows, cols, CV_16S); out = ((disp.type() == CV_16S) ? disp : (out.create(rows, cols, CV_16S), out)); out = zero; csbp::compute_disp(u[cur_idx].ptr<T>(), d[cur_idx].ptr<T>(), l[cur_idx].ptr<T>(), r[cur_idx].ptr<T>(), data_cost_selected.ptr<T>(), disp_selected_pyr[cur_idx].ptr<T>(), step_pyr[0], out, nr_plane_pyr[0], stream); if (disp.type() != CV_16S) out.convertTo(disp, disp.type()); }
void cv::gpu::OpticalFlowDual_TVL1_GPU::operator ()(const GpuMat& I0, const GpuMat& I1, GpuMat& flowx, GpuMat& flowy) { CV_Assert( I0.type() == CV_8UC1 || I0.type() == CV_32FC1 ); CV_Assert( I0.size() == I1.size() ); CV_Assert( I0.type() == I1.type() ); CV_Assert( !useInitialFlow || (flowx.size() == I0.size() && flowx.type() == CV_32FC1 && flowy.size() == flowx.size() && flowy.type() == flowx.type()) ); CV_Assert( nscales > 0 ); // allocate memory for the pyramid structure I0s.resize(nscales); I1s.resize(nscales); u1s.resize(nscales); u2s.resize(nscales); I0.convertTo(I0s[0], CV_32F, I0.depth() == CV_8U ? 1.0 : 255.0); I1.convertTo(I1s[0], CV_32F, I1.depth() == CV_8U ? 1.0 : 255.0); if (!useInitialFlow) { flowx.create(I0.size(), CV_32FC1); flowy.create(I0.size(), CV_32FC1); } u1s[0] = flowx; u2s[0] = flowy; I1x_buf.create(I0.size(), CV_32FC1); I1y_buf.create(I0.size(), CV_32FC1); I1w_buf.create(I0.size(), CV_32FC1); I1wx_buf.create(I0.size(), CV_32FC1); I1wy_buf.create(I0.size(), CV_32FC1); grad_buf.create(I0.size(), CV_32FC1); rho_c_buf.create(I0.size(), CV_32FC1); p11_buf.create(I0.size(), CV_32FC1); p12_buf.create(I0.size(), CV_32FC1); p21_buf.create(I0.size(), CV_32FC1); p22_buf.create(I0.size(), CV_32FC1); diff_buf.create(I0.size(), CV_32FC1); // create the scales for (int s = 1; s < nscales; ++s) { gpu::resize(I0s[s-1], I0s[s], Size(), scaleStep, scaleStep); gpu::resize(I1s[s-1], I1s[s], Size(), scaleStep, scaleStep); if (I0s[s].cols < 16 || I0s[s].rows < 16) { nscales = s; break; } if (useInitialFlow) { gpu::resize(u1s[s-1], u1s[s], Size(), scaleStep, scaleStep); gpu::resize(u2s[s-1], u2s[s], Size(), scaleStep, scaleStep); gpu::multiply(u1s[s], Scalar::all(scaleStep), u1s[s]); gpu::multiply(u2s[s], Scalar::all(scaleStep), u2s[s]); } else { u1s[s].create(I0s[s].size(), CV_32FC1); u2s[s].create(I0s[s].size(), CV_32FC1); } } if (!useInitialFlow) { u1s[nscales-1].setTo(Scalar::all(0)); u2s[nscales-1].setTo(Scalar::all(0)); } // pyramidal structure for computing the optical flow for (int s = nscales - 1; s >= 0; --s) { // compute the optical flow at the current scale procOneScale(I0s[s], I1s[s], u1s[s], u2s[s]); // if this was the last scale, finish now if (s == 0) break; // otherwise, upsample the optical flow // zoom the optical flow for the next finer scale gpu::resize(u1s[s], u1s[s - 1], I0s[s - 1].size()); gpu::resize(u2s[s], u2s[s - 1], I0s[s - 1].size()); // scale the optical flow with the appropriate zoom factor gpu::multiply(u1s[s - 1], Scalar::all(1/scaleStep), u1s[s - 1]); gpu::multiply(u2s[s - 1], Scalar::all(1/scaleStep), u2s[s - 1]); } }
void cv::gpu::PyrLKOpticalFlow::sparse(const GpuMat& prevImg, const GpuMat& nextImg, const GpuMat& prevPts, GpuMat& nextPts, GpuMat& status, GpuMat* err) { using namespace cv::gpu::device::pyrlk; if (prevPts.empty()) { nextPts.release(); status.release(); if (err) err->release(); return; } dim3 block, patch; calcPatchSize(winSize, block, patch, isDeviceArch11_); CV_Assert(prevImg.type() == CV_8UC1 || prevImg.type() == CV_8UC3 || prevImg.type() == CV_8UC4); CV_Assert(prevImg.size() == nextImg.size() && prevImg.type() == nextImg.type()); CV_Assert(maxLevel >= 0); CV_Assert(winSize.width > 2 && winSize.height > 2); CV_Assert(patch.x > 0 && patch.x < 6 && patch.y > 0 && patch.y < 6); CV_Assert(prevPts.rows == 1 && prevPts.type() == CV_32FC2); if (useInitialFlow) CV_Assert(nextPts.size() == prevPts.size() && nextPts.type() == CV_32FC2); else ensureSizeIsEnough(1, prevPts.cols, prevPts.type(), nextPts); GpuMat temp1 = (useInitialFlow ? nextPts : prevPts).reshape(1); GpuMat temp2 = nextPts.reshape(1); multiply(temp1, Scalar::all(1.0 / (1 << maxLevel) / 2.0), temp2); ensureSizeIsEnough(1, prevPts.cols, CV_8UC1, status); status.setTo(Scalar::all(1)); if (err) ensureSizeIsEnough(1, prevPts.cols, CV_32FC1, *err); // build the image pyramids. prevPyr_.resize(maxLevel + 1); nextPyr_.resize(maxLevel + 1); int cn = prevImg.channels(); if (cn == 1 || cn == 4) { prevImg.convertTo(prevPyr_[0], CV_32F); nextImg.convertTo(nextPyr_[0], CV_32F); } else { cvtColor(prevImg, dx_calcBuf_, COLOR_BGR2BGRA); dx_calcBuf_.convertTo(prevPyr_[0], CV_32F); cvtColor(nextImg, dx_calcBuf_, COLOR_BGR2BGRA); dx_calcBuf_.convertTo(nextPyr_[0], CV_32F); } for (int level = 1; level <= maxLevel; ++level) { pyrDown(prevPyr_[level - 1], prevPyr_[level]); pyrDown(nextPyr_[level - 1], nextPyr_[level]); } loadConstants(make_int2(winSize.width, winSize.height), iters); for (int level = maxLevel; level >= 0; level--) { if (cn == 1) { lkSparse1_gpu(prevPyr_[level], nextPyr_[level], prevPts.ptr<float2>(), nextPts.ptr<float2>(), status.ptr(), level == 0 && err ? err->ptr<float>() : 0, prevPts.cols, level, block, patch); } else { lkSparse4_gpu(prevPyr_[level], nextPyr_[level], prevPts.ptr<float2>(), nextPts.ptr<float2>(), status.ptr(), level == 0 && err ? err->ptr<float>() : 0, prevPts.cols, level, block, patch); } } }
inline void Stream::enqueueConvert(const GpuMat& src, OutputArray dst, int dtype, double alpha, double beta) { src.convertTo(dst, dtype, alpha, beta, *this); }