簡體   English   中英

如何反轉warpPerspective()

[英]How to reverse warpPerspective()

我得到的是,當我使用轉換矩陣的逆函數再次應用warpPerspective()時,我得到了第一個圖像:

M = cv2.getPerspectiveTransform(pts1,pts2)
warped = cv2.warpPerspective(img, M, (cols,rows))

# ... some operations, obtain a rectangle

ret, IM = cv2.invert(M)
restored = cv2.warpPerspective(warped, IM, (cols,rows))

但是,我對warped應用了一些操作,並且在圖像上得到了一些位置(如矩形)。 如何在restored圖像上找到矩形的相應坐標?

感謝python和C ++的答案。

您應該能夠簡單地將矩形的坐標作為輸入數組傳遞給warpPerspective函數,以獲取相應的變換點。 例如(C ++中帶有std :: vector的示例代碼):

//r1,r2,r3,r4 are the four points of your rectangle in the source image
std::vector<Point2f> rectangle_vertexes;
rectangle_vertexes.push_back(r1);
rectangle_vertexes.push_back(r2);
rectangle_vertexes.push_back(r3);
rectangle_vertexes.push_back(r4);
Mat transformed_vertexes;
warpPerspective(Mat(rectangle_vertexes),transformed_vertexes,IM,Size(1,4),WARP_INVERSE_MAP);

觀察到,如果需要在src圖像上應用變換M以獲得dst圖像,則需要設置標志WARP_INVERSE_MAP

希望這可以幫到你

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM