[英]OpenCV channels when trying to project point on image to 2d plane
從同一位置拍攝現場照片的點。
我的代碼如下:
import cv2
import numpy as np
if __name__ == '__main__':
# Read source image.
im_src = cv2.imread('field2.png')
# Four corners of the book in source image
pts_src = np.array([[436, 181], [319, 181], [539, 314], [180, 312]])
# Read destination image.
im_dst = cv2.imread('field1.png')
# Four corners of the book in destination image.
pts_dst = np.array([[297, 233], [297, 139], [55, 234], [54, 139]])
# Calculate Homography
h, status = cv2.findHomography(pts_src, pts_dst)
# provide a point you wish to map from image 1 to image 2
a = np.array([[493, 274]], dtype='float32')
a = np.array([a])
pointsOut = cv2.perspectiveTransform(a, h)
# Display image
cv2.imshow("Warped Source Image", pointsOut)
cv2.waitKey(0)
但是我遇到以下錯誤:
cv2.imshow(“ Warped Source Image”,pointsOut)
cv2.error:OpenCV(3.4.2)/io/opencv/modules/imgcodecs/src/utils.cpp:622:錯誤:(-15:通道數錯誤)源映像必須具有1、3或4個通道'cvConvertImage'
由於某種原因,這個問題對我來說很難解決-可能是我剛接觸OpenCV時-但我似乎無法解決。
看起來好像是在指
pointsOut = cv2.perspectiveTransform(a, h)
我找到了類似問題的答案 ,但由於已經將其設置為浮點,因此無法解決我的問題。
如果我不能使用perspectiveTransform()
這樣做,那么有人對我做錯了什么或解決此問題的更好方法有任何建議嗎?
編輯:我找到了此解決方案 ,但當我添加額外的括號時,它將引發新的錯誤。
pointsOut = cv2.perspectiveTransform(a,h)cv2.error:OpenCV(3.4.2)/io/opencv/modules/core/src/matmul.cpp:2268:error:(-215:Assertion failed)scn + 1 = =函數'perspectiveTransform'中的m.cols
我還嘗試在處理之前將兩個圖像都轉換為灰度,但仍然收到通道錯誤。
cv2.perspectiveTransform
是一個返回點而不是圖像的函數。 您必須繪制由其返回的點。 一個例子是
pointsOut = cv2.perspectiveTransform(a, H)
cv2.polylines(im_src, [np.int32(pointsOut)], True, (0, 255, 0), 5)
話雖如此,我的方法是先進行透視變換,然后將其包裹在圖像周圍。 與此片段類似,
M = cv2.getPerspectiveTransform(rect, dst)
warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))
其中maxWidth和maxHeight是源圖像的。
您可以在這里參考更多內容, https://www.pyimagesearch.com/2014/08/25/4-point-opencv-getperspective-transform-example/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.