Skip to content

Commit

Permalink
Merge pull request #2 from mturk24/master
Browse files Browse the repository at this point in the history
Fixed syntax issues and logic
  • Loading branch information
SZanlongo authored Mar 9, 2018
2 parents 8af07eb + b87105c commit 5a6038d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 31 deletions.
8 changes: 2 additions & 6 deletions src/Testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@
from VanishingPoint import *

i = 0
for subdir, dirs, files in os.walk('../pictures/input'):
for subdir, dirs, files in os.walk('/pictures/input'):
for file in files:
filepath = subdir + os.sep + file

if filepath.endswith(".jpg"):
i += 1
img = cv2.imread(filepath)

hough_lines = hough_transform(img)
if hough_lines:
random_sample = sample_lines(hough_lines, 100)
intersections = find_intersections(random_sample, img)
if intersections:
grid_size = min(img.shape[0], img.shape[1]) // 3
vanishing_point = find_vanishing_point(img, grid_size, intersections)

filename = '../pictures/output/center' + str(i) + '.jpg'

filename = '/pictures/output/center' + str(i) + '.jpg'
cv2.imwrite(filename, img)
50 changes: 25 additions & 25 deletions src/VanishingPoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,21 @@ def hough_transform(img):

lines = cv2.HoughLines(edges, 1, np.pi / 180, 200) # Hough line detection
hough_lines = []

# Lines are represented by rho, theta; convert to endpoint notation
for line in lines:
for rho, theta in line:
a = np.cos(theta)
b = np.sin(theta)
x0 = a * rho
y0 = b * rho
x1 = int(x0 + 1000 * (-b))
y1 = int(y0 + 1000 * (a))
x2 = int(x0 - 1000 * (-b))
y2 = int(y0 - 1000 * (a))

cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
hough_lines.append(((x1, y1), (x2, y2)))
if lines is not None:
for line in lines:
for rho, theta in line:
a = np.cos(theta)
b = np.sin(theta)
x0 = a * rho
y0 = b * rho
x1 = int(x0 + 1000 * (-b))
y1 = int(y0 + 1000 * (a))
x2 = int(x0 - 1000 * (-b))
y2 = int(y0 - 1000 * (a))

cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
hough_lines.append(((x1, y1), (x2, y2)))

# cv2.imwrite('../pictures/output/hough.jpg', img)
return hough_lines
Expand Down Expand Up @@ -67,9 +67,9 @@ def line_intersection(line1, line2):
# Find intersections between multiple lines (not line segments!)
def find_intersections(lines, img):
intersections = []
for i in xrange(len(lines)):
for i in range(len(lines)):
line1 = lines[i]
for j in xrange(i + 1, len(lines)):
for j in range(i + 1, len(lines)):
line2 = lines[j]

if not line1 == line2:
Expand Down Expand Up @@ -97,10 +97,10 @@ def find_vanishing_point(img, grid_size, intersections):

# Current cell with most intersection points
max_intersections = 0
best_cell = None
best_cell = (0.0, 0.0)

for i in xrange(grid_rows):
for j in xrange(grid_columns):
for i in range(grid_rows):
for j in range(grid_columns):
cell_left = i * grid_size
cell_right = (i + 1) * grid_size
cell_bottom = j * grid_size
Expand All @@ -116,12 +116,12 @@ def find_vanishing_point(img, grid_size, intersections):
if current_intersections > max_intersections:
max_intersections = current_intersections
best_cell = ((cell_left + cell_right) / 2, (cell_bottom + cell_top) / 2)

if not best_cell == [None, None]:
rx1 = best_cell[0] - grid_size / 2
ry1 = best_cell[1] - grid_size / 2
rx2 = best_cell[0] + grid_size / 2
ry2 = best_cell[1] + grid_size / 2
print(best_cell, "this best cell")
if best_cell[0] != None and best_cell[1] != None:
rx1 = int(best_cell[0] - grid_size / 2)
ry1 = int(best_cell[1] - grid_size / 2)
rx2 = int(best_cell[0] + grid_size / 2)
ry2 = int(best_cell[1] + grid_size / 2)
cv2.rectangle(img, (rx1, ry1), (rx2, ry2), (0, 255, 0), 10)
# cv2.imwrite('../pictures/output/center.jpg', img)

Expand Down

0 comments on commit 5a6038d

Please sign in to comment.