DDR爱好者之家 Design By 杰米

本文实例讲述了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程序设计有所帮助。

DDR爱好者之家 Design By 杰米
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
DDR爱好者之家 Design By 杰米

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?