首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >opencl:C++实现双线性插值图像缩放

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

作者头像
10km
发布2019-05-25 22:04:45
2.1K0
发布2019-05-25 22:04:45
举报
文章被收录于专栏:10km的专栏10km的专栏

版权声明:本文为博主原创文章,转载请注明源地址。 https://cloud.tencent.com/developer/article/1433586

用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);
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016年02月27日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档