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
| THREADS_PER_BLOCK = 512
def gpu_enhanceImage(pic):
# convert to array
pix = numpy.array(pic)
width, height = pic.size
total = width*height
# ------------------------------------------------
# accelerate the following part as much as possible using GPU
#
# compute histograms for RGB components separately
# the value range for each pixel is [0,255]
hist_rgb = [0]*256
hist_rgb_buffer = numpy.float32(hist_rgb)
pix = pix.astype(numpy.uint8)
hist_rgb_gpu = cuda.mem_alloc(hist_rgb_buffer.nbytes)
pix_gpu = cuda.mem_alloc(pix.nbytes)
cuda.memcpy_htod(hist_rgb_gpu, hist_rgb_buffer)
cuda.memcpy_htod(pix_gpu, pix)
histograms(hist_rgb_gpu, pix_gpu,
numpy.int32(total),
block=(THREADS_PER_BLOCK, 1, 1),
grid=((total + THREADS_PER_BLOCK - 1)/THREADS_PER_BLOCK,1))
cuda.memcpy_dtoh(hist_rgb_buffer, hist_rgb_gpu)
hist_rgb = hist_rgb_buffer
# compute the accumulative histograms
for intensity in range(1,256):
temp = hist_rgb[intensity] + hist_rgb[intensity-1]
# take care of rounding error
temp = min(temp, 256 - 1)
hist_rgb[intensity] = temp
# enhance the picture according to the inversed histgram
cuda.memcpy_htod(hist_rgb_gpu, hist_rgb)
enhance(pix_gpu, hist_rgb_gpu,
numpy.int32(total),
block=(THREADS_PER_BLOCK, 1, 1),
grid=((total + THREADS_PER_BLOCK - 1)/THREADS_PER_BLOCK, 1))
cuda.memcpy_dtoh(pix, pix_gpu)
# -----------------------------------------------
# save the picture
pic = Image.fromarray(pix)
return pic
|