2 years ago

#40446

test-img

Nguyen Viet

How to do stretching Image distortion transformation

I have an assignment to transform an image

im1

to one that has distortion effect like a dent, squeeze, stretch like this:

im2

im3

im4

I have done with twirling, fisheye, bulge, but I'm having a hard time finding the right formulas for those effects. here is my code for twirling:

import numpy as np
import cv2 
import math 
from google.colab.patches import cv2_imshow
img = cv2.imread("./orig_img.png")

h,w,_ = img.shape
flex_x = np.zeros((h,w),np.float32)
flex_y = np.zeros((h,w),np.float32)

scale_y= 1
scale_x = 1
alpha = -1.8
center_x, center_y = (w // 2, h // 2)
radius = h/5

for y in range(h):
    delta_y = scale_y * (y - center_y)
    for x in range(w):
        delta_x = scale_x * (x - center_x)
        distance = delta_x * delta_x + delta_y * delta_y

        if distance >= (radius * radius):
            flex_x[y, x] = x
            flex_y[y, x] = y
        else:
            theta = np.arctan2(delta_x,delta_y) + alpha*(radius-math.sqrt(distance))/radius
            r_sin = math.sqrt(distance)*np.cos(theta)
            r_cos = math.sqrt(distance)*np.sin(theta)
            flex_x[y, x] = r_cos + center_x
            flex_y[y, x] = r_sin + center_y

dst = cv2.remap(img, flex_x, flex_y, cv2.INTER_LINEAR)
cv2_imshow(dst)

Anyone who has experience with this kind of transformation, please help me! I'm really thankful.

opencv

image-processing

computer-vision

transformation

distortion

0 Answers

Your Answer

Accepted video resources