OpenCV warpperspective

后端 未结 9 2356
孤城傲影
孤城傲影 2020-12-01 05:45

For some reason whenever I use OpenCV\'s warpPerspective() function, the final warped image does not contain everything in the original image. The left part of the image see

9条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-01 06:15

    Matt's answer is a good start, and he is correct in saying you need to multiply your homography by

    [ 1 , 0 , x_offset]
    [ 0 , 1 , y_offset]
    [ 0 , 0 ,    1    ]
    

    But he does not specify what x_offset and y_offset are. Other answers have said just take the perspective transform, but that is not correct. You want to take the INVERSE perspective transform.

    Just because a point 0,0 transforms into, say, -10,-10, does not mean that shifting the image by 10,10 will result in a non-cropped image. This is because point 10,10 does not necessarily map into 0,0.
    What you want to do is find out what point would map into 0,0, and shift the image by that much. To do that you take the inverse (cv2.invert) of the homography and apply perspectiveTransform.

    does not imply:

    You need to apply a reverse transform to find the correct points.

    This will get the correct x_offset and y_offset to align your top left point. From there to find the correct bounding box and fit the entire image perfectly, you need to figure out the skew (how much the image slants left or up after your normal, non-inverse, transformation) and add that amount to your x_offset and y_offset as well.

    EDIT: This is all theory. Images are a few pixels off in my tests, I'm not sure why.

提交回复
热议问题