[Python] Manually writing code for warpAffine in python

Discussão em 'Python' iniciado por Stack, Junho 15, 2025.

  1. Stack

    Stack Membro Participativo

    I want to implement affine transformation by not using library functions. I have an image named "transformed" and I want to apply inverse transformation to obtain "img_org" image. Right now, I am using my own basic GetBilinearPixel function to set the intensity value. But, the image is not transforming properly.This is what I came up with. :

    This is image("transformed.png"):

    [​IMG]

    This is image("img_org.png"):

    [​IMG]

    But My goal is to produce this image: [​IMG]

    You can see the transformation matrix here:

    pts1 = np.float32( [[693,349] , [605,331] , [445,59]] )
    pts2 = np.float32 ( [[1379,895] , [1213,970] ,[684,428]] )
    Mat = cv2.getAffineTransform(pts2,pts1)
    B=Mat


    code:

    img_org = np.zeros(shape=(780,1050))
    img_size = np.zeros(shape=(780,1050))

    def GetBilinearPixel(imArr, posX, posY):
    return imArr[posX][posY]

    for i in range(1, img.shape[0]-1):
    for j in range(1, img.shape[1]-1):
    pos = np.array([, [j], [1]], np.float32)
    # print pos
    pos = np.matmul(B,pos)
    r = int(pos[0][0])
    c = int(pos[1][0])
    # print r,c
    if(c <= 1024 and r <= 768 and c >= 0 and r >= 0):
    img_size[r][c] = img_size[r][c]+1
    img_org[r][c] += GetBilinearPixel(img, i, j)

    for i in range(0,img_org.shape[0]):
    for j in range(0,img_org.shape[1]):
    if(img_size[j]>0):
    img_org[j] = img_org[j]/img_size[j]


    Is my logic wrong? I know that i have applied very inefficient algorithm. Is there any insight that i am missing? Or can you give me any other algorithm which will work fine.

    (Request) . I don't want to use warpAffine function.

    Continue reading...

Compartilhe esta Página