opencl:C++实现双线性插值图像缩放

用OpenCL实现图像缩放代码是比较简单的,因为OpenCL本身就支持双线性插值
下面是kernel代码(从Mali OpenCL SDK 抄来的:/samples/image_scaling/assets/image_scaling.cl)
非常简单只有4行

// 定义采样器 
// CLK_NORMALIZED_COORDS_TRUE指定使用归一化坐标
// CLK_ADDRESS_CLAMP 指定超出图像范围的颜色为黑色
// CLK_FILTER_LINEAR指定使用双线性插值
__constant sampler_t sampler = CLK_NORMALIZED_COORDS_TRUE | CLK_ADDRESS_CLAMP | CLK_FILTER_LINEAR;
__kernel void image_scaling(__read_only image2d_t sourceImage,
                            __write_only image2d_t destinationImage,
                            const float widthNormalizationFactor,
                            const float heightNormalizationFactor)
{
//从glob_id中获取目标像素坐标
    int2 coordinate = (int2)(get_global_id(0), get_global_id(1));
//计算归一化浮点坐标    
    float2 normalizedCoordinate = convert_float2(coordinate) * (float2)(widthNormalizationFactor, heightNormalizationFactor);
//根据归一化坐标从原图中读取像素数据
    float4 colour = read_imagef(sourceImage, sampler, normalizedCoordinate);
//将像素数据写入目标图像    
    write_imagef(destinationImage, coordinate, colour);
}

下面是主机代码片段

/* 向OpenCL设备中上传原始图像数据 */
void gray_matrix_cl::upload(const cl::CommandQueue& command_queue) const {
	cl::size_t<3> region;
	region[0] = width, region[1] = height, region[2] = 1;
	command_queue.enqueueWriteImage(cl_img,
	CL_TRUE, ZeroOffset, region, get_row_stride(), 0, (void*) pixels.data());
}
/* 从OpenCL设备中下载结果数据 */
void gray_matrix_cl::download(const cl::CommandQueue& command_queue) {
	cl::size_t<3> region;
	region[0] = width, region[1] = height, region[2] = 1;
	// 分配目标图像空间
	if (pixels.size() != get_row_stride() * height)
		pixels = std::vector<uint8_t>(get_row_stride() * height);
	command_queue.enqueueReadImage(cl_img,
	CL_TRUE, ZeroOffset, region, get_row_stride(), 0, (void*) pixels.data());
}
/* 缩放图像(双线性插值) 返回缩放后的图像数据*/
// facecl_context参见上一篇博客(http://blog.csdn.net/10km/article/details/50755251)
gray_matrix_cl gray_matrix_cl::zoom(size_t dst_width, size_t dst_height, const facecl_context& context)const {
	gray_matrix_cl dst_matrix(dst_width, dst_height);
	auto kernel = context.getKernel(KERNEL_NAME(image_scaling));// 获取已经编译好的cl::Kernel
	// 参见上一篇博客(http://blog.csdn.net/10km/article/details/50755251)
	auto command_queue = global_facecl_context.getCommandQueue();// 获取cl::CommandQueue
	this->upload(command_queue);//向OpenCL设备中上传原始图像数据
	cl_float widthNormalizationFactor = 1.0f / dst_width;
	cl_float heightNormalizationFactor = 1.0f / dst_height;
	// 设置kernel参数
	kernel.setArg(0, cl_img);
	kernel.setArg(1, dst_matrix.cl_img);
	kernel.setArg(2, widthNormalizationFactor);
	kernel.setArg(3, heightNormalizationFactor);
	const cl::NDRange global(dst_width, dst_height);
	// 执行 kernel
	command_queue.enqueueNDRangeKernel(kernel, gray_matrix_cl::NullRange, global);
	command_queue.finish();// 等待kernel执行结束
	dst_matrix.download(command_queue);// 从OpenCL设备中下载结果数据
	return std::move(dst_matrix);
}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

10km

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值