OpenCL 第8课:旋转变换(2)
上兩節(jié)課都是對(duì)一個(gè)數(shù)組進(jìn)行處理。這節(jié)我們來(lái)個(gè)有意思的。同樣是旋轉(zhuǎn)。但我們旋轉(zhuǎn)的對(duì)象是張(256*256)的圖片。圖片旋轉(zhuǎn)45度,旋轉(zhuǎn)后大小還是256*256,超出部份進(jìn)行剪除。
?
圖片旋轉(zhuǎn)處理有個(gè)特別的地方。buf_A是存儲(chǔ)源圖數(shù)據(jù)(R,G,B顏色分量),buf_B是存儲(chǔ)旋轉(zhuǎn)后數(shù)據(jù)。我們不能簡(jiǎn)單將buf_A中的數(shù)據(jù)直接計(jì)算旋轉(zhuǎn)后位置。而是遍歷buf_B每個(gè)數(shù)據(jù),計(jì)算那些數(shù)據(jù)旋轉(zhuǎn)后存在這個(gè)位置的數(shù)據(jù)他在buf_A的坐標(biāo)是多少。為什么呢?不是因?yàn)椴荒苤苯有D(zhuǎn)。而是因?yàn)樽鴺?biāo)是整數(shù)型。進(jìn)行旋轉(zhuǎn)計(jì)算后會(huì)得出浮點(diǎn)數(shù),有小數(shù)。計(jì)算精度不同。如果還用直接旋轉(zhuǎn)計(jì)算方法。成出的位圖將會(huì)出現(xiàn)斑點(diǎn)。大家可試下。
?
我們來(lái)看源碼
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 27 28 | __kernel void rotation(__global int* A, ????????????????????__global int* B, ????????????????????int width, ????????????????????int height, ????????????????????float sinangle, ????????????????????float cosangle) { ????//獲取索引號(hào),這里是二維的,所以可以取兩個(gè) ????//否則另一個(gè)永遠(yuǎn)是0 ????int col = get_global_id(0); ????int row = get_global_id(1); ????//計(jì)算圖形中心點(diǎn) ????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)); ????//邊界檢測(cè) ????if(nx>=0 && nx<width && ny>=0 && ny<height) ????{ ????????B[col*3+0+row*width*3] = A[nx*3+0+ny*width*3]; ????????B[col*3+1+row*width*3] = A[nx*3+1+ny*width*3]; ????????B[col*3+2+row*width*3] = A[nx*3+2+ny*width*3]; ????} } |
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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 | #include <iostream> #include <stdio.h> #include <string.h> #include <string> #include <conio.h> #include <math.h>//數(shù)學(xué)庫(kù) #include <CL/cl.h>//包含CL的頭文件 //調(diào)用freeimage #include <freeimage.h> using namespace std; //8x8數(shù)組 const int dim_x = 256; const int dim_y = 256; //45度的弧度 const float angle = 3.1415926f/4.0f; static int buf_A[dim_x*dim_y*3]; static int buf_B[dim_x*dim_y*3]; //加載圖片 //以RGBA格式存儲(chǔ)圖片 static bool LoadImg(const char* fname) { ????//初始化FreeImage ????FreeImage_Initialise(TRUE); ????//定義圖片格式為未知 ????FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; ????//獲取圖片格式 ????fif = FreeImage_GetFileType(fname,0); ????//根據(jù)獲取格式讀取圖片數(shù)據(jù) ????FIBITMAP* bitmap = FreeImage_Load(fif,fname,0); ????if(!bitmap) ????{ ????????printf("load error!\n"); ????????return false; ????} ????int x,y; ????RGBQUAD m_rgb; ????//獲取圖片長(zhǎng)寬 ????int width = (int)FreeImage_GetWidth(bitmap); ????int height = (int)FreeImage_GetHeight(bitmap); ????//獲取圖片數(shù)據(jù) ????//按RGBA格式保存到數(shù)組中 ????for(y=0;y<height;y++) ????{ ????????for(x=0;x<width;x++) ????????{ ????????????//獲取像素值 ????????????FreeImage_GetPixelColor(bitmap,x,y,&m_rgb); ????????????//將RGB值存入數(shù)組 ????????????buf_A[y*width*3+x*3+2] = m_rgb.rgbRed; ????????????buf_A[y*width*3+x*3+1] = m_rgb.rgbGreen; ????????????buf_A[y*width*3+x*3+0] = m_rgb.rgbBlue; ????????} ????} ????FreeImage_Unload(bitmap); ????return true; } static bool SaveImg() { ????//初始化FreeImage ????FreeImage_Initialise(TRUE); ????FIBITMAP* bitmap =FreeImage_Allocate(dim_x,dim_y,32,8,8,8); ????int m,n; ????for(n=0;n<dim_y;n++) ????{ ????????BYTE *bits =FreeImage_GetScanLine(bitmap,n); ????????for(m=0;m<dim_x;m++) ????????{ ????????????bits[0] = buf_B[dim_x*3*n+m*3+0]; ????????????bits[1] = buf_B[dim_x*3*n+m*3+1]; ????????????bits[2] = buf_B[dim_x*3*n+m*3+2]; ????????????bits[3] = 255; ????????????bits+=4; ????????} ????} ????//保存圖片為PNG格式 ????if(false ==FreeImage_Save(FIF_PNG, bitmap,"rotate.png", PNG_DEFAULT)) ????{ ????????printf("save image error\n"); ????} ????FreeImage_Unload(bitmap); ????return true; } //從外部文件獲取cl內(nèi)核代碼 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() { ????if(LoadImg("bk.png")==false) ????{ ????????printf("error load bk.png!\n"); ????????return 0; ????} ????//先讀外部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; ????//我們使用的是二維向量 ????//設(shè)定向量大小(維數(shù)) ????size_t globalWorkSize[2]; ????globalWorkSize[0] = dim_x; ????globalWorkSize[1] = dim_y; ????cl_int err; ????/* ????????定義輸入變量和輸出變量,并設(shè)定初值 ????*/ ????size_t datasize = sizeof(int) * dim_x * dim_y * 3; ????//step 1:初始化OpenCL ????err = clGetPlatformIDs(1,&platform_id,NULL); ????if(err!=CL_SUCCESS) ????{ ????????cout<<"clGetPlatformIDs error:"<<err<<endl; ????????return 0; ????} ????//這次我們只用CPU來(lái)進(jìn)行并行運(yùn)算,當(dāng)然你也可以該成GPU ????clGetDeviceIDs(platform_id,CL_DEVICE_TYPE_CPU,1,&device,NULL); ????//step 2:創(chuàng)建上下文 ????context = clCreateContext(NULL,1,&device,NULL,NULL,NULL); ????//step 3:創(chuàng)建命令隊(duì)列 ????cmdQueue = clCreateCommandQueue(context,device,0,NULL); ????//step 4:創(chuàng)建數(shù)據(jù)緩沖區(qū) ????bufferA = clCreateBuffer(context, ?????????????????????????????CL_MEM_READ_ONLY, ?????????????????????????????datasize,NULL,NULL); ????bufferB = clCreateBuffer(context, ?????????????????????????????CL_MEM_WRITE_ONLY, ?????????????????????????????datasize,NULL,NULL); ????//step 5:將數(shù)據(jù)上傳到緩沖區(qū) ????clEnqueueWriteBuffer(cmdQueue, ?????????????????????????bufferA,CL_FALSE, ?????????????????????????0,datasize, ?????????????????????????buf_A,0, ?????????????????????????NULL,NULL); ????//step 6:加載編譯代碼,創(chuàng)建內(nèi)核調(diào)用函數(shù) ????program = clCreateProgramWithSource(context,1, ????????????????????????????????????????(const char**)&buf_code, ????????????????????????????????????????NULL,NULL); ????clBuildProgram(program,1,&device,NULL,NULL,NULL); ????kernel = clCreateKernel(program,"rotation",NULL); ????//step 7:設(shè)置參數(shù),執(zhí)行內(nèi)核 ????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); ????//注意這里第三個(gè)參數(shù)已經(jīng)改成2,表示二維數(shù)據(jù)。 ????clEnqueueNDRangeKernel(cmdQueue,kernel, ???????????????????????????2,NULL, ???????????????????????????globalWorkSize, ???????????????????????????NULL,0,NULL,NULL); ????//step 8:取回計(jì)算結(jié)果 ????clEnqueueReadBuffer(cmdQueue,bufferB,CL_TRUE,0, ????????????????????????datasize,buf_B,0,NULL,NULL); ????SaveImg(); ????//釋放所有調(diào)用和內(nèi)存 ????clReleaseKernel(kernel); ????clReleaseProgram(program); ????clReleaseCommandQueue(cmdQueue); ????clReleaseMemObject(bufferA); ????clReleaseMemObject(bufferB); ????clReleaseContext(context); ????delete buf_code; ????return 0; } |
?
源圖
?
旋轉(zhuǎn)后圖
總結(jié)
以上是生活随笔為你收集整理的OpenCL 第8课:旋转变换(2)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 邮政银行需要实名认证是什么意思 什么是邮
- 下一篇: 白银股票龙头股是什么