How to randomize perspective transformations within a certain ratio
I am using OpenCV's perspective transformation to take a transparent logo and make it 3D. This is my goal:
The logo "Photoshop" is increasing from left to right. I also want to have it decrease from left to right. However, I don't to do fix the values for this. I am using random.randit(a,b) to get random values and I'm trying to use those random values to change the size and the transformations while maintaining the ratio.
I have a working code. I just couldn't figure out the mathematical logic to apply these transformations at random while maintaining the ratio.
def perspective_transform(path, lg_x, lg_y):
rand_x = random.randint(0, 5)
rand_y = random.randint(0, 5)
# read input
img = cv2.imread(path, cv2.IMREAD_UNCHANGED)
# specify desired output size
width = lg_x
height = lg_y
# specify conjugate x,y coordinates (not y,x)
input = np.float32([[0, 0], [width, 0], [width, height], [0, height]])
output = np.float32([[rand_x, rand_y], [418, rand_y], [442, 443], [rand_y, 438]]) # Randomize this
# compute perspective matrix
matrix = cv2.getPerspectiveTransform(input, output)
# do perspective transformation setting area outside input to black
imgOutput = cv2.warpPerspective(img, matrix, (width, height))
imgOutput = Image.fromarray(np.uint8(imgOutput)).convert('RGBA')
return imgOutput
In particular, these are the lines of interest. I have to adjust the coordinates of output:
# conjugate x,y coordinates (not y,x)
input = np.float32([[0, 0], [width, 0], [width, height], [0, height]])
output = np.float32([[rand_x, rand_y], [418, rand_y], [442, 443], [rand_y, 438]]) # Randomize this
from Recent Questions - Stack Overflow https://ift.tt/34J8SlW
https://ift.tt/3povIaq


Comments
Post a Comment