rgb2gray using cuda Simple rgb2gray implementation on 256x256 image

ashwinashok9111993@gmail.com in.linkedin.com/pub/ashwin-ashok/54/183/b5b/

License of this example:

Public Domain

Date:

2014-06-26

PyCUDA version:

2013.1.1

   1 __author__ = 'ashwin'
   2 
   3 import pycuda.driver as drv
   4 import pycuda.tools
   5 import pycuda.autoinit
   6 from pycuda.compiler import SourceModule
   7 import numpy as np
   8 import scipy.misc as scm
   9 import matplotlib.pyplot as p
  10 
  11 mod = SourceModule \
  12     (
  13         """
  14 #include<stdio.h>
  15 #define INDEX(a, b) a*256+b
  16 
  17 __global__ void rgb2gray(float *dest,float *r_img, float *g_img, float *b_img)
  18 {
  19 
  20 unsigned int idx = threadIdx.x+(blockIdx.x*(blockDim.x*blockDim.y));
  21 
  22   unsigned int a = idx/256;
  23   unsigned int b = idx%256;
  24 
  25 dest[INDEX(a, b)] = (0.299*r_img[INDEX(a, b)]+0.587*g_img[INDEX(a, b)]+0.114*b_img[INDEX(a, b)]);
  26 
  27 
  28 }
  29 
  30 """
  31     )
  32 
  33 a = scm.imread('Lenna.png').astype(np.float32)
  34 print a
  35 r_img = a[:, :, 0].reshape(65536, order='F')
  36 g_img = a[:, :, 1].reshape(65536, order='F')
  37 b_img = a[:, :, 2].reshape(65536, order='F')
  38 dest=r_img
  39 print dest
  40 rgb2gray = mod.get_function("rgb2gray")
  41 rgb2gray(drv.Out(dest), drv.In(r_img), drv.In(g_img),drv.In(b_img),block=(1024, 1, 1), grid=(64, 1, 1))
  42 
  43 dest=np.reshape(dest,(256,256), order='F')
  44 
  45 
  46 p.imshow(dest)
  47 p.show()

PyCuda/Examples/SimpleRGB2Gray (last edited 2014-06-26 13:30:50 by ::ffff:49)