pythonopencvimage-processingpolynomialsdistortion

Altering the shape of an image to take the shape of an enclosed image with Python


I am new to Python. I would like to warp the first image such that it fills the closed path of the second image. I have two images. One is the source image and the other is an enclosed image. Is it possible to fill the image within any kind of closed path. Can you please suggest Python libraries to be used or it'd be great if you can share some code. I was also trying to work with corner detection algorithms and making them to stick with map function, but I can't.

Two images and the end result which I'm expecting to get:

Two images and the end result which I'm expecting to get


Solution

  • Here is an example of overlaying one image onto another using a mask with Python/OpenCV.

    tshirt image:

    enter image description here

    pattern image:

    enter image description here

    tshirt mask image:

    enter image description here

     - Read the 3 images and get shapes
     - Convert the mask to gray and binarize
     - Resize the pattern so that its smallest dimension is the size of the largest dimension of the tshirt image
     - Crop it to exactly the same size as the tshirt image
     - Apply the mask to the pattern image
     - Apply the inverse mask to the tshirt image
     - Add the two together
     - Save the results
    


    import cv2
    import numpy as np
    
    # read shirt image and get its max dimension
    img = cv2.imread('tshirt.jpg')
    hh, ww = img.shape[:2]
    maxwh = max(hh, ww)
    
    # read pattern image and get its size and minimum dimension
    pattern = cv2.imread('tshirt_pattern.jpg')
    ht, wd = pattern.shape[:2]
    minwh = min(ht,wd)
    
    # read shirt mask image
    maskimg = cv2.imread('tshirt_mask.png')
    
    # convert mask to gray and binarize
    maskimg = cv2.cvtColor(maskimg, cv2.COLOR_BGR2GRAY)
    maskimg = cv2.threshold(maskimg, 128, 255, cv2.THRESH_BINARY)[1]
    
    # resize pattern so minimum dimension is size of largest dimension of tshirt image
    scale = maxwh/minwh
    pattern_enlarge = cv2.resize(pattern, dsize=(0,0), fx=scale, fy=scale)
    
    # limit resized pattern to size of tshirt
    pattern_enlarge = pattern_enlarge[0:hh, 0:ww]
    
    # do masked overlay
    pattern_masked = cv2.bitwise_and(pattern_enlarge, pattern_enlarge, mask=maskimg)
    img_masked = cv2.bitwise_and(img, img, mask=(255-maskimg))
    result = cv2.add(img_masked, pattern_masked)
    
    cv2.imshow('image', img)
    cv2.imshow('pattern', pattern)
    cv2.imshow('mask', maskimg)
    cv2.imshow('masked pattern', pattern_masked)
    cv2.imshow('masked image', img_masked)
    cv2.imshow('result', result)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
    # save results
    cv2.imwrite('tshirt_masked.jpg', img_masked)
    cv2.imwrite('pattern_masked.jpg', pattern_masked)
    cv2.imwrite('tshirt_pattern_overlay.jpg', result)
    


    masked pattern image:

    enter image description here

    masked tshirt image:

    enter image description here

    result image:

    enter image description here