Opencl图片旋转编写

kernel函数代码为:

图像旋转是指把定义的图像绕某一点以逆时针或顺时针方向旋转一定的角度,通常是指绕图像的中心以逆时针方向旋转。
假设图像的左上角为(left, top),右下角为(right, bottom),则图像上任意点(x0, y0)绕其中心(xcenter, ycenter)逆时针旋转angle角度后,新的坐标位置(x′, y′)的计算公式为:
xcenter = (right - left + 1) / 2 + left;
ycenter = (bottom - top + 1) / 2 + top;
x′ = (x0 - xcenter) cosθ - (y0 - ycenter) sinθ + xcenter;
y′ = (x0 - xcenter) sinθ + (y0 - ycenter) cosθ + ycenter;



__kernel void image_rotate( __global uchar * src_data, __global uchar * dest_data, //Data in global memory 
 int W, int H, //Image Dimensions 
float sinTheta, float cosTheta ) //Rotation Parameters 
 {
 //Thread gets its index within index space 
 const int ix = get_global_id(0);
const int iy = get_global_id(1);
 int xc = W/2;
 int yc = H/2;


int xpos = ( ix-xc)*cosTheta - (iy-yc)*sinTheta+xc;
 int ypos = (ix-xc)*sinTheta + ( iy-yc)*cosTheta+yc;


if ((xpos>=0) && (xpos< W) && (ypos>=0) && (ypos< H)) //Bound Checking 
{
  dest_data[ypos*W+xpos]= src_data[iy*W+ix];
}
}


src_data为原始图像(灰度图)数据,dest_data为旋转后的图像数据。W、H分别为图像的高度和宽度。sinTheta和cosTheta是旋转参数。我在代码中实现了旋转90度,所以sinTheta为1,cosTheta为0,大家可以尝试其它的值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值