一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

python如何实现图像外边界跟踪 python实现图像外边界跟踪代码示例

时间:2020-07-13 编辑:袖梨 来源:一聚教程网

python如何实现图像外边界跟踪?本篇文章小编给大家分享一下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")

搜索过程,红色标记线如下:

热门栏目