我有一個 numpy 陣列,它包含來自影像(newimg)的 n(x 和 y)坐標(我將此 nparray 宣告為'line')。讓我們以 5 個坐標為例
[[ 33 101]
[170 95]
[151 190]
[125 223]
[115 207]]
然后我想在該坐標(點 1 到點 2,點 2 到點 3,....,點 n-1 到點 n 和點 n 到點 1)之間畫一條連續線
我試圖用 cv2.line 像這樣制作演算法
for a in range(5) :
cv2.line(newimg, line[a:], line[a 1:], (255,255,255), 1)
cv2.line(newimg, line, line[1], (255,255,255), 1)
它產生 SystemError: new style getargs format but argument is not a tuple
任何想法?
uj5u.com熱心網友回復:
問題:
有一些變化需要解決:
- 要訪問陣列中的單個值,請使用
line[i]. 回傳從 index到末尾line[i:]的點陣列。i cv2.line()接受元組作為起點和終點。
代碼:
# proposed line as an array
line = np.array([[ 33, 101], [170, 95], [151, 190], [125, 223], [115, 207]])
# blank image
newimg = np.zeros((300, 300, 3), np.uint8)
# iterate every point and connect a line between it and the next point
for a in range(len(line) - 1):
newimg = cv2.line(newimg, tuple(line[a]), tuple(line[a 1]), (255,255,255), 2)
newimg = cv2.line(newimg, tuple(line[0]), tuple(line[-1]), (255,255,255), 2)
輸出:

編輯
更新了代碼以在第一個點和最后一個點之間連接一條線line
uj5u.com熱心網友回復:
解決,我將代碼編輯為
for a in range(4) :
cv2.line(newimg, tuple(line[a]), tuple(line[a 1]), (255,255,255), 1)
cv2.line(newimg, tuple(line[4], tuple(line[0]), (255,255,255), 1)
和影像:

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/486906.html
上一篇:使用C openCV處理顫振影像
