OpenCV GPU 简单遍历图像
- #include "cuda_runtime.h"
- #include "device_launch_parameters.h"
- #include <cuda.h>
- #include <cuda_device_runtime_api.h>
- #include <opencv2\gpu\gpu.hpp>
- #include <opencv2\gpu\gpumat.hpp>
- #include <opencv2\opencv.hpp>
- #include <opencv.hpp>
- #include <stdio.h>
- #include <iostream>
- #include "opencv2/gpu/device/common.hpp"
- #include "opencv2/gpu/device/reduce.hpp"
- #include "opencv2/gpu/device/functional.hpp"
- #include "opencv2/gpu/device/warp_shuffle.hpp"
- using namespace std;
- using namespace cv;
- using namespace gpu;
- template <int nthreads>
- __global__ void compute_kernel(int height, int width, const PtrStepb img ,PtrStepb dst)
- {
- const int x = blockIdx.x * blockDim.x + threadIdx.x;
- const int y = blockIdx.y * blockDim.y + threadIdx.y;
- const uchar* src_y = (const uchar*)(img+y*img.step);//对于彩色图像
- uchar* dst_y = (uchar*)(dst+y*dst.step);
- if (x < width && y < height)
- {
- dst_y[3*x] = src_y[3*x] ;
- dst_y[3*x+1] = src_y[3*x+1] ;
- dst_y[3*x+2] = src_y[3*x+2] ;
- }
- ////////////////////////////////////灰度图像//////////////////////////////////////
- //if (x < width)
- // {
- // if (blockIdx.y > 0 && blockIdx.y < height )
- // {
- // ((float*)dst.ptr(blockIdx.y))[x] = ((float*)img.ptr(blockIdx.y))[x];
- // //((uchar2*)dst.ptr(blockIdx.y))[x] = make_uchar2(blockIdx.y,blockIdx.x);
- // }
- //}
- }
- int main()
- {
- Mat a= imread("d:/1.jpg");
- GpuMat d_a(a);
- GpuMat d_dst(d_a.size(),CV_8UC3);
- int width = a.size().width;
- int height = a.size().height;
- const int nthreads =256;
- dim3 bdim(nthreads, 1);
- dim3 gdim(divUp(width, bdim.x), divUp(height, bdim.y));
- compute_kernel<nthreads><<<gdim, bdim>>>(height,width,d_a,d_dst);
- Mat dst(d_dst);
- imshow("a",a);
- imshow("dst",dst);
- waitKey();
- return 0;
- }
显示结果