-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_accel.py
executable file
·92 lines (62 loc) · 1.78 KB
/
image_accel.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/python
import cv2
import numpy
import time
from xillybus_accel import xb_accel
def invert(img):
c = numpy.empty(img.shape,numpy.uint8)
c.fill(255)
img_out = numpy.subtract(c,img)
return img_out
def rgb_to_flat(rgb):
shape = numpy.shape(rgb)
flat = numpy.zeros(shape[0:2], dtype=numpy.uint32)
for i in range(shape[0]):
for j in range(shape[1]):
#flat[i,j] = img[i,j,0] + (img[i,j,1]<<8) + (img[i,j,2]<<16)
flat[i,j] = sum(rgb[i,j,:] * [1,256,65536])
return flat
def flat_to_rgb(flat):
shape = numpy.shape(flat)
rgb = numpy.zeros((shape[0], shape[1], 3), numpy.uint8)
for i in range(shape[0]):
for j in range(shape[1]):
rgb[i,j,:] = numpy.ndarray((3), numpy.uint8, flat[i,j])
return rgb
def display_loop():
while True:
try:
key = cv2.waitKey(50)
if key == 27: # exit on ESC
break
except KeyboardInterrupt:
break
if __name__ == "__main__":
print 'Reading Image'
#img = cv2.imread('campanile.jpg',cv2.CV_LOAD_IMAGE_GRAYSCALE)
img = cv2.imread('/home/zeddev/Pictures/campanile.jpg')
print 'Creating windows'
cv2.namedWindow('input')
cv2.namedWindow('software')
cv2.namedWindow('hardware')
print 'Software start'
soft_start = time.time()
soft_img = invert(img)
soft_time = time.time() - soft_start
print 'Software time: %f' % soft_time
print 'Hardware start'
hard_start = time.time()
print '>Input convert'
flat = rgb_to_flat(img)
print '>Accel'
hard_flat_img = xb_accel(flat)
print '>Output convert'
hard_img = flat_to_rgb(hard_flat_img)
hard_time = time.time() - hard_start
print 'Hardware time: %f' % hard_time
print 'Showing images'
cv2.imshow('input', img)
cv2.imshow('software', soft_img)
cv2.imshow('hardware', hard_img)
print 'Waiting for Esc'
display_loop()