金狮镖局 Design By www.egabc.com
本文实例讲述了Python基于分水岭算法解决走迷宫游戏。分享给大家供大家参考,具体如下:
#Solving maze with morphological transformation """ usage:Solving maze with morphological transformation needed module:cv2/numpy/sys ref: 1.http://www.mazegenerator.net/ 2.http://blog.leanote.com/post/leeyoung/539a629aab35bc44e2000000 @author:Robin Chen """ import cv2 import numpy as np import sys def 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程序设计有所帮助。
金狮镖局 Design By www.egabc.com
金狮镖局
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
金狮镖局 Design By www.egabc.com
暂无Python基于分水岭算法解决走迷宫游戏示例的评论...
更新日志
2024年11月18日
2024年11月18日
- 曾庆瑜1990-随风而逝[日本东芝1A1首版][WAV+CUE]
- 群星.2015-凭着爱ADMS2CD【华纳】【WAV+CUE】
- 陈冠希.2017-一只猴子3部曲【摩登天空】【WAV+CUE】
- 金元萱.1996-迷迷糊糊【宝丽金】【WAV+CUE】
- 齐秦《燃烧爱情》马来西亚版[WAV+CUE][1G]
- 动力火车《结伴》2024最新 [FLAC分轨][1G]
- 郑源《擦肩而过》[WAV+CUE][1.2G]
- 黑鸭子2008-江南四月天[首版][WAV+CUE]
- 黑鸭子2008-再醉一次·精选[首版][WAV+CUE]
- Elgar-Motdamour-UlfWallin,RolandPontinen(2024)[24bit-96kHz]FLAC
- 苏永康《 笑下去》 新曲+精选[WAV+CUE][1G]
- 周传雄《发觉》[WAV+CUE][1.1G]
- 证声音乐图书馆《真夏派对 x 浩室》[320K/MP3][67.19MB]
- 张镐哲.1994-无助【波丽佳音】【WAV+CUE】
- Relic.2024-浮在虛无的诗意【SEEAHOLE】【FLAC分轨】