CUDA和OpenCV混合编程,使用CUDA的纹理内存,实现图像的二值化以及滤波功能。

#include <cuda_runtime.h>
#include <highgui/highgui.hpp>
#include <imgproc/imgproc.hpp> using namespace cv; int width = 512;
int height = 512; // 2维纹理
texture<float, 2, cudaReadModeElementType> texRef; // 核函数
__global__ void transformKernel(uchar* output, int width, int height)
{
// 根据tid bid计算归一化的拾取坐标
unsigned int x = blockIdx.x * blockDim.x + threadIdx.x;
unsigned int y = blockIdx.y * blockDim.y + threadIdx.y;
float u = x / (float)width;
float v = y / (float)height; //从纹理存储器中拾取数据,并写入显存
output[(y * width + x)] = tex2D(texRef, u / 4, v / 4);
} int main()
{
// 分配CUDA数组
cudaChannelFormatDesc channelDesc = cudaCreateChannelDesc(32, 0, 0, 0, cudaChannelFormatKindFloat);
cudaArray* cuArray;
cudaMallocArray(&cuArray, &channelDesc, width, height); //使用OpenCV读入图像
Mat image = imread("D:\\lena.jpg", 0);
resize(image, image, Size(width, height));
imshow("原始图像", image); cudaMemcpyToArray(cuArray, 0, 0, image.data, width*height, cudaMemcpyHostToDevice); // 设置纹理属性
texRef.addressMode[0] = cudaAddressModeWrap; //循环寻址方式
texRef.addressMode[1] = cudaAddressModeWrap;
texRef.filterMode = cudaFilterModeLinear; //线性滤波
texRef.normalized = true; //归一化坐标 //绑定纹理
cudaBindTextureToArray(texRef, cuArray, channelDesc); Mat imageOutput = Mat(Size(width, height), CV_8UC1);
uchar * output = imageOutput.data; cudaMalloc((void**)&output, width * height * sizeof(float)); dim3 dimBlock(16, 16);
dim3 dimGrid((width + dimBlock.x - 1) / dimBlock.x, (height + dimBlock.y - 1) / dimBlock.y); transformKernel << <dimGrid, dimBlock >> > (output, width, height); cudaMemcpy(imageOutput.data, output, height*width, cudaMemcpyDeviceToHost); imshow("CUDA+OpenCV滤波", imageOutput);
waitKey(); cudaFreeArray(cuArray);
cudaFree(output);
}

原始lena图像:

运行效果:

CUDA二维纹理内存+OpenCV图像滤波的更多相关文章

  1. CUDA中多维数组以及多维纹理内存的使用

    纹理存储器(texture memory)是一种只读存储器,由GPU用于纹理渲染的图形专用单元发展而来,因此也提供了一些特殊功能.纹理存储器中的数据位于显存,但可以通过纹理缓存加速读取.在纹理存储器中 ...

  2. Unity 用户手册用户指南二维纹理 (Texture 2D)

    http://www.58player.com/blog-2327-953.html 二维纹理 (Texture 2D) 纹理 (Textures) 使您的 网格 (Meshes).粒子 (Parti ...

  3. WebGl 二维纹理贴图(矩形)

    效果: 代码: <!DOCTYPE html> <html lang="en"> <head> <meta charset="U ...

  4. 微信二维码引擎OpenCV开源研究

    <微信二维码引擎OpenCV开源研究> 一.编译和Test测试        opencv_wechat_qrcode的编译需要同时下载opencv(https://github.com/ ...

  5. 06-01 Java 二维数组格式、二维数组内存图解、二维数组操作

    二维数组格式1 /* 二维数组:就是元素为一维数组的一个数组. 格式1: 数据类型[][] 数组名 = new 数据类型[m][n]; m:表示这个二维数组有多少个一维数组. n:表示每一个一维数组的 ...

  6. 实验二、OpenCV图像滤波

    一.题目描述 对下面的图片进行滤波和边缘提取操作,请详细地记录每一步操作的步骤. 滤波操作可以用来过滤噪声,常见噪声有椒盐噪声和高斯噪声,椒盐噪声可以理解为斑点,随机出现在图像中的黑点或白点:高斯噪声 ...

  7. 二维纹理 Texture 2D

    Textures bring your Meshes, Particles, and interfaces to life! They are image or movie files that yo ...

  8. 嵌入式开发之davinci--- 8148/8168/8127 中的二维图像处理内存tiler 铺瓷砖

    http://blog.csdn.net/shanghaiqianlun/article/details/7619603

  9. CUDA一维纹理内存

    纹理一词来源于GPU图形世界,GPU通用并行计算"盗用"了纹理一词,定义了一个纹理内存的概念.纹理内存缓存在 设备上,在某些情况下能减少对内存的请求并降低内存带宽的使用,是专门为那 ...

随机推荐

  1. [Nuxt] Load Data from APIs with Nuxt and Vuex

    In a server-rendered application, if you attempt to load data before the page renders and the data f ...

  2. CSDN日报20170406 ——《代码非常烂,所以离职。》

    [程序人生]代码非常烂.所以离职? 作者:stormzhang 我在面试的时候一般会问这么一个问题:你为什么离职? 当中有不少同学会提到这么一个原因.现在的项目代码太烂了,前人留下了非常多坑,我实在忍 ...

  3. php xml转数组,数组转xml,array转xml,xml转array

    //数组转XML function arrayToXml($arr) { $xml = "<xml>"; foreach ($arr as $key=>$val) ...

  4. Visual Studio 项目目录下的bin目录和 obj目录

    一.Bin目录 Visual Studio 编译时,在bin 目录下有debug 和 release 目录. 1.Debug: 通常称为调试版本,它包含调试信息,所以要比Release 版本大很多(可 ...

  5. 手把手生成决策树(dicision tree)

    手把手生成决策树(dicision tree) 标签: Python 机器学习 主要參考资料: Peter HARRINGTON.机器学习实战[M].李锐,李鹏,曲亚东,王斌译.北京:人民邮电出版社, ...

  6. Lucene学习总结之八:Lucene的查询语法,JavaCC及QueryParser 2014-06-25 14:25 722人阅读 评论(1) 收藏

    一.Lucene的查询语法 Lucene所支持的查询语法可见http://lucene.apache.org/java/3_0_1/queryparsersyntax.html (1) 语法关键字 + ...

  7. ServerSocketChannel API用法

    java.nio.channels 类 ServerSocketChannel java.lang.Object java.nio.channels.spi.AbstractInterruptible ...

  8. JQuery中Ajax详细参数使用案例

    JQuery中Ajax详细参数使用案例 参考文档:http://www.jb51.net/shouce/jquery1.82/ 参考文档:http://jquery.cuishifeng.cn/jQu ...

  9. 枚举系统磁盘驱动器(使用GetLogicalDriveStrings API函数,system("pause"); 很实用,还用到wcslen等函数)

    代码如下: #include "stdafx.h" #include <vector> #include <string> #include <Win ...

  10. [tmux] Manage terminal workspaces using session naming

    It's a lot easier to manage your tmux session when they have sensible names. We'll cover: How to cre ...