国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python基于分水嶺算法解決走迷宮游戲示例

2020-02-16 10:19:20
字體:
來源:轉載
供稿:網友

本文實例講述了Python基于分水嶺算法解決走迷宮游戲。分享給大家供大家參考,具體如下:

#Solving maze with morphological transformation"""usage:Solving maze with morphological transformationneeded module:cv2/numpy/sysref:1.http://www.mazegenerator.net/2.http://blog.leanote.com/post/leeyoung/539a629aab35bc44e2000000@author:Robin Chen"""import cv2import numpy as npimport sysdef SolvingMaze(image):#load an image  try:    img = cv2.imread(image)  except Exception,e:    print 'Error:can not open the image!'    sys.exit()#show image  #cv2.namedWindow('image', cv2.WINDOW_NORMAL)  cv2.imshow('maze_image',img)#convert to gray  gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)#show gray image  #cv2.imshow('gray_image',gray_image)#convert to binary image  retval,binary_image = cv2.threshold(gray_image, 10,255, cv2.THRESH_BINARY_INV)  #cv2.imshow('binary_image',binary_image)  contours,hierarchy = cv2.findContours(binary_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)  if len(contours) != 2:    sys.exit("This is not a 'perfect maze' with just 2 walls!")  h, w, d = img.shape#The first wall  path = np.zeros((h,w),dtype = np.uint8)#cv2.CV_8UC1  cv2.drawContours(path, contours, 0, (255,255,255),-1)#cv2.FILLED  #cv2.imshow('The first wall',path)#Dilate the wall by a few pixels  kernel = np.ones((19, 19), dtype = np.uint8)  path = cv2.dilate(path, kernel)  #cv2.imshow('Dilate the wall by a few pixels',path)#Erode by the same amount of pixels  path_erode = cv2.erode(path, kernel);  #cv2.imshow('Erode by the same amount of pixels',path_erode)#absdiff  path = cv2.absdiff(path, path_erode);  #cv2.imshow('absdiff',path)#solution  channels = cv2.split(img);  channels[0] &= ~path;  channels[1] &= ~path;  channels[2] |= path;  dst = cv2.merge(channels);  cv2.imshow("solution", dst);#waiting for any key to close windows  cv2.waitKey(0)  cv2.destroyAllWindows()if __name__ == '__main__':  image = sys.argv[-1]  SolvingMaze(image)

更多關于Python相關內容可查看本站專題:《Python游戲開發技巧總結》、《Python數據結構與算法教程》、《Python Socket編程技巧總結》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》

希望本文所述對大家Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 遵义县| 浮山县| 鹤山市| 兴山县| 夏邑县| 浪卡子县| 邓州市| 金坛市| 堆龙德庆县| 涿州市| 江孜县| 永嘉县| 北安市| 铜陵市| 萨迦县| 安阳市| 兴义市| 北海市| 鹤壁市| 通化县| 安达市| 南昌市| 奉化市| 罗城| 三门峡市| 嘉义县| 德兴市| 孝义市| 宜黄县| 东平县| 洮南市| 政和县| 岑巩县| 合作市| 静乐县| 新泰市| 庄浪县| 司法| 汉川市| 阿鲁科尔沁旗| 宜兴市|