Skip to content

Commit 3f53f53

Browse files
committed
迷宫解密
1 parent 14265da commit 3f53f53

File tree

9 files changed

+234
-0
lines changed

9 files changed

+234
-0
lines changed
64.4 KB
Loading
2.77 KB
Loading
Loading
Loading
Loading
Loading
+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# -*- coding: utf-8 -*-
2+
# @Time : 2017/7/30 18:16
3+
# @Author : play4fun
4+
# @File : aStar1.py.py
5+
# @Software: PyCharm
6+
7+
"""
8+
aStar1.py: 不行!??
9+
"""
10+
11+
import sys
12+
13+
# from Queue import Queue
14+
from multiprocessing import Queue
15+
from PIL import Image
16+
17+
start = (400, 984)
18+
end = (398, 25)
19+
20+
21+
def iswhite(value):
22+
if value == (255, 255, 255):
23+
return True
24+
25+
26+
def getadjacent(n):
27+
x, y = n
28+
return [(x - 1, y), (x, y - 1), (x + 1, y), (x, y + 1)]
29+
30+
31+
def BFS(start, end, pixels):
32+
queue = Queue()
33+
queue.put([start]) # Wrapping the start tuple in a list
34+
35+
while not queue.empty():
36+
37+
path = queue.get()
38+
pixel = path[-1]
39+
40+
if pixel == end:
41+
return path
42+
43+
for adjacent in getadjacent(pixel):
44+
x, y = adjacent
45+
if iswhite(pixels[x, y]):
46+
pixels[x, y] = (127, 127, 127) # see note
47+
new_path = list(path)
48+
new_path.append(adjacent)
49+
queue.put(new_path)
50+
51+
print("Queue has been exhausted. No answer was found.")
52+
53+
54+
if __name__ == '__main__':
55+
56+
# invoke: python mazesolver.py <mazefile> <outputfile>[.jpg|.png|etc.]
57+
base_img = Image.open(sys.argv[1])
58+
base_pixels = base_img.load()
59+
print(base_pixels)
60+
61+
path = BFS(start, end, base_pixels)
62+
if path is None:
63+
print('path is None')
64+
exit(-1)
65+
print('path:',path)
66+
67+
path_img = Image.open(sys.argv[1])
68+
path_pixels = path_img.load()
69+
70+
for position in path:
71+
x, y = position
72+
path_pixels[x, y] = (255, 0, 0) # red
73+
74+
path_img.save(sys.argv[2])

my04-Maze-Solver迷宫解密/maze.cpp

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include<iostream>
2+
#include<cstdlib>
3+
#include<opencv2/core/core.hpp>
4+
#include<opencv2/highgui/highgui.hpp>
5+
#include<opencv2/imgproc/imgproc.hpp>
6+
7+
using namespace std;
8+
using namespace cv;
9+
Mat kernel = Mat::ones(15, 15, CV_8UC1);
10+
class Morph{
11+
private:
12+
int dilationElem,erodeElem;
13+
int dilationSize,erodeSize;
14+
15+
public:
16+
Morph(){
17+
dilationElem=0;
18+
erodeElem=0;
19+
dilationSize=2;
20+
erodeSize=2;
21+
}
22+
Mat dilateImage(Mat input){
23+
Mat temp,element;
24+
int dilationType;
25+
if(dilationElem==0)
26+
dilationType=MORPH_RECT;
27+
else if(dilationElem==1)
28+
dilationType=MORPH_CROSS;
29+
else if(dilationElem==2)
30+
dilationType=MORPH_ELLIPSE;
31+
element= getStructuringElement(dilationType,Size(2*dilationSize+1,2*dilationSize+1),Point(dilationSize,dilationSize));
32+
dilate(input,temp,kernel);
33+
return temp;
34+
}
35+
Mat erodeImage(Mat input){
36+
Mat temp,element;
37+
int erodeType;
38+
if(erodeElem==0)
39+
erodeType=MORPH_RECT;
40+
else if(erodeElem==1)
41+
erodeType=MORPH_CROSS;
42+
else if(erodeElem==2)
43+
erodeType=MORPH_ELLIPSE;
44+
element= getStructuringElement(erodeType,Size(2*erodeSize+1,2*erodeSize+1),Point(erodeSize,erodeSize));
45+
dilate(input,temp,kernel);
46+
return temp;
47+
}
48+
};
49+
int main(int argc, char **argv){
50+
if(argc!=2){
51+
cout<<"Wait for an image"<<endl;
52+
return -1;
53+
}
54+
vector<vector<Point> > contours;
55+
Mat inputMaze,gray,binary,dilation,erosion,imgDiff,BGRcomp[3],imgDiff_inv,output,red,green;
56+
Morph mp;
57+
inputMaze=imread(argv[1],CV_LOAD_IMAGE_COLOR);
58+
cvtColor(inputMaze,gray,CV_BGR2GRAY);
59+
threshold(gray,binary,127,255,CV_THRESH_BINARY_INV);
60+
61+
findContours(binary,contours,CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
62+
drawContours(binary, contours, 0, CV_RGB(255,255,255), CV_FILLED);
63+
threshold(binary,binary,240,255,CV_THRESH_BINARY);
64+
65+
dilation=mp.dilateImage(binary);
66+
erosion=mp.erodeImage(dilation);
67+
absdiff(dilation,erosion,imgDiff);
68+
bitwise_not(imgDiff,imgDiff_inv);
69+
split(inputMaze,BGRcomp);
70+
namedWindow("diff",WINDOW_AUTOSIZE);
71+
imshow("diff",imgDiff);
72+
bitwise_and(BGRcomp[2],BGRcomp[2],red,imgDiff_inv);
73+
bitwise_and(BGRcomp[1],BGRcomp[1],green,imgDiff_inv);
74+
BGRcomp[2]=red.clone();
75+
BGRcomp[1]=green.clone();
76+
merge(BGRcomp,3,output);
77+
namedWindow("SolvedMaze",WINDOW_AUTOSIZE);
78+
imshow("SolvedMaze",output);
79+
//imwrite("OutputMaze.jpg",output);
80+
waitKey(0);
81+
return 0;
82+
}

my04-Maze-Solver迷宫解密/maze.py

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
3+
'''
4+
源文件是使用opencv2.4,
5+
改成opencv3.2有点问题。
6+
https://ishankgulati.github.io/posts/Maze-Solver/
7+
'''
8+
import cv2
9+
import numpy as np
10+
11+
12+
13+
img = cv2.imread('SampleImages/1.png')
14+
# img = cv2.imread('SampleImages/2.png')
15+
# img = cv2.imread('SampleImages/3.jpg')#不行,得修改
16+
# img = cv2.imread('SampleImages/huge_maze.jpg')#不行,得修改
17+
cv2.imshow('maze',img)
18+
cv2.waitKey(0)
19+
20+
# Binary conversion
21+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
22+
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)#反转tholdolding将给我们一个二进制的图像与白色的墙壁和黑色的背景。
23+
cv2.imshow('THRESH_BINARY_INV',thresh)
24+
cv2.waitKey(0)
25+
26+
# Contours
27+
image,contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
28+
cv2.CHAIN_APPROX_NONE)
29+
print('len(contours):',len(contours))
30+
# dc=cv2.drawContours(thresh, contours, 0, (255, 255, 255), -1)
31+
dc=cv2.drawContours(thresh, contours, 0, (255, 255, 255), 5)#用不同颜色来标注
32+
dc=cv2.drawContours(dc, contours, 1, (0, 0, 0), 5)# TODO 大迷宫的len(contours): 26
33+
cv2.imshow('drawContours',dc)
34+
cv2.waitKey(0)
35+
36+
ret, thresh = cv2.threshold(dc, 240, 255, cv2.THRESH_BINARY)
37+
# ret, thresh = cv2.threshold(thresh, 240, 255, cv2.THRESH_BINARY)
38+
cv2.imshow('thresh2',thresh)
39+
cv2.waitKey(0)
40+
41+
# Dilate
42+
'''
43+
扩张
44+
45+
扩张是数学形态领域的两个基本操作者之一,另一个是侵蚀。它通常应用于二进制图像,但有一些版本可用于灰度图像。操作者对二进制图像的基本效果是逐渐扩大前景像素区域的边界(通常为白色像素)。因此,前景像素的面积大小增加,而这些区域内的孔变小。
46+
'''
47+
ke = 10
48+
# kernel = np.ones((19, 19), np.uint8)
49+
kernel = np.ones((ke, ke), np.uint8)
50+
dilation = cv2.dilate(thresh, kernel, iterations=1)
51+
cv2.imshow('dilation',dilation)
52+
cv2.waitKey(0)
53+
54+
# Erosion
55+
#侵蚀是第二个形态运算符。它也适用于二进制图像。操作者对二进制图像的基本效果是消除前景像素区域的边界(通常为白色像素)。因此,前景像素的面积缩小,并且这些区域内的孔变大。
56+
erosion = cv2.erode(dilation, kernel, iterations=1)
57+
cv2.imshow('erosion',erosion)
58+
cv2.waitKey(0)
59+
60+
#找到两个图像的差异
61+
diff = cv2.absdiff(dilation, erosion)
62+
cv2.imshow('diff',diff)
63+
cv2.waitKey(0)
64+
65+
# splitting the channels of maze
66+
b, g, r = cv2.split(img)
67+
mask_inv = cv2.bitwise_not(diff)
68+
#为了在原始迷宫图像上显示解决方案,首先将原来的迷宫分割成r,g,b组件。现在通过反转diff图像创建一个掩码。使用在最后一步中创建的掩码的原始迷宫的按位和r和g分量。这一步将从迷宫解决方案的图像部分去除红色和绿色成分。最后一个是合并所有组件,我们将使用蓝色标记的解决方案。
69+
# masking out the green and red colour from the solved path
70+
r = cv2.bitwise_and(r, r, mask=mask_inv)
71+
g = cv2.bitwise_and(g, g, mask=mask_inv)
72+
73+
res = cv2.merge((b, g, r))
74+
cv2.imshow('Solved Maze', res)
75+
cv2.imwrite('SampleImages/Solved-Maze-1.png',res)
76+
77+
cv2.waitKey(0)
78+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)