0

I am cutting out an image of an animal, aligning it, modifying it, then I want to paste it back into the original image. So the workflow is like this:

Original image:
Original image

Animal detected:
Animal detected

Animal aligned:
Animal aligned

Animal extracted:
Animal extracted

Modified Animal:
Modified Animal

Repasted Animal:
Repasted Animal

I have the code to detect and align the animal:

# get rotation matrix
M = cv2.getRotationMatrix2D(eyesCenter, angle, scale)
# update the translation component of the matrix
tX = w * 0.5
tY = h * LeftEye[1]
M[0, 2] += (tX - eyesCenter[0])
M[1, 2] += (tY - eyesCenter[1])
# apply the affine transformation
output = cv2.warpAffine(image, M, (w, h), flags=cv2.INTER_CUBIC)

I tried to use

Mp = np.linalg.inv(M)

But I get the error:

Last 2 dimensions of the array must be square

How can I place the owl back into original image?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241

1 Answers1

2

Add a third row of [0, 0, 1]. This has the effect of making the product y = A x a 2d homogeneous vector (i.e. one with three components) whose first two components are the same as if you had not added the row.

Francesco Callari
  • 11,300
  • 2
  • 25
  • 40