使用OpenCL+OpenCV实现图像卷积(二)

[题外话]近期申请了一个微信公众号:平凡程式人生。有兴趣的朋友可以关注,那里将会涉及更多更新OpenCL+OpenCV以及图像处理方面的文章。


3、kernel程序代码

Kernel程序是每个workitem需要执行的,它需要存储在以cl为后缀的文件中。该程序中kernel文件为ImageConvolution.cl。

Kernel内程序定义如下:

constsampler_t mysampler = CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;

__kernelvoidconvolution(

       __read_only image2d_t sourceImage,

       __write_only image2d_t outputImage,

       int cols,

       int rows, 

       __constant float*filter,

int filterWidth);

变量sourceImage和outputImage为输入、输出图像的buffer;

变量cols和rows是所需处理图像的宽度和高度;

变量*filter指向存储卷积核的buffer;

变量filterWidth为卷积核的宽度;

这里在私有空间,定义了一个sampler_t变量,用于从输入图像buffer中读取图像数据。如下面的code:

pixel =read_imageui(sourceImage, mysampler, coords);

Kernel程序定义如下:

1.	const sampler_t mysampler = CLK_ADDRESS_CLAMP_TO_EDGE | CLK_FILTER_NEAREST;  
2.	__kernel void convolution(  
3.	    __read_only image2d_t sourceImage,  
4.	    __write_only image2d_t outputImage,  
5.	    int cols,  
6.	    int rows,     
7.	    __constant float *filter,  
8.	    int filterWidth)  
9.	{  
10.	    //Store each work-item's unique row and column  
11.	    int column = get_global_id(0);  
12.	    int row = get_global_id(1);  
13.	  
14.	    //Each work-item iterates around its local area based on the size of the filter  
15.	    int2 coords; //Coordinates for accessing the image  
16.	      
17.	    //Half the width of the filter is needed for indexing memory later  
18.	    int halfWidth = (int)(filterWidth / 2);  
19.	  
20.	    //All accesses to images return data as four-element vector  
21.	    //(i.e., float4), although only the 'x' component will contain meaningful data in this code  
22.	    uint4 sum = {0, 0, 0, 0};     
23.	  
24.	    //Iterator for the filter  
25.	    int filterIdx = 0;    
26.	  
27.	    //Iterate the filter rows  
28.	    for (int i = -halfWidth; i <= halfWidth; i++) {  
29.	        coords.y = row + i;  
30.	  
31.	        //Iterate over the filter columns  
32.	        for (int j = -halfWidth; j <= halfWidth; j++) {  
33.	            coords.x = column + j;  
34.	            uint4 pixel;  
35.	  
36.	            //Read a pixel from the image. A single channel image store the pixel  
37.	            //in the 'x' coordinate of the returned vector  
38.	            pixel = read_imageui(sourceImage, mysampler, coords);  
39.	            sum.x += pixel.x * filter[filterIdx++];           
40.	        }  
41.	    }  
42.	  
43.	    //Copy t
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值