国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

CUDA二維紋理內(nèi)存+OpenCV圖像濾波

2019-11-10 16:47:47
字體:
供稿:網(wǎng)友

CUDA和OpenCV混合編程,使用CUDA的紋理內(nèi)存,實(shí)現(xiàn)圖像的二值化以及濾波功能。

#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;// 核函數(shù)__global__ void transformKernel(uchar* output, int width, int height){	// 根據(jù)tid bid計(jì)算歸一化的拾取坐標(biāo)	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;	//從紋理存儲(chǔ)器中拾取數(shù)據(jù),并寫入顯存	output[(y * width + x)] = tex2D(texRef, u / 4, v / 4);}int main(){	// 分配CUDA數(shù)組	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);	// 設(shè)置紋理屬性	texRef.addressMode[0] = cudaAddressModeWrap; //循環(huán)尋址方式	texRef.addressMode[1] = cudaAddressModeWrap;	texRef.filterMode = cudaFilterModeLinear;   //線性濾波	texRef.normalized = true; //歸一化坐標(biāo)							 	//綁定紋理	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圖像:

運(yùn)行效果:


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 福安市| 永年县| 北票市| 乐陵市| 改则县| 东辽县| 青海省| 玉林市| 嵊泗县| 台南市| 扬州市| 大姚县| 西宁市| 堆龙德庆县| 广元市| 库车县| 新密市| 那坡县| 五莲县| 大石桥市| 工布江达县| 定西市| 汉沽区| 什邡市| 墨江| 颍上县| 奇台县| 磴口县| 通许县| 铜鼓县| 永德县| 赞皇县| 黎城县| 襄垣县| 炉霍县| 乌恰县| 安塞县| 西贡区| 鄂尔多斯市| 若尔盖县| 闽侯县|