Ask Your Question

The problem about iteration of gpumat

asked 2013-11-30 11:54:03 -0500

我干过豪哥 gravatar image

updated 2013-11-30 12:09:01 -0500

I want to plant a section of class Blender to support gpumat .just like above

 class CV_EXPORTS Blender

    virtual ~Blender() {}
    enum { NO, FEATHER, MULTI_BAND };
    static Ptr<Blender> createDefault(int type, bool try_gpu = false);

    void prepare(const std::vector<Point> &corners, const std::vector<Size> &sizes);
    virtual void prepare(Rect dst_roi);
    virtual void feed(const Mat &img, const Mat &mask, Point tl);
    virtual void blend(Mat &dst, Mat &dst_mask);
        Mat dst_, dst_mask_;

And when iterating a GpuMat matrix ,My project always crash and stop at a unfix row.But I have use the function "createContinuous"to create a continous matrix.

class Blender_n
    virtual ~Blender_n() {}

    //enum { NO, FEATHER, MULTI_BAND };
    static Ptr<Blender_n> createDefault();

    void prepare(const std::vector<Point> &corners, const std::vector<Size> &sizes);
    virtual void prepare(Rect dst_roi);
    virtual void feed(const gpu::GpuMat &img, const Mat &mask, Point tl);
    virtual void blend(gpu::GpuMat &dst, Mat &dst_mask);

    gpu::GpuMat dst_;Mat dst_mask_;
    Rect dst_roi_;

the functions like above

#include "blend_n.h"
using namespace gpu;
Ptr<Blender_n> Blender_n::createDefault()
    return new Blender_n();

    CV_Error(CV_StsBadArg, "unsupported blending method");
    return NULL;

void Blender_n::prepare(const vector<Point> &corners, const vector<Size> &sizes)
    prepare(resultRoi(corners, sizes));

void Blender_n::prepare(Rect dst_roi)

    dst_= cv::gpu::createContinuous(dst_roi.size(),CV_16UC3);// CV_16SC3);
    dst_mask_.create(dst_roi.size(), CV_8U);
    dst_roi_ = dst_roi;

void Blender_n::feed(const GpuMat &img, const Mat &mask, Point tl)

    CV_Assert(img.type() == CV_16UC3);//CV_16SC3);
    CV_Assert(mask.type() == CV_8U);
    int dx = tl.x - dst_roi_.x;
    int dy = tl.y - dst_roi_.y;

    int rows = img.rows;
    int cols = img.cols;

    for (int y = 0; y < rows; ++y)
        const Point3_<short> *src_row = img.ptr<Point3_<short> >(y);
        Point3_<short> *dst_row = dst_.ptr<Point3_<short> >(dy + y);
        const uchar *mask_row = mask.ptr<uchar>(y);
        uchar *dst_mask_row = dst_mask_.ptr<uchar>(dy + y);
        for (int x = 0; x < img.cols; ++x)
            if (mask_row[x])
                dst_row[dx + x] = src_row[x];
            dst_mask_row[dx + x] |= mask_row[x];

void Blender_n::blend(GpuMat &dst, Mat &dst_mask)
    GpuMat k1;
    dst_.setTo(Scalar::all(0), k1 );
    dst = dst_;
    dst_mask = dst_mask_;

when I use the funtion feed,it alway stop at " dst_row[dx + x] = src_row[x];".And the number of rows is random.Did someone try to iterate gpumat and meet the same problem?thank for your reply!

edit retag flag offensive close merge delete



我干过豪哥 gravatar image我干过豪哥 ( 2013-12-01 02:38:19 -0500 )edit

1 answer

Sort by » oldest newest most voted

answered 2013-12-01 03:35:15 -0500

Vladislav Vinogradov gravatar image

You can't iterate GpuMat data in host code. GpuMat data is stored in GPU memory and can be accessed only from GPU code. If you want to fill GpuMat you must:

  • fill Mat object in CPU code and upload it.


  • write your own CUDA kernel.
edit flag offensive delete link more


Thanks a lot! I realized that by reading tutorial and write a new cuda kernel.ΘΘ

我干过豪哥 gravatar image我干过豪哥 ( 2013-12-01 18:13:53 -0500 )edit

Question Tools


Asked: 2013-11-30 11:54:03 -0500

Seen: 837 times

Last updated: Dec 01 '13