accelerate

This commit is contained in:
fxia22 2017-09-14 17:38:41 -07:00
parent 07d210f412
commit 48d7cb4808
3 changed files with 70 additions and 23 deletions

View File

@ -9,9 +9,11 @@ import numpy as np
import PIL
from PIL import Image
import scipy.misc
import os
from cube2equi import find_corresponding_pixel
from transfer import transfer2
context = zmq.Context()
@ -23,7 +25,7 @@ socket = context.socket(zmq.REQ)
socket.connect("tcp://localhost:5555")
# Do 10 requests, waiting each time for a response
for request in range(10):
for request in range(6):
print("Sending request %s ..." % request)
socket.send(b"Hello")
@ -45,11 +47,29 @@ for request in range(10):
print("Received reply %s [ %s ]" % (request, data))
def transfer(in_img, coords, h, w):
out_img = np.zeros((h,w,3)).astype(np.uint8)
for ycoord in range(0, h):
for xcoord in range(0, w):
ind, corrx, corry = coords[ycoord, xcoord, :]
out_img[ycoord, xcoord, :] = in_img[ind, corrx, corry, :]
return out_img
def convert_img():
#inimg = Image.open(infile)
inimg = InImg()
in_imgs = np.array([np.array(Image.open("0.tiff")),
np.array(Image.open("1.tiff")),
np.array(Image.open("2.tiff")),
np.array(Image.open("3.tiff")),
np.array(Image.open("4.tiff")),
np.array(Image.open("5.tiff"))]).astype(np.uint8)
print(in_imgs.shape)
wo, ho = inimg.grid * 4, inimg.grid * 3
# Calculate height and width of output image, and size of each square face
@ -58,34 +78,36 @@ def convert_img():
n = ho/3
# Create new image with width w, and height h
outimg = Image.new('RGB', (w, h))
outimg = np.zeros((h,w,3)).astype(np.uint8)
if not os.path.isfile('coord.npy'):
coords = np.zeros((h,w,3)).astype(np.int32)
for ycoord in range(0, h):
for xcoord in range(0, w):
corrx, corry = find_corresponding_pixel(xcoord, ycoord, w, h, n)
coords[ycoord, xcoord, :] = inimg.getpixel((corrx, corry))
np.save('coord.npy', coords)
else:
coords = np.load('coord.npy')
# For each pixel in output image find colour value from input image
for ycoord in range(0, h):
for xcoord in range(0, w):
corrx, corry = find_corresponding_pixel(xcoord, ycoord, w, h, n)
outimg.putpixel((xcoord, ycoord), inimg.getpixel((corrx, corry)))
# Print progress percentage
print str(round((float(ycoord)/float(h))*100, 2)) + '%'
outimg = transfer2(in_imgs, coords, h, w)
outimg = Image.fromarray(outimg)
outimg.save('output', 'PNG')
class InImg(object):
def __init__(self):
self.grid = 512
self.img0 = Image.open("0.tiff")
self.img1 = Image.open("1.tiff")
self.img2 = Image.open("2.tiff")
self.img3 = Image.open("3.tiff")
self.img4 = Image.open("4.tiff")
self.img5 = Image.open("5.tiff")
def getpixel(self, key):
corrx, corry = key[0], key[1]
imgs = [[self.img0], [self.img1, self.img2, self.img3, self.img4], [self.img5]]
indx = int(corrx / self.grid)
indy = int(corry / self.grid)
@ -93,9 +115,11 @@ class InImg(object):
remx = int(corrx % self.grid)
remy = int(corry % self.grid)
if (indy == 0 or indy == 2):
return imgs[indy][0].getpixel((remx, remy))
if (indy == 0):
return (0, remx, remy)
elif (indy == 2):
return (5, remx, remy)
else:
return imgs[1][indx].getpixel((remx, remy))
return (indx + 1, remx, remy)
convert_img()
convert_img()

View File

@ -0,0 +1,4 @@
from distutils.core import setup
from Cython.Build import cythonize
setup(name="transfer", ext_modules=cythonize('transfer.pyx'),)

View File

@ -0,0 +1,19 @@
import numpy as np
def transfer2(unsigned char [:,:,:,:] in_img, int [:,:,:]coords, int h, int w):
cdef unsigned char [:,:,:] out_img = np.zeros((h,w,3)).astype(np.uint8)
cdef int xcoord, ycoord
cdef int ind, corrx, corry
cdef int c
for ycoord in range(0, h):
for xcoord in range(0, w):
ind = coords[ycoord, xcoord, 0]
corrx = coords[ycoord, xcoord, 1]
corry = coords[ycoord, xcoord, 2]
for c in range(3):
out_img[ycoord, xcoord, c] = in_img[ind, corrx, corry, c]
return np.array(out_img)