CUDA学习--图像取反

grid是1D的,block是2D的

#include<opencv2\opencv.hpp>
#include<cuda_runtime.h>
#include<stdio.h>
#include "device_launch_parameters.h"  

using namespace cv;

__global__ void revImg(uchar3* dev_c,int width,int height)
{
	int tid = blockIdx.x*blockDim.x*blockDim.y + threadIdx.y*blockDim.x + threadIdx.x;
	if (tid < width*height) {
		dev_c[tid].x = 255 - dev_c[tid].x;
		dev_c[tid].y = 255 - dev_c[tid].y;
		dev_c[tid].z = 255 - dev_c[tid].z;
	}     
}

int main()
{
	Mat image = imread("E:/code/study_cuda/study_reduce/study_reduce/cinque_terre_small.jpg");
	imshow("src", image);

	int width, height;
	width = image.size().width;
	height = image.size().height;
	printf("width=%d height=%d", width, height);
	int size = width * height;

	uchar3 *dev_c ;

	cudaMalloc((void**)&dev_c, size * sizeof(uchar3));

	cudaMemcpy(dev_c, image.data, size * sizeof(uchar3), cudaMemcpyHostToDevice);


	int thread = 16;
	int grid = (size + thread - 1) / (thread*thread);
	dim3 dimGrid(grid);
	dim3 dimBlock(thread,thread);
	revImg << <dimGrid, dimBlock >> > (dev_c,width,height);

	cudaMemcpy(image.data, dev_c, size * sizeof(uchar3), cudaMemcpyDeviceToHost);

	imshow("gpu", image);
	waitKey(0);

	cudaFree(dev_c);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值