You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have detected a possible inconsistency in cv2.perspectiveTransform.
I got an image with a polygon of known coordinates like the one below:
I know the destination coordinates of the polygon and I would like to rectify the whole image based on that transformation.
In order to do that, I first calculate the transformation between src and dst coordinates (all in the same order TopLeft, BottomLeft, BottomRight, TopRight) and then I use it to covert the corners of the image. However, this routine does not seem to work in all cases. Here is an example where the transformed coordinates seem to be completely wrong.
importcv2importnumpyasnpfromPILimportImage, ImageDrawnp.set_printoptions(precision=4)
np.set_printoptions(suppress=True)
#the image dimensionsimg_height=2048img_width=2048# image coordinates of polygonsrc=np.array([[ 749.31, 1175.72],
[ 749.31 , 364.12],
[1171.8, 668.94],
[1171.8 , 1105.63]])
# Create the imageimage=Image.new('RGB', (img_width, img_height))
image.paste( (200,200,200), [0,0,image.size[0],image.size[1]])
draw=ImageDraw.Draw(image)
draw.line(((src[0][0],src[0][1]),(src[1][0],src[1][1]), (src[2][0],src[2][1]),(src[3][0],src[3][1]), (src[0][0],src[0][1])), width=4, fill="blue")
#image.show()# known coordinatesdst=np.array([[0, 1000],
[0, 0],
[921, 0],
[921, 1000]])
# calculate the tranformationmat=cv2.getPerspectiveTransform(src.astype("float32"), dst.astype("float32"))
# the transformation seems to warp the polygon fine img_warp=Image.fromarray((
cv2.warpPerspective(np.array(image),
mat,
(921,
1000))))
img_warp.show()
# new source: image cornerscorners=np.array([
[0, img_height],
[0, 0],
[img_width, 0],
[img_width, img_height]
])
# Transform the corners of the imagecorners_tranformed=cv2.perspectiveTransform(
np.array([corners.astype("float32")]), mat)
# These tranformed corners seems completely wrong/inverted x-axis print(corners_tranformed)
# [[[ -483.1003 1506.5758]# [ -483.1003 119.5345]# [-3627.2617 3817.4143]# [-3627.2617 -2191.3757]]]
In other cases though the transformation seems to work fine, the transformed coordinates seem fine and then I can use them to rectify the full image as shown below:
This is related to the vanishing point of the two lines falling inside the image. When this happens, then the corners of the image are "beyond infinity" and this is why I get the weird numbers. I added a check to see where the intersection point is and I defined my new corners accordingly. So this is not a problem of perspectiveTransform.
System information (version)
Detailed description
I have detected a possible inconsistency in cv2.perspectiveTransform.
I got an image with a polygon of known coordinates like the one below:
I know the destination coordinates of the polygon and I would like to rectify the whole image based on that transformation.
In order to do that, I first calculate the transformation between src and dst coordinates (all in the same order TopLeft, BottomLeft, BottomRight, TopRight) and then I use it to covert the corners of the image. However, this routine does not seem to work in all cases. Here is an example where the transformed coordinates seem to be completely wrong.
In other cases though the transformation seems to work fine, the transformed coordinates seem fine and then I can use them to rectify the full image as shown below:
Is this a bug or am I missing something?
This person seems to have the same problem:
https://answers.opencv.org/question/231802/opencv-getperspectivetransform-method-gives-unclear-result/
Issue submission checklist
answers.opencv.org, Stack Overflow, etc and have not found solution
The text was updated successfully, but these errors were encountered: