python实现图像外边界跟踪操作


Posted in Python onJuly 13, 2020

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 相关文章推荐
Python字符串逐字符或逐词反转方法
May 21 Python
使用Python保存网页上的图片或者保存页面为截图
Mar 05 Python
Python实现列表转换成字典数据结构的方法
Mar 11 Python
Python中字符串的处理技巧分享
Sep 17 Python
python实现俄罗斯方块游戏
Mar 25 Python
Python 带有参数的装饰器实例代码详解
Dec 06 Python
Python3实现的简单工资管理系统示例
Mar 12 Python
Python实现的文轩网爬虫完整示例
May 16 Python
Pytorch中index_select() 函数的实现理解
Nov 19 Python
opencv中图像叠加/图像融合/按位操作的实现
Apr 01 Python
python not运算符的实例用法
Jun 30 Python
python中Pyqt5使用Qlabel标签播放视频
Apr 22 Python
Python实现打包成库供别的模块调用
Jul 13 #Python
Python numpy矩阵处理运算工具用法汇总
Jul 13 #Python
解决pyinstaller 打包exe文件太大,用pipenv 缩小exe的问题
Jul 13 #Python
Python使用pyexecjs代码案例解析
Jul 13 #Python
如何在VSCode下使用Jupyter的教程详解
Jul 13 #Python
解决python 虚拟环境删除包无法加载的问题
Jul 13 #Python
利用PyQt5+Matplotlib 绘制静态/动态图的实现代码
Jul 13 #Python
You might like
基于simple_html_dom的使用小结
2013/07/01 PHP
编译PHP报错configure error Cannot find libmysqlclient under usr的解决方法
2014/06/27 PHP
PHP基于关联数组20行代码搞定约瑟夫问题示例
2017/11/07 PHP
PHP实现求两个字符串最长公共子串的方法示例
2017/11/17 PHP
PHP生成随机码的思路与方法实例探索
2019/04/11 PHP
PHP保留两位小数的几种方法
2019/07/24 PHP
IE autocomplete internet explorer's autocomplete
2007/06/30 Javascript
C#中TrimStart,TrimEnd,Trim在javascript上的实现
2011/01/17 Javascript
图片在浏览器中底部对齐 解决方法之一
2011/11/30 Javascript
javascript错误的认识不用关心内存管理
2012/12/15 Javascript
JS 获取鼠标左右键的键值方法
2014/10/11 Javascript
10条建议帮助你创建更好的jQuery插件
2015/05/18 Javascript
jquery实现ajax加载超时提示的方法
2016/07/23 Javascript
jQuery实现表格隔行及滑动,点击时变色的方法【测试可用】
2016/08/20 Javascript
简单实现js菜单栏切换效果
2017/03/04 Javascript
jquery仿苹果的时间/日期选择效果
2017/03/08 Javascript
Vue项目中quill-editor带样式编辑器的使用方法
2017/08/08 Javascript
在React 组件中使用Echarts的示例代码
2017/11/08 Javascript
Vue render深入开发讲解
2018/04/13 Javascript
vue-cli 默认路由再子路由选中下的选中状态问题及解决代码
2018/09/06 Javascript
使用Angular material主题定义自己的组件库的配色体系
2019/09/04 Javascript
对Layer UI 模块化的用法详解
2019/09/26 Javascript
wxPython 入门教程
2008/10/07 Python
python益智游戏计算汉诺塔问题示例
2014/03/05 Python
Python编程使用NLTK进行自然语言处理详解
2017/11/16 Python
利用pandas合并多个excel的方法示例
2019/10/10 Python
Python新手学习函数默认参数设置
2020/06/03 Python
2021年的Python 时间轴和即将推出的功能详解
2020/07/27 Python
出国导师推荐信
2014/01/16 职场文书
《颐和园》教学反思
2014/02/26 职场文书
2014年廉洁自律承诺书
2014/05/26 职场文书
汽车服务工程专业自荐信
2014/09/02 职场文书
2014市府办领导班子“四风问题”对照检查材料思想汇报
2014/09/24 职场文书
公司酒会主持词
2015/07/02 职场文书
检讨书格式
2019/04/25 职场文书
JPA 通过Specification如何实现复杂查询
2021/11/23 Java/Android