Python基于分水嶺算法解決走迷宮游戲示例
本文實(shí)例講述了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)
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python游戲開發(fā)技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
matlab輸出數(shù)據(jù)為excel文件的問題
這篇文章主要介紹了matlab輸出數(shù)據(jù)為excel文件的問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08python+django+rest框架配置創(chuàng)建方法
今天小編就為大家分享一篇python+django+rest框架配置創(chuàng)建方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08使用python庫xlsxwriter庫來輸出各種xlsx文件的示例
這篇文章主要介紹了使用python庫xlsxwriter庫來輸出各種xlsx文件的示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09用Python調(diào)用win命令行提高工作效率的實(shí)例
今天小編就為大家分享一篇用Python調(diào)用win命令行提高工作效率的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08Pandas的DataFrame如何做交集,并集,差集與對稱差集
這篇文章主要介紹了Pandas的DataFrame如何做交集,并集,差集與對稱差集,Python的數(shù)據(jù)類型集合由不同元素組成的集合,集合中是一組無序排列的可?Hash?的值,可以作為字典的Key,下面來看看文章的詳細(xì)內(nèi)容吧2022-01-01python編程使用協(xié)程并發(fā)的優(yōu)缺點(diǎn)
協(xié)程是一種用戶態(tài)的輕量級線程,又稱微線程。這篇文章主要介紹了python編程使用協(xié)程并發(fā)的優(yōu)缺點(diǎn),感興趣的朋友跟隨小編一起看看吧2018-09-09Python編程中用close()方法關(guān)閉文件的教程
這篇文章主要介紹了Python編程中用close()方法關(guān)閉文件的教程,是Python編程入門中的基礎(chǔ)知識,需要的朋友可以參考下2015-05-05