中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

python如何實現圖像外邊界跟蹤

發布時間:2020-07-17 09:48:27 來源:億速云 閱讀:297 作者:小豬 欄目:開發技術

這篇文章主要為大家展示了python如何實現圖像外邊界跟蹤,內容簡而易懂,希望大家可以學習一下,學習完之后肯定會有收獲的,下面讓小編帶大家一起來看看吧。

share一些python實現的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 跳出多重循環
#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)
 
#先從當前點的左上方開始,
# 如果左上方也是黑點(邊界點):
#     搜索方向逆時針旋轉90 i-=2
# 否則:
#     搜索方向順時針旋轉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")

補充知識:python實現目標跟蹤(opencv)

1.單目標跟蹤

import cv2
import sys
 
(major_ver, minor_ver, subminor_ver) = (cv2.__version__).split('.')
print(major_ver, minor_ver, subminor_ver)
 
if __name__ == '__main__':
  # 創建跟蹤器
  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()
  # 定義一個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.多目標跟蹤

使用GOTURN作為跟蹤器時,須將goturn.caffemodel和goturn.prototxt放到工作目錄才能運行,解決問題鏈接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__':
  # 創建跟蹤器
  # 'BOOSTING', 'MIL','KCF', 'TLD', 'MEDIANFLOW', 'GOTURN', 'MOSSE'
  tracker_type = 'MIL'
  tracker = cv2.MultiTracker_create()
  # 創建窗口
  cv2.namedWindow("Tracking")
  # 讀入視頻
  video = cv2.VideoCapture("./data/1.mp4")
  # 讀入第一幀
  ok, frame = video.read()
  if not ok:
    print('Cannot read video file')
    sys.exit()
  # 定義一個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如何實現圖像外邊界跟蹤的內容,如果你們有學習到知識或者技能,可以把它分享出去讓更多的人看到。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

通化市| 绥阳县| 西安市| 威远县| 建德市| 横峰县| 故城县| 周至县| 双城市| 普兰县| 宝应县| 叙永县| 黔东| 石狮市| 桐梓县| 中阳县| 镇江市| 安陆市| 大理市| 陆良县| 乐至县| 深圳市| 雷州市| 额济纳旗| 海伦市| 鸡泽县| 新竹县| 城固县| 兴山县| 广德县| 襄垣县| 资中县| 翼城县| 福泉市| 武安市| 资阳市| 承德市| 汉川市| 榆林市| 乌兰浩特市| 福贡县|