相機(jī)坐標(biāo)到圖像坐標(biāo)的轉(zhuǎn)換通常需要使用相機(jī)內(nèi)參矩陣和外參矩陣。在OpenCV中,可以通過cv2.projectPoints()函數(shù)實現(xiàn)相機(jī)坐標(biāo)到圖像坐標(biāo)的轉(zhuǎn)換。具體的程序如下:
import cv2
import numpy as np
# 讀取圖像和相機(jī)參數(shù)
img = cv2.imread('test.jpg')
K = np.array([[1000, 0, 500], [0, 1000, 500], [0, 0, 1]], dtype=np.float32)
R = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]], dtype=np.float32)
t = np.array([0, 0, 0], dtype=np.float32)
# 定義三維坐標(biāo)點
point_3d = np.array([[0, 0, 1]], dtype=np.float32)
# 進(jìn)行相機(jī)坐標(biāo)到圖像坐標(biāo)的轉(zhuǎn)換
point_2d, _ = cv2.projectPoints(point_3d, R, t, K, distCoeffs=None)
# 將結(jié)果轉(zhuǎn)換為整數(shù)
point_2d = tuple(map(int, point_2d[0].tolist()))
# 在圖像上繪制點
cv2.circle(img, point_2d, 3, (0, 0, 255), -1)
# 顯示圖像
cv2.imshow('Image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
以上代碼中,讀取了一張圖像和相機(jī)參數(shù),使用cv2.projectPoints()函數(shù)進(jìn)行相機(jī)坐標(biāo)到圖像坐標(biāo)的轉(zhuǎn)換,最后在圖像上繪制了轉(zhuǎn)換后的點并顯示出來。文章來源:http://www.zghlxwxcb.cn/news/detail-514448.html
需要注意的是,cv2.projectPoints()函數(shù)的第一個參數(shù)是三維坐標(biāo)點的數(shù)組,每行表示一個點的坐標(biāo);第二個參數(shù)是旋轉(zhuǎn)矩陣;第三個參數(shù)是平移向量;第四個參數(shù)是相機(jī)內(nèi)參矩陣;第五個參數(shù)是相機(jī)的畸變系數(shù),如果沒有畸變可以設(shè)置為None。函數(shù)的返回值包括轉(zhuǎn)換后的二維坐標(biāo)點和每個點的向量長度,可以通過下劃線接收。文章來源地址http://www.zghlxwxcb.cn/news/detail-514448.html
到了這里,關(guān)于opencv相機(jī)坐標(biāo)到圖像坐標(biāo)的轉(zhuǎn)換的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!