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实现图像外边界跟踪操作

补充知识: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实现图像外边界跟踪操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

标签:
python,图像,边界跟踪

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
评论“python实现图像外边界跟踪操作”
暂无“python实现图像外边界跟踪操作”评论...

《魔兽世界》大逃杀!60人新游玩模式《强袭风暴》3月21日上线

暴雪近日发布了《魔兽世界》10.2.6 更新内容,新游玩模式《强袭风暴》即将于3月21 日在亚服上线,届时玩家将前往阿拉希高地展开一场 60 人大逃杀对战。

艾泽拉斯的冒险者已经征服了艾泽拉斯的大地及遥远的彼岸。他们在对抗世界上最致命的敌人时展现出过人的手腕,并且成功阻止终结宇宙等级的威胁。当他们在为即将于《魔兽世界》资料片《地心之战》中来袭的萨拉塔斯势力做战斗准备时,他们还需要在熟悉的阿拉希高地面对一个全新的敌人──那就是彼此。在《巨龙崛起》10.2.6 更新的《强袭风暴》中,玩家将会进入一个全新的海盗主题大逃杀式限时活动,其中包含极高的风险和史诗级的奖励。

《强袭风暴》不是普通的战场,作为一个独立于主游戏之外的活动,玩家可以用大逃杀的风格来体验《魔兽世界》,不分职业、不分装备(除了你在赛局中捡到的),光是技巧和战略的强弱之分就能决定出谁才是能坚持到最后的赢家。本次活动将会开放单人和双人模式,玩家在加入海盗主题的预赛大厅区域前,可以从强袭风暴角色画面新增好友。游玩游戏将可以累计名望轨迹,《巨龙崛起》和《魔兽世界:巫妖王之怒 经典版》的玩家都可以获得奖励。