OpenCL 第7课:旋转变换(1)
来源:互联网 发布:萌化软件 编辑:程序博客网 时间:2024/05/23 13:30
旋转是一个常用的处理功能。图片中所有的点以某一个点为轴,顺时或逆时方向旋转N个角度。我们利用OpenCL就可以对图片中所有的点进行并行转换,大大提高效率。
上两节中,我们编写了CL文件来传递数组的地址,这一节中我们会多加入几个参数传递。
首先我们先来看下图片旋转的原理。这里我们假设图片的旋转是以图片的中心点为轴。也就是(width/2,height/2)这个点。旋转的角度是任意值。图片旋转会出现这两种情况。
第一幅是原图,第二幅是旋转了30度,大家可以看到旋转后图片的一部份数据已经超出了原来图片的大小范围。怎么处理超出部份的数据呢。因为我们的图片数据是用数组来存储的,一般我们有两个方法,1、存储图片的数组做大些。2、超出图片部份的数据不显示。这里我们选择后者,方便。
下面我们来看下rotate.cl程序和主程序。
rotate.cl程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
__kernel
void
rotation(__global
int
* A,
__global
int
* B,
int
width,
int
height,
float
sinangle,
float
cosangle)
{
//获取索引号,这里是二维的,所以可以取两个
//否则另一个永远是0
int
col = get_global_id(0);
int
row = get_global_id(1);
//计算图形中心点
float
cx = ((
float
)width)/2;
float
cy = ((
float
)height)/2;
int
nx = (
int
)(cx + cosangle * ((
float
)col-cx) + sinangle * ((
float
)row-cy));
int
ny = (
int
)(cy + (-1*sinangle) * ((
float
)col-cx) + cosangle * ((
float
)row-cy));
//边界检测
if
(nx>=0 && nx<width && ny>=0 && ny<height)
{
B[nx + ny*width] = A[col + row*width];
}
}
主程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <string>
#include <conio.h>
#include <math.h>//数学库
#include <CL/cl.h>//包含CL的头文件
using
namespace
std;
//8x8数组
const
int
dim_x = 8;
const
int
dim_y = 8;
//45度的弧度
const
float
angle = 3.1415926f/4.0f;
//从外部文件获取cl内核代码
bool
GetFileData(
const
char
* fname,string& str)
{
FILE
* fp =
fopen
(fname,
"r"
);
if
(fp==NULL)
{
printf
(
"no found file\n"
);
return
false
;
}
while
(
feof
(fp)==0)
{
str +=
fgetc
(fp);
}
return
true
;
}
int
main()
{
//先读外部CL核心代码,如果失败则退出。
//代码存buf_code里面
string code_file;
if
(
false
== GetFileData(
"rotate.cl"
,code_file))
{
printf
(
"Open rotate.cl error\n"
);
return
0;
}
char
* buf_code =
new
char
[code_file.size()];
strcpy
(buf_code,code_file.c_str());
buf_code[code_file.size()-1] = NULL;
//声明CL所需变量。
cl_device_id device;
cl_platform_id platform_id = NULL;
cl_context context;
cl_command_queue cmdQueue;
cl_mem bufferA,bufferB;
cl_program program;
cl_kernel kernel = NULL;
//我们使用的是二维向量
//设定向量大小(维数)
size_t
globalWorkSize[2];
globalWorkSize[0] = dim_x ;
globalWorkSize[1] = dim_y;
cl_int err;
/*
定义输入变量和输出变量,并设定初值
*/
size_t
datasize =
sizeof
(
int
) * dim_x * dim_y;
int
m,n;
int
buf_A[] = {0,0,0,1,1,0,0,0,
0,0,1,0,0,1,0,0,
0,1,0,0,0,0,1,0,
1,1,1,0,0,1,1,1,
0,0,1,0,0,1,0,0,
0,0,1,0,0,1,0,0,
0,0,1,0,0,1,0,0,
0,0,1,1,1,1,0,0,};
//输出数组初始为-1,即该位置不显示数字
int
buf_B[] = {-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1,
-1,-1,-1,-1,-1,-1,-1,-1};
//step 1:初始化OpenCL
err = clGetPlatformIDs(1,&platform_id,NULL);
if
(err!=CL_SUCCESS)
{
cout<<
"clGetPlatformIDs error:"
<<err<<endl;
return
0;
}
//这次我们只用CPU来进行并行运算,当然你也可以该成GPU
clGetDeviceIDs(platform_id,CL_DEVICE_TYPE_GPU,1,&device,NULL);
//step 2:创建上下文
context = clCreateContext(NULL,1,&device,NULL,NULL,NULL);
//step 3:创建命令队列
cmdQueue = clCreateCommandQueue(context,device,0,NULL);
//step 4:创建数据缓冲区
bufferA = clCreateBuffer(context,
CL_MEM_READ_ONLY,
datasize,NULL,NULL);
bufferB = clCreateBuffer(context,
CL_MEM_WRITE_ONLY,
datasize,NULL,NULL);
//step 5:将数据上传到缓冲区
clEnqueueWriteBuffer(cmdQueue,
bufferA,CL_FALSE,
0,datasize,
buf_A,0,
NULL,NULL);
//step 6:加载编译代码,创建内核调用函数
program = clCreateProgramWithSource(context,1,
(
const
char
**)&buf_code,
NULL,NULL);
clBuildProgram(program,1,&device,NULL,NULL,NULL);
kernel = clCreateKernel(program,
"rotation"
,NULL);
//step 7:设置参数,执行内核
float
sinangle = sinf(angle);
float
cosangle = cosf(angle);
clSetKernelArg(kernel,0,
sizeof
(cl_mem),&bufferA);
clSetKernelArg(kernel,1,
sizeof
(cl_mem),&bufferB);
clSetKernelArg(kernel,2,
sizeof
(cl_int),&dim_x);
clSetKernelArg(kernel,3,
sizeof
(cl_int),&dim_y);
clSetKernelArg(kernel,4,
sizeof
(cl_float),&sinangle);
clSetKernelArg(kernel,5,
sizeof
(cl_float),&cosangle);
//注意这里第三个参数已经改成2,表示二维数据。
clEnqueueNDRangeKernel(cmdQueue,kernel,
2,NULL,
globalWorkSize,
NULL,0,NULL,NULL);
//step 8:取回计算结果
clEnqueueReadBuffer(cmdQueue,bufferB,CL_TRUE,0,
datasize,buf_B,0,NULL,NULL);
//输出计算结果
for
(n=0;n<dim_x;n++)
{
for
(m=0;m<dim_y;m++)
{
if
(buf_A[m+dim_x*n]==0)
cout<<
" "
;
else
cout<< buf_A[m+dim_x*n] <<
" "
;
}
cout<<endl;
}
cout<<endl<<
"====Rotate 45===="
<<endl<<endl;
for
(n=0;n<dim_x;n++)
{
for
(m=0;m<dim_y;m++)
{
if
(buf_B[m+dim_x*n]<=0)
cout<<
" "
;
else
cout<< buf_B[m+dim_x*n] <<
" "
;
}
cout<<endl;
}
//释放所有调用和内存
clReleaseKernel(kernel);
clReleaseProgram(program);
clReleaseCommandQueue(cmdQueue);
clReleaseMemObject(bufferA);
clReleaseMemObject(bufferB);
clReleaseContext(context);
delete
buf_code;
return
0;
}
程序运行结果:
为什么旋转后图形不规整呢?这是因为图片的大小和运算的精度不够。主要是图片的大小不够,一个8X8的图你还能指望他能好成啥样啊!
0 0
- OpenCL 第7课:旋转变换(1)
- OpenCL 第8课:旋转变换(2)
- Opencl图片旋转编写
- 旋转变换
- 旋转变换
- OpenCL 第5课:向量相加
- OpenCL 第6课:矩阵转置
- OpenGL教程翻译 第七课 旋转变换
- 一步步学OpenGL(7) - 《旋转变换》
- 【笔记】《WebGL编程指南》学习-第4章高级变换与动画基础(1-平移然后旋转))
- sdut 2603 向量的旋转变换与旋转角的关系(山东第4届省赛)
- 旋转变换和平移变换
- 傅立叶变换第1讲
- 三维空间中的旋转变换
- 坐标点旋转变换
- OpenGL: 旋转变换
- 图像旋转变换
- UI旋转变换视图
- setcolor(2); line(x0,y0,x,y);}
- 常用加密算法
- OpenCL 第6课:矩阵转置
- LeetCode: Longest Common Prefix
- Oracle Security Alert for CVE-2014-0160
- OpenCL 第7课:旋转变换(1)
- 布delphi程序(build with runtime package)要带哪些文件?
- Android开发之如何完全退出程序
- OpenCL 第8课:旋转变换(2)
- Bloglink list
- Android 异步加载网络图片并缓存到本地
- [wxWidgets]_[初级]_[监听wxListCtrl和wxGrid的滚动条事件]
- Java5、Java6、Java7的新特性
- 如何获得动态链接库函数被调用时获得自己存储路径