Python基于分水岭算法解决走迷宫游戏示例
作者:白交_Robin 时间:2021-08-04 17:41:37
本文实例讲述了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程序设计有所帮助。
来源:http://blog.csdn.net/chendejiao/article/details/43231659
标签:Python,算法,游戏
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
"模板化"——限制还是激发
2009-03-26 11:36:00
python封装成exe的超详细教程
2021-07-30 14:46:20
![](https://img.aspxhome.com/file/2023/3/107813_0s.jpg)
asp可设置参数的分页代码例子
2007-10-07 12:34:00
文章内链(标签)的一个思路
2009-10-31 19:03:00
Python字典遍历的陷阱
2022-11-16 21:59:11
基于python SMTP实现自动发送邮件教程解析
2023-03-08 20:15:02
![](https://img.aspxhome.com/file/2023/2/121602_0s.png)
Python文本文件的合并操作方法代码实例
2022-12-07 21:52:54
![](https://img.aspxhome.com/file/2023/3/101713_0s.jpg)
Python 中 Shutil 模块详情
2022-12-20 15:25:58
![](https://img.aspxhome.com/file/2023/8/128618_0s.jpg)
基于JS实现将JSON数据转换为TypeScript类型声明的工具
2023-06-12 16:20:44
Python爬虫破解登陆哔哩哔哩的方法
2021-03-28 14:17:40
使用Python操作excel文件的实例代码
2023-07-14 10:45:50
Python Matplotlib实现三维数据的散点图绘制
2021-01-30 09:45:19
![](https://img.aspxhome.com/file/2023/2/87672_0s.jpg)
Python ORM框架之SQLAlchemy 的基础用法
2023-06-09 18:03:41
![](https://img.aspxhome.com/file/2023/1/131151_0s.png)
浅析python的优势和不足之处
2021-06-11 02:33:20
Windows 平台做 Python 开发的最佳组合(推荐)
2022-12-08 05:38:51
![](https://img.aspxhome.com/file/2023/3/124663_0s.jpg)
获取 Textarea 的光标位置
2010-11-30 21:33:00
Vue 项目中遇到的跨域问题及解决方法(后台php)
2023-11-16 09:14:13
![](https://img.aspxhome.com/file/2023/0/100540_0s.png)
Python3一行代码实现图片文字识别的示例
2021-11-22 06:45:51
![](https://img.aspxhome.com/file/2023/9/127799_0s.jpg)
Python startswith()和endswith() 方法原理解析
2023-05-30 02:16:06
OpenCV+Python几何变换的实现示例
2022-02-09 14:44:52
![](https://img.aspxhome.com/file/2023/7/95747_0s.jpg)