Promising OpenCV conversion giving unexpected results

I am trying to convert from a trapezoid (in the first image) to a rectangle (in the second image), but get a weird result (in the third image).

enter image description here

My plan was to use a perspective transformation, defined by four corner points of the trapezoid and the four corner points of the rectangle.

In this example, for a trapezoid they are:

ptsTrap = [[ 50. 100. ] [ 50. 200. ] [ 250. 64.73460388] [ 250. 235.26539612]] 

and for the rectangle:

 ptsRect = [[ 50. 100.] [ 50. 200.] [ 250. 100.] [ 250. 200.]] 

I get the conversion prospects of these points:

 T = cv2.getPerspectiveTransform(ptsTrap, ptsRect) 

And then build an image from this:

 arrTrapToRect = cv2.warpPerspective(arrTrap, T, arrTrap.shape[:2]) 

However, as you can see from the image, this does not give the expected conversion.

I can not understand why even the points that define the transformation, is projected in accordance with it. Any ideas?

+4
source share
1 answer

Your methodology is correct. The problem arises when you indicate the coordinates of your corner points. I don’t know how you calculated them, but you changed your X and Y axes. This is reflected in the transformation applied to your final image. I believe the corner points are:

 ptsTrap = [[[ 99. 51.]] [[ 64. 251.]] [[ 234. 251.]] [[ 199. 51.]]] ptsRect = [[[ 102. 49.]] [[ 100. 249.]] [[ 200. 250.]] [[ 200. 50.]]] 

Search perspective transformation of these points gives the correct result: Perspective Transform result

For reference, this is the code I used:

 import cv2 import numpy as np def find_corners(image): im = cv2.Canny(image, 100, 200) cnt = cv2.findContours(im,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[0] cnt = cv2.approxPolyDP(cnt[0], 5, True) return cnt.astype(np.float32) def main(argv): trap = cv2.imread('trap.png', cv2.IMREAD_GRAYSCALE) rect = cv2.imread('rect.png', cv2.IMREAD_GRAYSCALE) ptsTrap = find_corners(trap) ptsRect = find_corners(rect) T = cv2.getPerspectiveTransform(ptsTrap, ptsRect) warp = cv2.warpPerspective(trap, T, rect.shape[:2]) cv2.imshow('', warp) cv2.imwrite('warp.png', warp) cv2.waitKey() cv2.destroyAllWindows() cv2.IMREAD_GRAYSCALE) import cv2 import numpy as np def find_corners(image): im = cv2.Canny(image, 100, 200) cnt = cv2.findContours(im,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[0] cnt = cv2.approxPolyDP(cnt[0], 5, True) return cnt.astype(np.float32) def main(argv): trap = cv2.imread('trap.png', cv2.IMREAD_GRAYSCALE) rect = cv2.imread('rect.png', cv2.IMREAD_GRAYSCALE) ptsTrap = find_corners(trap) ptsRect = find_corners(rect) T = cv2.getPerspectiveTransform(ptsTrap, ptsRect) warp = cv2.warpPerspective(trap, T, rect.shape[:2]) cv2.imshow('', warp) cv2.imwrite('warp.png', warp) cv2.waitKey() cv2.destroyAllWindows() cv2.IMREAD_GRAYSCALE) import cv2 import numpy as np def find_corners(image): im = cv2.Canny(image, 100, 200) cnt = cv2.findContours(im,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[0] cnt = cv2.approxPolyDP(cnt[0], 5, True) return cnt.astype(np.float32) def main(argv): trap = cv2.imread('trap.png', cv2.IMREAD_GRAYSCALE) rect = cv2.imread('rect.png', cv2.IMREAD_GRAYSCALE) ptsTrap = find_corners(trap) ptsRect = find_corners(rect) T = cv2.getPerspectiveTransform(ptsTrap, ptsRect) warp = cv2.warpPerspective(trap, T, rect.shape[:2]) cv2.imshow('', warp) cv2.imwrite('warp.png', warp) cv2.waitKey() cv2.destroyAllWindows() 
+7
source

Source: https://habr.com/ru/post/1491767/


All Articles