-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
179 lines (140 loc) · 6 KB
/
main.cpp
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
#include <stdio.h>
#ifdef __APPLE__
#include <OpenCL/cl.h>
#elif defined(__linux__)
#include <CL/cl.h>
#include <CL/opencl.h>
#endif
#include "utils.h"
#include "interpolation_cpu.h"
#include "interpolation_gpu.h"
#include <stdlib.h>
#include <time.h>
#include "ocl_macros.h"
int main(int argc, char *argv[])
{
Image *img = new Image();
// 以二进制方式读取文件
FILE *file;
if (!(file = fopen("image.dat", "rb")))
{
perror("fopen()");
exit(1);
}
// 获取图片信息
int res = fscanf(file, "%d %d %f %f %f\n",
&(img->width), &(img->height),
&(img->depth), &(img->angle),
&(img->radius));
if (5 != res) {
printf("获取图片信息出错!");
return 0;
}
// 读取图片并转换成opencv图片对象
int cnt = img->width*img->height;
img->data = (float*)malloc(cnt*sizeof(float));
fread(img->data, sizeof(float), cnt, file);
IplImage* im = arr2img(img->data, img->height, img->width);
cv::imwrite("原图片.bmp", cv::cvarrToMat(im));
Mat mat;
Mat cubic_mat;
IplImage* imsc;
double start_time=0.0, end_time=0.0;
double cpu_total_time = 0.0;
double gpu_time = 0.0;
double cpu_time = 0.0;
int counts = 5;
cl_int status = 0;
// size_t groupSizeX = 1, groupSizeY = 1;
// size_t groupSizeX = 2, groupSizeY = 2;
// size_t groupSizeX = 4, groupSizeY = 4;
size_t groupSizeX = 8, groupSizeY = 8;
// size_t groupSizeX = 16, groupSizeY = 16;
// size_t groupSizeX = 32, groupSizeY = 32;
size_t ResImageH, ResImageW;
cl_context context;
cl_command_queue commandQueue;
cl_program program;
cl_mem memObjects[16];//内存对象
cl_kernel kernel;
init(groupSizeX, groupSizeY, ResImageH, ResImageW, context, commandQueue, program, "kernel.cl", img, memObjects);
double ssim = 0.0, mse = 0.0, psnr = 0.0;
printf("插值结果如下:\n");
printf("算法\t平台\t平均时间(ms)\tSSIM\tPSNR\tMSE\n");
//双三次插值
for (int i=0; i<counts; i++)
{
start_time = clock();
imsc = Bi_cubic(img);
end_time = clock();
cpu_total_time += double(end_time-start_time)/CLOCKS_PER_SEC;
cubic_mat = cvarrToMat(imsc);
ssim += getSSIM(cubic_mat, cubic_mat);
mse += getMSE(cubic_mat, cubic_mat);
psnr += getPSNR(cubic_mat, cubic_mat);
}
cv::imwrite("双三次插值_cpu.bmp", cubic_mat);
cpu_time = cpu_total_time/counts*1000.0;
printf("双三次插值\t%s\t%.2f\t\t%.2f\t%.2f\t%.2f\t\n", "CPU", cpu_time, ssim/counts, psnr/counts, mse/counts);
kernel = createKernel(program, "Bi_cubic", memObjects);
gpu_time = run_kernel("双三次插值", cubic_mat, commandQueue, kernel, memObjects[15], ResImageW, ResImageH, groupSizeX, groupSizeY, counts);
printf("加速比: %4.3f\n", cpu_time/gpu_time);
ssim = 0.0, mse = 0.0, psnr = 0.0, cpu_total_time = 0.0;
//邻近插值
for (int i=0; i<counts; i++)
{
start_time = clock();
imsc = ScanConvCurve_B(img);
end_time = clock();
cpu_total_time += double(end_time-start_time)/CLOCKS_PER_SEC;
mat = cvarrToMat(imsc);
ssim += getSSIM(mat, cubic_mat);
mse += getMSE(mat, cubic_mat);
psnr += getPSNR(mat, cubic_mat);
}
cv::imwrite("邻近插值_cpu.bmp", mat);
cpu_time = cpu_total_time/counts*1000.0;
printf("邻近插值\t%s\t%.2f\t\t%.2f\t%.2f\t%.2f\t\n", "CPU", cpu_time, ssim/counts, psnr/counts, mse/counts);
kernel = createKernel(program, "ScanConvCurve_B", memObjects);
gpu_time = run_kernel("邻近插值", cubic_mat, commandQueue, kernel, memObjects[15], ResImageW, ResImageH, groupSizeX, groupSizeY, counts);
printf("加速比: %4.3f\n", cpu_time/gpu_time);
ssim = 0.0, mse = 0.0, psnr = 0.0, cpu_total_time = 0.0;
//双线性插值
for (int i=0; i<counts; i++)
{
start_time = clock();
imsc = Inter_Linear(img);
end_time = clock();
cpu_total_time += double(end_time-start_time)/CLOCKS_PER_SEC;
mat = cvarrToMat(imsc);
ssim += getSSIM(mat, cubic_mat);
mse += getMSE(mat, cubic_mat);
psnr += getPSNR(mat, cubic_mat);
}
cv::imwrite("双线性插值_cpu.bmp", mat);
cpu_time = cpu_total_time/counts*1000.0;
printf("双线性插值\t%s\t%.2f\t\t%.2f\t%.2f\t%.2f\t\n", "CPU", cpu_time, ssim/counts, psnr/counts, mse/counts);
kernel = createKernel(program, "Inter_Linear", memObjects);
gpu_time = run_kernel("双线性插值", cubic_mat, commandQueue, kernel, memObjects[15], ResImageW, ResImageH, groupSizeX, groupSizeY, counts);
printf("加速比: %4.3f\n", cpu_time/gpu_time);
// 使用3种不同的插值策略
kernel = createKernel(program, "Nearst_cubic", memObjects);
gpu_time = run_kernel("邻近(上)+双三次(下)", cubic_mat, commandQueue, kernel, memObjects[15], ResImageW, ResImageH, groupSizeX, groupSizeY, counts);
kernel = createKernel(program, "Nearst_linear", memObjects);
gpu_time = run_kernel("邻近(上)+双线性(下)", cubic_mat, commandQueue, kernel, memObjects[15], ResImageW, ResImageH, groupSizeX, groupSizeY, counts);
kernel = createKernel(program, "Linear_cubic", memObjects);
gpu_time = run_kernel("双线性线性(上)+双三次(下)", cubic_mat, commandQueue, kernel, memObjects[15], ResImageW, ResImageH, groupSizeX, groupSizeY, counts);
kernel = createKernel(program, "Nearst_linear_cubic", memObjects);
gpu_time = run_kernel("邻近(上)+双线性(中)+双三次(下)", cubic_mat, commandQueue, kernel, memObjects[15], ResImageW, ResImageH, groupSizeX, groupSizeY, counts);
cvReleaseImage(&im);
cvReleaseImage(&imsc);
status = clReleaseKernel(kernel);
status |= clReleaseProgram(program);
for (int i = 0; i < 16; i++) {
clReleaseMemObject(memObjects[i]);
}
status |= clReleaseCommandQueue(commandQueue);
status |= clReleaseContext(context);
LOG_OCL_ERROR(status, "clean Failed.");
return 0;
}