使用 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