使用 cuda 的 thrust 加速图像翻转

10 篇文章 0 订阅

使用 cuda 的 thrust, 实现 RGB 图像,绕 Y 轴翻转。

优点有如下:

  • 速度快,对于 1024 * 1024 的图像,大约 0.01 ms,远远小于 CPU 版本时间。
  • 翻转时,不需要额外的拷贝时间,使用内存少。

代码如下:

const uchar3* im_rgb_data = static_cast<uchar3*>(device_original_result.get());
ImageFlipFunctor functor(im_rgb_data, width, height);
thrust::transform(CountingIterator(0), CountingIterator(total), dout_flip_result, functor);
 
 /*! \brief Image Flip functor */
  class ImageFlipFunctor : public thrust::unary_function<int, uchar3>
  {
  public:

    ImageFlipFunctor(const uchar3* d_im_ptr, int width, int height)
      : m_d_im_ptr(d_im_ptr), m_width(width), m_height(height)
    {

    }

    __device__ uchar3 operator() (int out_idx) const
    {
      int x = out_idx % m_width;
      int y = out_idx / m_width;

      int flip_idx = (m_height - y - 1) * m_width + x;
      uchar3 ret = m_d_im_ptr[flip_idx];
      return ret;
    }

  private:
    int m_width;
    int m_height;
    const uchar3* m_d_im_ptr;

  }; // end of class ImageFlipFunctor

如果使用 CPU

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值