-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.py
190 lines (157 loc) · 6.52 KB
/
main.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
'''
CLPatchMatch by AbiusX
This code runs the patch match algorithm on two images, using OpenCL and runs in realtime.
Performance:
50 iterations of this take less than a second on the development machine, where as the sequential
code requires 5+ seconds for each iteration.
'''
import pyopencl as cl
import numpy
import pylab
import matplotlib;
import skimage
import skimage.io
import skimage.transform
import datetime
from operator import itemgetter
import sys
import math
import random
import os
files=["bike_a.png","bike_b.png"];
def getTime():
return datetime.datetime.now();
class CLPatchMatch:
'''
CLSeamCarving class,
performs the seam carving algorithm on an image to reduce its size without scaling
its main features, using OpenCL in realtime
'''
def __init__(self):
'''
Inits the opencl environment and parameters
'''
#profiling operation times
self.times={k:datetime.timedelta() for k in ("execute",
"init","randomfill")};
#show opencl compiler errors
os.environ["PYOPENCL_COMPILER_OUTPUT"]="1";
t=getTime();
self.ctx = cl.create_some_context(False)
self.queue = cl.CommandQueue(self.ctx)
self.times["init"]+=getTime()-t;
def loadProgram(self, filename):
t=getTime();
f = open(filename, 'r')
fstr = "".join(f.readlines())
#create the program
self.program = cl.Program(self.ctx, fstr).build()
self.times["init"]+=getTime()-t;
def loadImages(self,files):
'''
load both images into self.img[0,1]
'''
t=getTime();
self.img = [skimage.img_as_float(skimage.io.imread(files[i])) for i in (0,1)]
self.loadProgram("patchmatch.c")
self.times["init"]+=getTime()-t;
def randomfill(self):
t=getTime();
mf= cl.mem_flags
self.inputBuf=[cl.Buffer(self.ctx,mf.READ_ONLY | mf.COPY_HOST_PTR,hostbuf=self.img[i]) for i in [0,1]];
self.outputBuf=cl.Buffer(self.ctx,mf.WRITE_ONLY | mf.COPY_HOST_PTR ,hostbuf=self.nff)
self.program.randomfill(self.queue, self.effectiveSize, None,
numpy.int32(self.patchSize[0]), #patchHeight
numpy.int32(self.patchSize[1]), #patchWidth
numpy.int32(self.size[0]), #height
numpy.int32(self.size[1]), #width
self.inputBuf[0],
self.inputBuf[1],
self.outputBuf)
c = numpy.empty_like(self.nff)
cl.enqueue_read_buffer(self.queue, self.outputBuf, c).wait()
self.nff=numpy.copy(c);
self.times["randomfill"]+=getTime()-t;
def execute(self):
'''
execute an iteration of patchMatch
'''
t=getTime();
mf= cl.mem_flags
self.inputBuf=[cl.Buffer(self.ctx,mf.READ_ONLY | mf.COPY_HOST_PTR,hostbuf=self.img[i]) for i in [0,1]];
self.outputBuf=cl.Buffer(self.ctx,mf.READ_WRITE | mf.COPY_HOST_PTR ,hostbuf=self.nff)
self.program.propagate(self.queue, self.effectiveSize, None,
numpy.int32(self.patchSize[0]), #patchHeight
numpy.int32(self.patchSize[1]), #patchWidth
numpy.int32(self.size[0]), #height
numpy.int32(self.size[1]), #width
numpy.int32(self.iteration),
self.inputBuf[0],
self.inputBuf[1],
self.outputBuf)
c = numpy.empty_like(self.nff)
cl.enqueue_read_buffer(self.queue, self.outputBuf, c).wait()
self.nff=numpy.copy(c);
self.times["execute"]+=getTime()-t;
def _drawRect(self,img,y,x,height,width,color=(1,0,0)):
'''
used for demo, showing which rectangles match
'''
for i in range(0,width+1):
img[y][x+i]=color;
img[y+height][x+i]=color;
for i in range(0,height+1):
img[y+i][x]=color
img[y+i][x+width]=color;
def show(self,nffs=True):
'''
shows times and images
'''
samples=5;
for i in range(0,samples):
color=(random.random(),random.random(),random.random())
randomPoint=[(int)(random.random()*i) for i in self.effectiveSize];
self._drawRect(self.img[0],randomPoint[0],randomPoint[1],self.patchSize[0],self.patchSize[1],color);
self._drawRect(self.img[1],self.nff[randomPoint[0]][randomPoint[1]][0],self.nff[randomPoint[0]][randomPoint[1]][1],self.patchSize[0],self.patchSize[1],color);
for i in self.times.keys():
print i,":", (self.times[i].seconds*1000+self.times[i].microseconds/1000)/1000.0,"seconds"
if nffs:
for i in range(0,3):
pylab.imshow(self.nff[:,:,i])
pylab.show();
f=pylab.figure()
f.add_subplot(1,2,0);
pylab.imshow(self.img[0],cmap=matplotlib.cm.Greys_r);
f.add_subplot(1,2,1);
pylab.imshow(self.img[1],cmap=matplotlib.cm.Greys_r);
pylab.title("Patch Match (by AbiusX)")
pylab.show();
def D(self,y1,x1,y2,x2):
'''
compute difference of two patches
'''
return ((self.img[0][y1:y1+self.patchSize[0],x1:x1+self.patchSize[1]]-
self.img[1][y2:y2+self.patchSize[0],x2:x2+self.patchSize[1]])**2).sum();
def match(self,files,patchSize=(7,7),iterations=20,Demo=False):
'''
run the patchMatch algorithm on the images, returning nff array
'''
self.loadImages(files);
self.size=self.img[0].shape;
self.patchSize=patchSize;
self.effectiveSize=[self.size[i]-patchSize[i] for i in (0,1)];
self.nff=numpy.ndarray((self.effectiveSize[0],self.effectiveSize[1],3));
self.randomfill();
for i in range(0,iterations):
self.iteration=i+1;
if (Demo):
print "iteration",self.iteration
print "mean block difference:", self.nff[:,:,2].mean();
self.execute();
if (Demo): self.show();
return self.nff;
if __name__ == "__main__":
patchmatch = CLPatchMatch()
print "Please wait a few seconds...";
patchmatch.match(files,Demo=True);
print "Done."