創(chuàng)新互聯(lián)www.cdcxhl.cn八線動態(tài)BGP香港云服務器提供商,新人活動買多久送多久,劃算不套路!
創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設、高性價比寧陽網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式寧陽網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設找我們,業(yè)務覆蓋寧陽地區(qū)。費用合理售后完善,10余年實體公司更值得信賴。這篇文章主要為大家展示了python如何實現(xiàn)圖像外邊界跟蹤,內(nèi)容簡而易懂,希望大家可以學習一下,學習完之后肯定會有收獲的,下面讓小編帶大家一起來看看吧。
share一些python實現(xiàn)的code
#!/usr/bin/env python #coding=utf-8 import cv2 img = cv2.imread("trace_border2.bmp") [img_h, img_w, img_channel] = img.shape trace = [] start_x = 0 start_y = 0 gray = img[:,:,1] for h in range(img_h): for w in range(img_w): if (gray[h,w] > 128): gray[h,w] = 255 else: gray[h,w] = 0 #python 跳出多重循環(huán) #https://www.cnblogs.com/xiaojiayu/p/5195316.html class getoutofloop(Exception): pass try: for h in range(img_h - 2): for w in range(img_w - 2): if gray[h,w] == 0: start_x = w start_y = h raise getoutofloop except getoutofloop: pass print("Start Point (%d %d)"%(start_x, start_y)) trace.append([start_x, start_y]) # 8鄰域 順時針方向搜索 neighbor = [[-1,-1],[0,-1],[1,-1],[1,0],[1,1],[0,1],[-1,1],[-1,0]] neighbor_len = len(neighbor) #先從當前點的左上方開始, # 如果左上方也是黑點(邊界點): # 搜索方向逆時針旋轉(zhuǎn)90 i-=2 # 否則: # 搜索方向順時針旋轉(zhuǎn)45 i+=1 i = 0 cur_x = start_x + neighbor[i][0] cur_y = start_y + neighbor[i][1] is_contour_point = 0 try: while not ((cur_x == start_x) and (cur_y == start_y)): is_contour_point = 0 while is_contour_point == 0: #neighbor_x = cur_x + if gray[cur_y, cur_x] == 0: is_contour_point = 1 trace.append([cur_x, cur_y]) i -= 2 if i < 0: i += neighbor_len else: i += 1 if i >= neighbor_len: i -= neighbor_len #print(i) cur_x = cur_x + neighbor[i][0] cur_y = cur_y + neighbor[i][1] except: print("throw error") for i in range(len(trace)-1): cv2.line(img,(trace[i][0],trace[i][1]), (trace[i+1][0], trace[i+1][1]),(0,0,255),3) cv2.imshow("img", img) cv2.waitKey(10) cv2.rectangle(img,(start_x, start_y),(start_x + 20, start_y + 20),(255,0,0),2) cv2.imshow("img", img) cv2.waitKey(0) cv2.destroyWindow("img")