def find_code(self,img ):
h, w = img.shape[:2]
gray=img
thrs1 = cv2.getTrackbarPos('thrs1', 'edge')
thrs2 = cv2.getTrackbarPos('thrs2', 'edge')
edge = cv2.Canny(gray, thrs1, thrs2, apertureSize=5)
vis = cv2.cvtColor(img.copy(), cv2.COLOR_GRAY2BGR)
vis /= 2
#vis[edge != 0] = (0, 255, 0)
#cv2.imshow('edge', vis)
vis2 = np.zeros((h, w), np.uint8)
vis2[edge != 0] = 255
_, contours0, hierarchy = cv2.findContours(vis2.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours = [cv2.approxPolyDP(cnt, 3, True) for cnt in contours0]
selected = []
# **[Next, Previous, First_Child, Parent]**
if hierarchy != None:
for c, h in zip(contours, hierarchy[0]):
if h[0] == -1 and h[1] == -1:
kid = h[2]
if kid != -1:
kidh = hierarchy[0][kid]
if kidh[0] == -1 and kidh[1] == -1: ### only checking for nested circles, without brothers
selected.append(c)
cv2.drawContours(vis, selected, -1, (255, 0, 0), 2, cv2.LINE_AA)
for candidate in selected:
try:
self.try_to_decode(candidate, gray, vis)
except Exception as e:
print(e)
cv2.imshow('contours', vis)
ch = cv2.waitKey(5) & 0xFF
if ch == 27:
exit()
token_locator.py 文件源码
python
阅读 26
收藏 0
点赞 0
评论 0
评论列表
文章目录