我嘗試撰寫最快的代碼來選擇輪廓內的一個點。我需要點坐標,而不是影像中的像素。我寫了這個定義OnePointInsideContour

問題是在北(紅色),南(綠色),東(青色)和西(藍色)點附近的輪廓上可能還有一些其他點,因此一旦找到第一個點,for回圈停止break并開始一個新的,尋找下一個條件。
僅當在輪廓上找到所有四個彩色點時,測驗點才被接受為在內部。
有什么建議可以加快執行速度嗎?
uj5u.com熱心網友回復:
您肯定可以通過改進回圈操作來減少計算時間。例如,您可以嘗試以下操作:
found1, found2, found3, found4 = False, False, False, False
for k in range(L):
ycont = list(contourArray[k])[0]
xcont = list(contourArray[k])[1]
if not found1 and (ycont > y0 - tol and ycont < y0 tol and xcont > x0):
p = (ycont, xcont)
counter = 1
ret.append(p)
found1 = True
if not found2 and (ycont > y0 - tol and ycont < y0 tol and xcont < x0):
p = (ycont, xcont)
counter = 1
ret.append(p)
found2 = True
if not found3 and (xcont > x0 - tol and xcont < x0 tol and ycont < y0):
p = (ycont, xcont)
counter = 1
ret.append(p)
found3 = True
if not found4 and (xcont > x0 - tol and xcont < x0 tol and ycont < y0):
p = (ycont, xcont)
counter = 1
ret.append(p)
found4 = True
請注意,您需要避免中斷回圈,因為您只嘗試迭代 L 一次。你似乎有一些可以擺脫的代碼復制,下面的所有部分都可以寫成一個函式。
...
p = (ycont, xcont)
counter = 1
ret.append(p)
...
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/465272.html
