Skip to content

Commit 3543e12

Browse files
committed
pep-8 compatibility
1 parent 3e49745 commit 3543e12

21 files changed

+256
-254
lines changed

Astar Search/SampleImages.zip

-1.36 KB
Binary file not shown.
4.33 KB
Loading
4.33 KB
Loading
4.33 KB
Loading
4.4 KB
Loading
4.35 KB
Loading

Astar Search/pathPlanning.py

+105-101
Original file line numberDiff line numberDiff line change
@@ -2,163 +2,167 @@
22
import numpy as np
33
import heapq
44

5-
#no of rows and columns in grid
6-
rows=10
7-
columns=10
5+
# no of rows and columns in grid
6+
rows = 10
7+
columns = 10
8+
89

910
def grid_map(img):
10-
#create a 2d array
11-
grid=np.zeros((rows,columns))
11+
# create a 2d array
12+
grid = np.zeros((rows, columns))
1213
for i in range(rows):
1314
for j in range(columns):
14-
#white blocks
15-
if (np.array_equal(img[20+(i*40),20+(j*40)],[255,255,255])):
15+
# white blocks
16+
if (np.array_equal(img[20+(i*40), 20+(j*40)], [255, 255, 255])):
1617
grid[i][j] = 0
17-
#start -> orange block
18-
elif (np.array_equal(img[20+(i*40),20+(j*40)],[39,127,255])):
18+
# start -> orange block
19+
elif (np.array_equal(img[20+(i*40), 20+(j*40)], [39, 127, 255])):
1920
grid[i][j] = 2
20-
#end -> pink block
21-
elif (np.array_equal(img[20+(i*40),20+(j*40)],[201,174,255])):
21+
# end -> pink block
22+
elif (np.array_equal(img[20+(i*40), 20+(j*40)], [201, 174, 255])):
2223
grid[i][j] = 3
23-
#obstacles ->black blocks
24+
# obstacles ->black blocks
2425
else:
2526
grid[i][j] = 1
2627
return grid
2728

29+
2830
class Cell(object):
29-
def __init__(self,x,y,reachable):
30-
#setting some parameters for each cell
31-
self.reachable=reachable
32-
self.x=x
33-
self.y=y
34-
self.parent= None
35-
self.cost=0
36-
self.heuristic=0
37-
#net_cost=cost+heuristic
38-
self.net_cost=0
31+
def __init__(self, x, y, reachable):
32+
# setting some parameters for each cell
33+
self.reachable = reachable
34+
self.x = x
35+
self.y = y
36+
self.parent = None
37+
self.cost = 0
38+
self.heuristic = 0
39+
# net_cost=cost+heuristic
40+
self.net_cost = 0
41+
3942

4043
class Astar(object):
4144
def __init__(self):
42-
#list of unchecked neighbour cells
45+
# list of unchecked neighbour cells
4346
self.open = []
44-
#keeps cells with lowest total_cost at top
47+
# keeps cells with lowest total_cost at top
4548
heapq.heapify(self.open)
46-
#list of already checked cells
47-
self.closed= set()
48-
#list of neighbour cells
49-
self.cells=[]
50-
51-
def init_grid(self,grid):
49+
# list of already checked cells
50+
self.closed = set()
51+
# list of neighbour cells
52+
self.cells = []
53+
54+
def init_grid(self, grid):
5255
for i in range(rows):
5356
for j in range(columns):
54-
#detecting the obstacles
55-
if grid[i][j]==1:
56-
reachable= False
57+
# detecting the obstacles
58+
if grid[i][j] == 1:
59+
reachable = False
5760
else:
58-
reachable= True
59-
self.cells.append(Cell(i,j,reachable))
60-
#detecting the start and end
61-
if(grid[i][j]==2):
62-
self.start=self.cell(i,j)
63-
if(grid[i][j]==3):
64-
self.end=self.cell(i,j)
65-
66-
def cell(self,x,y):
67-
#returns the location to identify each cell
61+
reachable = True
62+
self.cells.append(Cell(i, j, reachable))
63+
# detecting the start and end
64+
if(grid[i][j] == 2):
65+
self.start = self.cell(i, j)
66+
if(grid[i][j] == 3):
67+
self.end = self.cell(i, j)
68+
69+
def cell(self, x, y):
70+
# returns the location to identify each cell
6871
return self.cells[x*columns+y]
6972

70-
def cell_heuristic(self,cell):
71-
#returns the heuristic for astar algo
73+
def cell_heuristic(self, cell):
74+
# returns the heuristic for astar algo
7275
return abs(cell.x-self.end.x)+abs(cell.y-self.end.y)
7376

74-
def neighbour(self,cell):
75-
cells=[]
76-
#returns a list of neigbours of a cell
77-
if cell.x<columns-1:
78-
cells.append(self.cell(cell.x+1,cell.y))
79-
if cell.x>0:
80-
cells.append(self.cell(cell.x-1,cell.y))
81-
if cell.y<rows-1:
82-
cells.append(self.cell(cell.x,cell.y+1))
83-
if cell.y>0:
84-
cells.append(self.cell(cell.x,cell.y-1))
77+
def neighbour(self, cell):
78+
cells = []
79+
# returns a list of neigbours of a cell
80+
if cell.x < columns - 1:
81+
cells.append(self.cell(cell.x+1, cell.y))
82+
if cell.x > 0:
83+
cells.append(self.cell(cell.x-1, cell.y))
84+
if cell.y < rows-1:
85+
cells.append(self.cell(cell.x, cell.y+1))
86+
if cell.y > 0:
87+
cells.append(self.cell(cell.x, cell.y-1))
8588
return cells
8689

87-
def update_cell(self,adj,cell):
88-
#update the details about the selected neigbour cell
89-
adj.cost=cell.cost+1
90-
adj.heuristic=self.cell_heuristic(adj)
91-
adj.parent=cell
92-
adj.net_cost= adj.cost + adj.heuristic
90+
def update_cell(self, adj, cell):
91+
# update the details about the selected neigbour cell
92+
adj.cost = cell.cost + 1
93+
adj.heuristic = self.cell_heuristic(adj)
94+
adj.parent = cell
95+
adj.net_cost = adj.cost + adj.heuristic
9396

94-
9597
def display_path(self):
96-
#list for storing the path
97-
route_path =[]
98-
#flag to determine length of path
99-
count=0
100-
cell=self.end
98+
# list for storing the path
99+
route_path = []
100+
# flag to determine length of path
101+
count = 0
102+
cell = self.end
101103
while cell.parent is not None:
102-
#storing the parents in list from end to start
103-
route_path.append([(cell.y)+1,(cell.x)+1])
104-
cell=cell.parent
105-
count+=1
106-
return route_path,count
104+
# storing the parents in list from end to start
105+
route_path.append([(cell.y)+1, (cell.x)+1])
106+
cell = cell.parent
107+
count += 1
108+
return route_path, count
107109

108110
def search(self):
109-
#pushing the first element in open queue
110-
heapq.heappush(self.open,(self.start.net_cost,self.start))
111+
# pushing the first element in open queue
112+
heapq.heappush(self.open, (self.start.net_cost, self.start))
111113
while(len(self.open)):
112-
net_cost,cell=heapq.heappop(self.open)
113-
#adding the checked cell to closed list
114+
net_cost, cell = heapq.heappop(self.open)
115+
# adding the checked cell to closed list
114116
self.closed.add(cell)
115117
if cell is self.end:
116-
#store path and path legth
118+
# store path and path legth
117119
route_path, route_length = self.display_path()
118120
route_path.reverse()
119121
break
120-
#getting the adjoint cells
121-
neighbours=self.neighbour(cell)
122+
# getting the adjoint cells
123+
neighbours = self.neighbour(cell)
122124
for path in neighbours:
123-
if path.reachable and path not in self.closed: #if cell is not an obstacle and has not been already checked
124-
if (path.net_cost,path) in self.open:
125-
if path.cost > cell.cost + 1: #selecting the cell with least cost
126-
self.update_cell(path,cell)
125+
# if cell is not an obstacle and has not been already checked
126+
if path.reachable and path not in self.closed:
127+
if (path.net_cost, path) in self.open:
128+
# selecting the cell with least cost
129+
if path.cost > cell.cost + 1:
130+
self.update_cell(path, cell)
127131
else:
128-
self.update_cell(path,cell)
129-
heapq.heappush(self.open,(path.net_cost,path))
132+
self.update_cell(path, cell)
133+
heapq.heappush(self.open, (path.net_cost, path))
130134
return route_path, route_length
131135

132136

133137
def play(img):
134-
#map the grid in an array
138+
# map the grid in an array
135139
grid = grid_map(img)
136140

137-
#executing A*
138-
solution=Astar()
141+
# executing A*
142+
solution = Astar()
139143
solution.init_grid(grid)
140-
route_path,route_length = solution.search()
141-
144+
route_path, route_length = solution.search()
145+
142146
return route_length, route_path
143147

144148

145149
if __name__ == "__main__":
146-
#code for checking output for single image
147-
img = cv2.imread('test_images/test_image1.png')
150+
# code for checking output for single image
151+
img = cv2.imread('SampleImages/test_image1.png')
148152
route_length, route_path = play(img)
149153
print "OUTPUT FOR SINGLE IMAGE (IMAGE 1)..."
150154
print "route length = ", route_length
151155
print "route path = ", route_path
152-
#code for checking output for all images
156+
# code for checking output for all images
153157
route_length_list = []
154-
route_path_list = []
155-
for file_number in range(1,6):
156-
file_name = "test_images/test_image"+str(file_number)+".png"
158+
route_path_list = []
159+
for file_number in range(1, 6):
160+
file_name = "SampleImages/test_image"+str(file_number)+".png"
157161
pic = cv2.imread(file_name)
158162
route_length, route_path = play(pic)
159163
route_length_list.append(route_length)
160164
route_path_list.append(route_path)
161-
print "OUTPUT FOR ALL IMAGES..."
162-
for i in range(5):
163-
print "route length for image ",i+1," = " ,route_length_list[i]
164-
print "route path for image ",i+1," = ", route_path_list[i]
165+
print "OUTPUT FOR ALL IMAGES..."
166+
for i in range(5):
167+
print "route length for image ", i+1, " = ", route_length_list[i]
168+
print "route path for image ", i+1, " = ", route_path_list[i]

Maze Solving/SampleImages.zip

-453 KB
Binary file not shown.

Maze Solving/SampleImages/1.png

64.4 KB
Loading

Maze Solving/SampleImages/2.png

2.77 KB
Loading

Maze Solving/SampleImages/3.jpg

477 KB
Loading

Maze Solving/maze.py

+20-21
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,37 @@
11
import cv2
22
import numpy as np
33

4-
img = cv2.imread('40.png')
4+
img = cv2.imread('SampleImages/2.png')
55

6-
#Binary conversion
7-
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
8-
ret, thresh = cv2.threshold(gray,127,255,cv2.THRESH_BINARY_INV)
6+
# Binary conversion
7+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
8+
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY_INV)
99

10-
#Contours
11-
contours, hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
12-
cv2.drawContours(thresh,contours,0,(255,255,255),-1)
13-
ret, thresh = cv2.threshold(thresh,240,255,cv2.THRESH_BINARY)
10+
# Contours
11+
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_EXTERNAL,
12+
cv2.CHAIN_APPROX_NONE)
13+
cv2.drawContours(thresh, contours, 0, (255, 255, 255), -1)
14+
ret, thresh = cv2.threshold(thresh, 240, 255, cv2.THRESH_BINARY)
1415

15-
#Dilate
16-
kernel = np.ones((19,19),np.uint8)
16+
# Dilate
17+
kernel = np.ones((19, 19), np.uint8)
1718
dilation = cv2.dilate(thresh, kernel, iterations=1)
1819

19-
#Erosion
20+
# Erosion
2021
erosion = cv2.erode(dilation, kernel, iterations=1)
2122

22-
diff = cv2.absdiff(dilation,erosion)
23+
diff = cv2.absdiff(dilation, erosion)
2324

24-
#splitting the channels of maze
25-
b,g,r = cv2.split(img)
26-
27-
mask=diff
25+
# splitting the channels of maze
26+
b, g, r = cv2.split(img)
2827
mask_inv = cv2.bitwise_not(diff)
2928

30-
#masking out the green and red colour from the solved path
31-
r = cv2.bitwise_and(r,r,mask=mask_inv)
32-
g = cv2.bitwise_and(g,g,mask=mask_inv)
29+
# masking out the green and red colour from the solved path
30+
r = cv2.bitwise_and(r, r, mask=mask_inv)
31+
g = cv2.bitwise_and(g, g, mask=mask_inv)
3332

34-
res=cv2.merge((b,g,r))
35-
cv2.imshow('Solved Maze',res)
33+
res = cv2.merge((b, g, r))
34+
cv2.imshow('Solved Maze', res)
3635

3736
cv2.waitKey(0)
3837
cv2.destroyAllWindows()

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
These are some fun, introductory image processing projects. For more details visit my website [ishankgulati.github.io](ishankgulati.github.io).

Snooker Table game/SampleImages.zip

-43.9 KB
Binary file not shown.

0 commit comments

Comments
 (0)