Differences between revisions 9 and 10
Revision 9 as of 2014-06-26 13:18:59
Size: 1898
Editor: ::ffff:49
Comment:
Revision 10 as of 2014-06-26 13:30:50
Size: 1413
Editor: ::ffff:49
Comment:
Deletions are marked like this. Additions are marked like this.
Line 26: Line 26:
##########################################################################
# This program takes a 256x256 RGB image and converts it to grayscale
#
#
##########################################################################
Line 38: Line 32:
__global__ void rgb2gray(int *dest,int *r_img, int *g_img, int *b_img) __global__ void rgb2gray(float *dest,float *r_img, float *g_img, float *b_img)
Line 46: Line 40:
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)];
__syncthreads();
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)]);
Line 54: Line 48:
a = scm.imread('Lenna.png').astype(np.uint8) a = scm.imread('Lenna.png').astype(np.float32)
pr
int a
Line 58: Line 53:

##############################################################################
#TypeError: expected a single-segment buffer object
#the abova can be avoided using fortran arrays
##############################################################################
Line 64: Line 54:
print dest
Line 69: Line 60:
p.gray()

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)