python實(shí)現(xiàn)圖像外邊界跟蹤操作
share一些python實(shí)現(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鄰域 順時(shí)針方向搜索 neighbor = [[-1,-1],[0,-1],[1,-1],[1,0],[1,1],[0,1],[-1,1],[-1,0]] neighbor_len = len(neighbor) #先從當(dāng)前點(diǎn)的左上方開始, # 如果左上方也是黑點(diǎn)(邊界點(diǎn)): # 搜索方向逆時(shí)針旋轉(zhuǎn)90 i-=2 # 否則: # 搜索方向順時(shí)針旋轉(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")
搜索過程,紅色標(biāo)記線如下:
補(bǔ)充知識(shí):python實(shí)現(xiàn)目標(biāo)跟蹤(opencv)
1.單目標(biāo)跟蹤
import cv2 import sys (major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.') print(major_ver, minor_ver, subminor_ver) if __name__ == '__main__': # 創(chuàng)建跟蹤器 tracker_type = 'MIL' tracker = cv2.TrackerMIL_create() # 讀入視頻 video = cv2.VideoCapture("./data/1.mp4") # 讀入第一幀 ok, frame = video.read() if not ok: print('Cannot read video file') sys.exit() # 定義一個(gè)bounding box bbox = (287, 23, 86, 320) bbox = cv2.selectROI(frame, False) # 用第一幀初始化 ok = tracker.init(frame, bbox) while True: ok, frame = video.read() if not ok: break # Start timer timer = cv2.getTickCount() # Update tracker ok, bbox = tracker.update(frame) # Cakculate FPS fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer) # Draw bonding box if ok: p1 = (int(bbox[0]), int(bbox[1])) p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3])) cv2.rectangle(frame, p1, p2, (255,0,0), 2, 1) else: cv2.putText(frame, "Tracking failed detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2) # 展示tracker類型 cv2.putText(frame, tracker_type+"Tracker", (100, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2) # 展示FPS cv2.putText(frame, "FPS:"+str(fps), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2) # Result cv2.imshow("Tracking", frame) # Exit k = cv2.waitKey(1) & 0xff if k ==27 : break
2.多目標(biāo)跟蹤
使用GOTURN作為跟蹤器時(shí),須將goturn.caffemodel和goturn.prototxt放到工作目錄才能運(yùn)行,解決問題鏈接https://stackoverflow.com/questions/48802603/getting-deep-learning-tracker-goturn-to-run-opencv-python
import cv2 import sys (major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.') print(major_ver, minor_ver, subminor_ver) if __name__ == '__main__': # 創(chuàng)建跟蹤器 # 'BOOSTING', 'MIL','KCF', 'TLD', 'MEDIANFLOW', 'GOTURN', 'MOSSE' tracker_type = 'MIL' tracker = cv2.MultiTracker_create() # 創(chuàng)建窗口 cv2.namedWindow("Tracking") # 讀入視頻 video = cv2.VideoCapture("./data/1.mp4") # 讀入第一幀 ok, frame = video.read() if not ok: print('Cannot read video file') sys.exit() # 定義一個(gè)bounding box box1 = cv2.selectROI("Tracking", frame) box2 = cv2.selectROI("Tracking", frame) box3 = cv2.selectROI("Tracking", frame) # 用第一幀初始化 ok = tracker.add(cv2.TrackerMIL_create(), frame, box1) ok1 = tracker.add(cv2.TrackerMIL_create(), frame, box2) ok2 = tracker.add(cv2.TrackerMIL_create(), frame, box3) while True: ok, frame = video.read() if not ok: break # Start timer timer = cv2.getTickCount() # Update tracker ok, boxes = tracker.update(frame) print(ok, boxes) # Cakculate FPS fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer) for box in boxes: # Draw bonding box if ok: p1 = (int(box[0]), int(box[1])) p2 = (int(box[0] + box[2]), int(box[1] + box[3])) cv2.rectangle(frame, p1, p2, (255, 0, 0), 2, 1) else: cv2.putText(frame, "Tracking failed detected", (100, 80), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255),2) # 展示tracker類型 cv2.putText(frame, tracker_type+"Tracker", (100, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2) # 展示FPS cv2.putText(frame, "FPS:"+str(fps), (100, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (50, 170, 50), 2) # Result cv2.imshow("Tracking", frame) # Exit k = cv2.waitKey(1) & 0xff if k ==27 : break
以上這篇python實(shí)現(xiàn)圖像外邊界跟蹤操作就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python技巧之變長(zhǎng)和定長(zhǎng)序列拆分
這篇文章主要給大家分享的是Python技巧之變長(zhǎng)和定長(zhǎng)序列拆分,Python中的任何序列(可迭代的對(duì)象)都可以通過賦值操作進(jìn)行拆分,包括但不限于元組、列表、字符串、文件、迭代器、生成器等。想了解更多詳細(xì)的小伙伴請(qǐng)參考下面文章內(nèi)容2021-12-12結(jié)合Python的SimpleHTTPServer源碼來解析socket通信
SimpleHTTPServer是Python中一個(gè)現(xiàn)成的HTTP服務(wù)器例子,本文我們將結(jié)合Python的SimpleHTTPServer源碼來解析socket通信,我們先來看一下socket的基本概念:2016-06-06python 刪除excel表格重復(fù)行,數(shù)據(jù)預(yù)處理操作
這篇文章主要介紹了python 刪除excel表格重復(fù)行,數(shù)據(jù)預(yù)處理操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-07-07卷積神經(jīng)網(wǎng)絡(luò)的發(fā)展及各模型的優(yōu)缺點(diǎn)及說明
這篇文章主要介紹了卷積神經(jīng)網(wǎng)絡(luò)的發(fā)展及各模型的優(yōu)缺點(diǎn)及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02Python語法學(xué)習(xí)之線程的創(chuàng)建與常用方法詳解
本文主要介紹了線程的使用,線程是利用進(jìn)程的資源來執(zhí)行業(yè)務(wù),并且通過創(chuàng)建多個(gè)線程,對(duì)于資源的消耗相對(duì)來說會(huì)比較低,今天就來看一看線程的使用方法具體有哪些吧2022-04-04python如何實(shí)現(xiàn)單向鏈表及單向鏈表的反轉(zhuǎn)
這篇文章主要介紹了python如何實(shí)現(xiàn)單向鏈表及單向鏈表的反轉(zhuǎn),幫助大家更好的理解和學(xué)習(xí)使用python,感興趣的朋友可以了解下2021-03-03python簡(jiǎn)單實(shí)現(xiàn)刷新智聯(lián)簡(jiǎn)歷
本文給大家分享的是個(gè)人弄的一個(gè)使用Python簡(jiǎn)單實(shí)現(xiàn)刷新智聯(lián)招聘簡(jiǎn)歷的小工具的代碼,非常的簡(jiǎn)單,給大家參考下吧。2016-03-03python實(shí)現(xiàn)MySQL指定表增量同步數(shù)據(jù)到clickhouse的腳本
這篇文章主要介紹了python實(shí)現(xiàn)MySQL指定表增量同步數(shù)據(jù)到clickhouse的腳本,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02