没有合适的资源?快使用搜索试试~ 我知道了~
首页python实现图像外边界跟踪操作
资源详情
资源评论
资源推荐

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")

















安全验证
文档复制为VIP权益,开通VIP直接复制

评论0