Пример #1
0
inline
Mat::Mat(const gpu::GpuMat& m)
    : flags(0), dims(0), rows(0), cols(0), data(0), refcount(0), datastart(0), dataend(0), datalimit(0), allocator(0), size(&rows)
{
    m.download(*this);
}
Пример #2
0
void App::handleKey(char key)
{
    switch (key)
    {
    case 27:
        running = false;
        break;
    case 'p': case 'P':
        printParams();
        break;
    case 'g': case 'G':
        if (left.channels() == 1 && p.method != Params::BM)
        {
            left = left_src;
            right = right_src;
        }
        else
        {
            cvtColor(left_src, left, CV_BGR2GRAY);
            cvtColor(right_src, right, CV_BGR2GRAY);
        }
        d_left.upload(left);
        d_right.upload(right);
        cout << "image_channels: " << left.channels() << endl;
        imshow("left", left);
        imshow("right", right);
        break;
    case 'm': case 'M':
        switch (p.method)
        {
        case Params::BM:
            p.method = Params::BP;
            break;
        case Params::BP:
            p.method = Params::CSBP;
            break;
        case Params::CSBP:
            p.method = Params::BM;
            break;
        }
        cout << "method: " << p.method_str() << endl;
        break;
    case 's': case 'S':
        if (p.method == Params::BM)
        {
            switch (bm.preset)
            {
            case gpu::StereoBM_GPU::BASIC_PRESET:
                bm.preset = gpu::StereoBM_GPU::PREFILTER_XSOBEL;
                break;
            case gpu::StereoBM_GPU::PREFILTER_XSOBEL:
                bm.preset = gpu::StereoBM_GPU::BASIC_PRESET;
                break;
            }
            cout << "prefilter_sobel: " << bm.preset << endl;
        }
        break;
    case '1':
        p.ndisp = p.ndisp == 1 ? 8 : p.ndisp + 8;
        cout << "ndisp: " << p.ndisp << endl;
        bm.ndisp = p.ndisp;
        bp.ndisp = p.ndisp;
        csbp.ndisp = p.ndisp;
        break;
    case 'q': case 'Q':
        p.ndisp = max(p.ndisp - 8, 1);
        cout << "ndisp: " << p.ndisp << endl;
        bm.ndisp = p.ndisp;
        bp.ndisp = p.ndisp;
        csbp.ndisp = p.ndisp;
        break;
    case '2':
        if (p.method == Params::BM)
        {
            bm.winSize = min(bm.winSize + 1, 51);
            cout << "win_size: " << bm.winSize << endl;
        }
        break;
    case 'w': case 'W':
        if (p.method == Params::BM)
        {
            bm.winSize = max(bm.winSize - 1, 2);
            cout << "win_size: " << bm.winSize << endl;
        }
        break;
    case '3':
        if (p.method == Params::BP)
        {
            bp.iters += 1;
            cout << "iter_count: " << bp.iters << endl;
        }
        else if (p.method == Params::CSBP)
        {
            csbp.iters += 1;
            cout << "iter_count: " << csbp.iters << endl;
        }
        break;
    case 'e': case 'E':
        if (p.method == Params::BP)
        {
            bp.iters = max(bp.iters - 1, 1);
            cout << "iter_count: " << bp.iters << endl;
        }
        else if (p.method == Params::CSBP)
        {
            csbp.iters = max(csbp.iters - 1, 1);
            cout << "iter_count: " << csbp.iters << endl;
        }
        break;
    case '4':
        if (p.method == Params::BP)
        {
            bp.levels += 1;
            cout << "level_count: " << bp.levels << endl;
        }
        else if (p.method == Params::CSBP)
        {
            csbp.levels += 1;
            cout << "level_count: " << csbp.levels << endl;
        }
        break;
    case 'r': case 'R':
        if (p.method == Params::BP)
        {
            bp.levels = max(bp.levels - 1, 1);
            cout << "level_count: " << bp.levels << endl;
        }
        else if (p.method == Params::CSBP)
        {
            csbp.levels = max(csbp.levels - 1, 1);
            cout << "level_count: " << csbp.levels << endl;
        }
        break;
    }
}
Пример #3
0
void MultiBandBlenderGpu::feed(const gpu::GpuMat &d_img, const gpu::GpuMat &d_mask, Point tl)
{
    CV_Assert(d_img.type() == CV_16SC3);
    CV_Assert(d_mask.type() == CV_8U);

    // Keep source image in memory with small border
    int gap = 3 * (1 << num_bands_);
    Point tl_new(max(dst_roi_.x, tl.x - gap),
                 max(dst_roi_.y, tl.y - gap));
    Point br_new(min(dst_roi_.br().x, tl.x + d_img.cols + gap),
                 min(dst_roi_.br().y, tl.y + d_img.rows + gap));

    // Ensure coordinates of top-left, bottom-right corners are divided by (1 << num_bands_).
    // After that scale between layers is exactly 2.
    //
    // We do it to avoid interpolation problems when keeping sub-images only. There is no such problem when
    // image is bordered to have size equal to the final image size, but this is too memory hungry approach.
    tl_new.x = dst_roi_.x + (((tl_new.x - dst_roi_.x) >> num_bands_) << num_bands_);
    tl_new.y = dst_roi_.y + (((tl_new.y - dst_roi_.y) >> num_bands_) << num_bands_);
    int width = br_new.x - tl_new.x;
    int height = br_new.y - tl_new.y;
    width += ((1 << num_bands_) - width % (1 << num_bands_)) % (1 << num_bands_);
    height += ((1 << num_bands_) - height % (1 << num_bands_)) % (1 << num_bands_);
    br_new.x = tl_new.x + width;
    br_new.y = tl_new.y + height;
    int dy = max(br_new.y - dst_roi_.br().y, 0);
    int dx = max(br_new.x - dst_roi_.br().x, 0);
    tl_new.x -= dx; br_new.x -= dx;
    tl_new.y -= dy; br_new.y -= dy;

    int top = tl.y - tl_new.y;
    int left = tl.x - tl_new.x;
    int bottom = br_new.y - tl.y - d_img.rows;
    int right = br_new.x - tl.x - d_img.cols;

    // Create the source image Laplacian pyramid
    gpu::GpuMat d_img_with_border;
    gpu::copyMakeBorder(d_img, d_img_with_border, top, bottom, left, right, BORDER_REFLECT, Scalar(), stream_);

    vector<gpu::GpuMat> d_src_pyr_laplace;
    createLaplacePyrGpu(d_img_with_border, num_bands_, d_src_pyr_laplace);

    // Create the weight map Gaussian pyramid
    gpu::GpuMat d_weight_map;
    stream_.enqueueConvert(d_mask, d_weight_map, CV_32F, 1./255.);

    vector<gpu::GpuMat> d_weight_pyr_gauss(num_bands_ + 1);
    gpu::copyMakeBorder(d_weight_map, d_weight_pyr_gauss[0], top, bottom, left, right, BORDER_CONSTANT, Scalar(), stream_);
    for (int i = 0; i < num_bands_; ++i)
        gpu::pyrDown(d_weight_pyr_gauss[i], d_weight_pyr_gauss[i + 1], stream_);

    int y_tl = tl_new.y - dst_roi_.y;
    int y_br = br_new.y - dst_roi_.y;
    int x_tl = tl_new.x - dst_roi_.x;
    int x_br = br_new.x - dst_roi_.x;

    // Add weighted layer of the source image to the final Laplacian pyramid layer
    gpu::GpuMat d_tmp;
    for (int i = 0; i <= num_bands_; ++i)
    {
        gpu::GpuMat d_src_roi = d_src_pyr_laplace[i](cv::Rect(0, 0, x_br-x_tl, y_br-y_tl));
        gpu::GpuMat d_dst_roi = d_dst_pyr_laplace_[i](cv::Rect(x_tl, y_tl, x_br-x_tl, y_br-y_tl));
        gpu::GpuMat d_weight_roi = d_weight_pyr_gauss[i](cv::Rect(0, 0, x_br-x_tl, y_br-y_tl));
        gpu::GpuMat d_dst_weight_roi = d_dst_band_weights_[i](cv::Rect(x_tl, y_tl, x_br-x_tl, y_br-y_tl));

        // dst_roi += src_roi * weight_roi;
        // dst_weight_roi += weight_roi;

        gpu::multiply(d_src_roi, d_weight_roi, d_tmp, 1, -1, stream_);
        gpu::add(d_dst_roi, d_tmp, d_dst_roi, gpu::GpuMat(), -1, stream_);
        gpu::add(d_dst_weight_roi, d_weight_roi, d_dst_weight_roi, gpu::GpuMat(), -1, stream_);

        x_tl /= 2; y_tl /= 2;
        x_br /= 2; y_br /= 2;
    }

    stream_.waitForCompletion();
}