-
Notifications
You must be signed in to change notification settings - Fork 0
/
ds2float.py
62 lines (38 loc) · 1.42 KB
/
ds2float.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import pycuda.driver as cuda
import pycuda.gpuarray as gpuarray #For GPU array computations
import pycuda.compiler as nvcc #Compiles our CUDA kernels
import pycuda.autoinit #Start Those GPUs!
import numpy as np
import cv2
import sys
import time
downsample=nvcc.SourceModule("""
texture<float, 2, cudaReadModeElementType> tex;
__global__ void downsample(float * data, int width, int new_width) {
unsigned int x = blockIdx.x * blockDim.x + threadIdx.x;
unsigned int y = blockIdx.y * blockDim.y + threadIdx.y;
float value = tex2D(tex, y, x)*255.;
data[(x/2 * new_width + y/2)] = value;
}
""")
downsample_func = downsample.get_function("downsample")
texref = downsample.get_texref("tex")
blocksize = (16,16,1)
def doit(img):
width, height = (img.shape[0], img.shape[1])
cuda.matrix_to_texref(img, texref, order="C")
texref.set_filter_mode(cuda.filter_mode.LINEAR)
gpu_output = np.zeros((width/2,height/2), dtype=np.float32)
gridsize = (width / blocksize[0], height / blocksize[1])
downsample_func(cuda.Out(gpu_output), np.int32(width), np.int32(width/2), block=blocksize, grid = gridsize, texrefs=[texref])
# gpu_output = gpu_output.transpose()
return gpu_output
img = cv2.imread(sys.argv[1], cv2.CV_LOAD_IMAGE_GRAYSCALE)
k=0
while (img.shape[0] > 256):
k+=1
start_t = time.clock()
img = doit(img)
print time.clock() - start_t
img = img.astype(np.uint8)
cv2.imwrite('/tmp/py_z'+str(k)+'.jpg', img)