OpenCV GPU 简单遍历图像

OpenCV GPU 图像遍历

OpenCV GPU  简单遍历图像  


[cpp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #include "cuda_runtime.h"  
  2. #include "device_launch_parameters.h"  
  3. #include <cuda.h>  
  4. #include <cuda_device_runtime_api.h>  
  5. #include <opencv2\gpu\gpu.hpp>  
  6. #include <opencv2\gpu\gpumat.hpp>  
  7. #include <opencv2\opencv.hpp>  
  8. #include <opencv.hpp>  
  9. #include <stdio.h>  
  10. #include <iostream>  
  11. #include "opencv2/gpu/device/common.hpp"  
  12. #include "opencv2/gpu/device/reduce.hpp"  
  13. #include "opencv2/gpu/device/functional.hpp"  
  14. #include "opencv2/gpu/device/warp_shuffle.hpp"  
  15. using namespace std;  
  16. using namespace cv;  
  17. using namespace gpu;  
  18. template <int nthreads>  
  19.         __global__ void compute_kernel(int height, int width, const PtrStepb img ,PtrStepb dst)  
  20.         {  
  21.             const int x = blockIdx.x * blockDim.x + threadIdx.x;  
  22.             const int y = blockIdx.y * blockDim.y + threadIdx.y;  
  23.   
  24.             const uchar* src_y = (const uchar*)(img+y*img.step);//对于彩色图像  
  25.             uchar* dst_y = (uchar*)(dst+y*dst.step);  
  26.             if (x < width && y < height)  
  27.             {  
  28.                   
  29.                 dst_y[3*x] = src_y[3*x] ;  
  30.                 dst_y[3*x+1]  = src_y[3*x+1] ;  
  31.                 dst_y[3*x+2]  = src_y[3*x+2] ;         
  32.              }  
  33.   
  34. ////////////////////////////////////灰度图像//////////////////////////////////////  
  35.             //if (x < width)  
  36.    //         {  
  37.             // if (blockIdx.y > 0 && blockIdx.y < height )  
  38.    //          {  
  39.             //   ((float*)dst.ptr(blockIdx.y))[x] = ((float*)img.ptr(blockIdx.y))[x];  
  40.             //   //((uchar2*)dst.ptr(blockIdx.y))[x] = make_uchar2(blockIdx.y,blockIdx.x);  
  41.             // }  
  42.             //}  
  43.         }     
  44.     int main()  
  45. {  
  46. Mat a= imread("d:/1.jpg");  
  47.     GpuMat d_a(a);  
  48.     GpuMat d_dst(d_a.size(),CV_8UC3);  
  49.     int width = a.size().width;  
  50.     int height = a.size().height;  
  51.     const int nthreads =256;  
  52.     dim3 bdim(nthreads, 1);  
  53.     dim3 gdim(divUp(width, bdim.x), divUp(height, bdim.y));  
  54.     compute_kernel<nthreads><<<gdim, bdim>>>(height,width,d_a,d_dst);  
  55.     Mat dst(d_dst);  
  56.     imshow("a",a);  
  57.     imshow("dst",dst);  
  58.     waitKey();  
  59.     return 0;  
  60. }  


显示结果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值