0

I wanted to perform bilinear interpolation on a very large image(>20Mb). The conventional code takes very long time. I tried making it multiprocessing but in the results, only the last column of pixels seems to appear. Sorry if this is a noob question.

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)

Are there any alternate ways to optimize the code?

bumblebee
  • 1,811
  • 12
  • 19
Chinchan4
  • 1
  • 2

1 Answers1

0

If you are serious in solving this, please learn a 3D graphics framework such as OpenGL or DirectX, and let the GPU do the job. GPU's texture mapping is the feature that maps an image to any size, any shape images using the hardware-accelerated interpolation, which happens almost instantly.

Also you may have to use offscreen rendering to bring the rendered result back to main memory. Transferring images back and forth may take some time but it should be much much faster than doing everything in CPU.

OpenGL Texture Mapping

How to render offscreen on OpenGL?

hiroki
  • 381
  • 1
  • 7
  • Hi, Thanks for the answer.I'll definitely look into it but I'm still curious as to why my above code doesn't work. I am coding purely for educational purposes and not really to deploy my code anywhere. – Chinchan4 Feb 28 '19 at 05:37