python如何实现图像外边界跟踪-创新互联
创新互联www.cdcxhl.cn八线动态BGP香港云服务器提供商,新人活动买多久送多久,划算不套路!
创新互联-专业网站定制、快速模板网站建设、高性价比宁阳网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式宁阳网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖宁阳地区。费用合理售后完善,10余年实体公司更值得信赖。这篇文章主要为大家展示了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")
网页题目:python如何实现图像外边界跟踪-创新互联
网页地址:http://ybzwz.com/article/coedph.html