我想对一个非常大的图像(> 20Mb)执行双线性插值。常规代码需要很长时间。我尝试将其设为多处理,但在结果中,似乎只出现了最后一列像素。对不起,如果这是一个菜鸟问题。
def GetBilinearPixel(imArr,r,c, posX, posY,enlargedShape):
out=[]
modXi = int(posX)
modYi = int(posY)
modXf = posX - modXi
modYf = posY - modYi
modXiPlusOneLim = min(modXi+1,imArr.shape[1]-1)
modYiPlusOneLim = min(modYi+1,imArr.shape[0]-1)
for chan in range(imArr.shape[2]):
bl = imArr[modYi, modXi, chan]
br = imArr[modYi, modXiPlusOneLim, chan]
tl = imArr[modYiPlusOneLim, modXi, chan]
tr = imArr[modYiPlusOneLim, modXiPlusOneLim, chan]
b = modXf * br + (1. - modXf) * bl
t = modXf * tr + (1. - modXf) * tl
pxf = modYf * t + (1. - modYf) * b
out.append(int(pxf))
enlargedShape[r, c]=out
if __name__ == '__main__':
im = cv.imread('new.jpeg')
#print im.shape
#manager = multiprocessing.Manager()
size=map(int, [im.shape[0]*2, im.shape[1]*2, im.shape[2]])
print size
enlargedShape=sharedmem.full(size, 0, dtype=np.uint8)
#print enlargedShape
#enlargedShape = list(map(int, [im.shape[0]*2, im.shape[1]*2, im.shape[2]]))
rowScale = float(im.shape[0]) / float(enlargedShape.shape[0])
colScale = float(im.shape[1]) / float(enlargedShape.shape[1])
#My Code starts her
jobs = []
for r in range(enlargedShape.shape[0]):
for c in range(enlargedShape.shape[1]):
orir = r * rowScale
oric = c * colScale
#enlargedImg[r, c] = GetBilinearPixel(im, oric, orir)
#My code
p = multiprocessing.Process(target=GetBilinearPixel, args=(im,r,c, oric, orir,enlargedShape))
jobs.append(p)
p.start()
p.join()
print enlargedShape
cv.imshow("cropped",enlargedShape)
cv.waitKey(0)
是否有任何替代方法来优化代码?