广州凡科互联网科技有限公司

营业时间
MON-SAT 9:00-18:00

全国服务热线
18720358503

公司门店地址
广州市海珠区工业大道北67号凤凰创意园

python怎样完成图象外界限追踪 python完成图象外界

日期:2021-02-18 浏览:
python怎样完成图象外界限追踪 python完成图象外界限追踪编码实例
关注度3 评价 127  网民共享于:  :43 访问数16805次 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 跳出来多种循环系统
#xiaojiayu/
class getoutofloop(Exception): pass
 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
 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 = 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实现图像外边界跟踪代码示例




网站知识

联系方式丨CONTACT

  • 全国热线:18720358503
  • 传真热线:18720358503
  • Q Q咨询:2639601583
  • 企业邮箱:2639601583@qq.com

首页
电话
短信
联系